blob: 99c5c649a0a261d2612c6f03a7c84849c83ce4d7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ilya Yanok0b23fb32009-07-21 19:32:21 +04002/*
3 * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
4 * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
5 * (C) Copyright 2008 Armadeus Systems nc
6 * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
7 * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
Ilya Yanok0b23fb32009-07-21 19:32:21 +04008 */
9
10#include <common.h>
Jagan Teki60752ca2016-12-06 00:00:49 +010011#include <dm.h>
Alex Kiernan9925f1d2018-04-01 09:22:38 +000012#include <environment.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040013#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060014#include <memalign.h>
Jagan Teki567173a2016-12-06 00:00:50 +010015#include <miiphy.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040016#include <net.h>
Jeroen Hofstee84f64c82014-10-08 22:57:40 +020017#include <netdev.h>
Martin Fuzzeyad8c43c2018-10-04 19:59:20 +020018#include <power/regulator.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040019
Jagan Teki567173a2016-12-06 00:00:50 +010020#include <asm/io.h>
21#include <linux/errno.h>
22#include <linux/compiler.h>
23
Ilya Yanok0b23fb32009-07-21 19:32:21 +040024#include <asm/arch/clock.h>
25#include <asm/arch/imx-regs.h>
Stefano Babic552a8482017-06-29 10:16:06 +020026#include <asm/mach-imx/sys_proto.h>
Michael Trimarchiefd0b792018-06-17 15:22:39 +020027#include <asm-generic/gpio.h>
28
29#include "fec_mxc.h"
Ilya Yanok0b23fb32009-07-21 19:32:21 +040030
31DECLARE_GLOBAL_DATA_PTR;
32
Marek Vasutbc1ce152012-08-29 03:49:49 +000033/*
34 * Timeout the transfer after 5 mS. This is usually a bit more, since
35 * the code in the tightloops this timeout is used in adds some overhead.
36 */
37#define FEC_XFER_TIMEOUT 5000
38
Fabio Estevamdb5b7f52014-08-25 13:34:16 -030039/*
40 * The standard 32-byte DMA alignment does not work on mx6solox, which requires
41 * 64-byte alignment in the DMA RX FEC buffer.
42 * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
43 * satisfies the alignment on other SoCs (32-bytes)
44 */
45#define FEC_DMA_RX_MINALIGN 64
46
Ilya Yanok0b23fb32009-07-21 19:32:21 +040047#ifndef CONFIG_MII
48#error "CONFIG_MII has to be defined!"
49#endif
50
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000051#ifndef CONFIG_FEC_XCV_TYPE
52#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasut392b8502011-09-11 18:05:33 +000053#endif
54
Marek Vasutbe7e87e2011-11-08 23:18:10 +000055/*
56 * The i.MX28 operates with packets in big endian. We need to swap them before
57 * sending and after receiving.
58 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000059#ifdef CONFIG_MX28
60#define CONFIG_FEC_MXC_SWAP_PACKET
61#endif
62
63#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
64
65/* Check various alignment issues at compile time */
66#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
67#error "ARCH_DMA_MINALIGN must be multiple of 16!"
68#endif
69
70#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
71 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
72#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
Marek Vasutbe7e87e2011-11-08 23:18:10 +000073#endif
74
Ilya Yanok0b23fb32009-07-21 19:32:21 +040075#undef DEBUG
76
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000077#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +000078static void swap_packet(uint32_t *packet, int length)
79{
80 int i;
81
82 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
83 packet[i] = __swab32(packet[i]);
84}
85#endif
86
Jagan Teki567173a2016-12-06 00:00:50 +010087/* MII-interface related functions */
88static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr,
89 uint8_t regaddr)
Ilya Yanok0b23fb32009-07-21 19:32:21 +040090{
Ilya Yanok0b23fb32009-07-21 19:32:21 +040091 uint32_t reg; /* convenient holder for the PHY register */
92 uint32_t phy; /* convenient holder for the PHY */
93 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +000094 int val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +040095
96 /*
97 * reading from any PHY's register is done by properly
98 * programming the FEC's MII data register.
99 */
Marek Vasutd133b882011-09-11 18:05:34 +0000100 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Teki567173a2016-12-06 00:00:50 +0100101 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
102 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400103
104 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutd133b882011-09-11 18:05:34 +0000105 phy | reg, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400106
Jagan Teki567173a2016-12-06 00:00:50 +0100107 /* wait for the related interrupt */
Graeme Russa60d1e52011-07-15 23:31:37 +0000108 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000109 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400110 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
111 printf("Read MDIO failed...\n");
112 return -1;
113 }
114 }
115
Jagan Teki567173a2016-12-06 00:00:50 +0100116 /* clear mii interrupt bit */
Marek Vasutd133b882011-09-11 18:05:34 +0000117 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400118
Jagan Teki567173a2016-12-06 00:00:50 +0100119 /* it's now safe to read the PHY's register */
Troy Kisky13947f42012-02-07 14:08:47 +0000120 val = (unsigned short)readl(&eth->mii_data);
Jagan Teki567173a2016-12-06 00:00:50 +0100121 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
122 regaddr, val);
Troy Kisky13947f42012-02-07 14:08:47 +0000123 return val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400124}
125
Anatolij Gustschin58ec4d32018-10-18 16:15:11 +0200126static int fec_get_clk_rate(void *udev, int idx)
127{
128#if IS_ENABLED(CONFIG_IMX8)
129 struct fec_priv *fec;
130 struct udevice *dev;
131 int ret;
132
133 dev = udev;
134 if (!dev) {
135 ret = uclass_get_device(UCLASS_ETH, idx, &dev);
136 if (ret < 0) {
137 debug("Can't get FEC udev: %d\n", ret);
138 return ret;
139 }
140 }
141
142 fec = dev_get_priv(dev);
143 if (fec)
144 return fec->clk_rate;
145
146 return -EINVAL;
147#else
148 return imx_get_fecclk();
149#endif
150}
151
Troy Kisky575c5cc2012-10-22 16:40:41 +0000152static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic4294b242010-02-01 14:51:30 +0100153{
154 /*
155 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
156 * and do not drop the Preamble.
Måns Rullgård843a3e52015-12-08 15:38:45 +0000157 *
158 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
159 * MII_SPEED) register that defines the MDIO output hold time. Earlier
160 * versions are RAZ there, so just ignore the difference and write the
161 * register always.
162 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
163 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
164 * output.
165 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
166 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
167 * holdtime cannot result in a value greater than 3.
Stefano Babic4294b242010-02-01 14:51:30 +0100168 */
Anatolij Gustschin58ec4d32018-10-18 16:15:11 +0200169 u32 pclk;
170 u32 speed;
171 u32 hold;
172 int ret;
173
174 ret = fec_get_clk_rate(NULL, 0);
175 if (ret < 0) {
176 printf("Can't find FEC0 clk rate: %d\n", ret);
177 return;
178 }
179 pclk = ret;
180 speed = DIV_ROUND_UP(pclk, 5000000);
181 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
182
Markus Niebel6ba45cc2014-02-05 10:54:11 +0100183#ifdef FEC_QUIRK_ENET_MAC
184 speed--;
185#endif
Måns Rullgård843a3e52015-12-08 15:38:45 +0000186 writel(speed << 1 | hold << 8, &eth->mii_speed);
Troy Kisky575c5cc2012-10-22 16:40:41 +0000187 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic4294b242010-02-01 14:51:30 +0100188}
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400189
Jagan Teki567173a2016-12-06 00:00:50 +0100190static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
191 uint8_t regaddr, uint16_t data)
Troy Kisky13947f42012-02-07 14:08:47 +0000192{
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400193 uint32_t reg; /* convenient holder for the PHY register */
194 uint32_t phy; /* convenient holder for the PHY */
195 uint32_t start;
196
Jagan Teki567173a2016-12-06 00:00:50 +0100197 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
198 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400199
200 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutd133b882011-09-11 18:05:34 +0000201 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400202
Jagan Teki567173a2016-12-06 00:00:50 +0100203 /* wait for the MII interrupt */
Graeme Russa60d1e52011-07-15 23:31:37 +0000204 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000205 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400206 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
207 printf("Write MDIO failed...\n");
208 return -1;
209 }
210 }
211
Jagan Teki567173a2016-12-06 00:00:50 +0100212 /* clear MII interrupt bit */
Marek Vasutd133b882011-09-11 18:05:34 +0000213 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Teki567173a2016-12-06 00:00:50 +0100214 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
215 regaddr, data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400216
217 return 0;
218}
219
Jagan Teki567173a2016-12-06 00:00:50 +0100220static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
221 int regaddr)
Troy Kisky13947f42012-02-07 14:08:47 +0000222{
Jagan Teki567173a2016-12-06 00:00:50 +0100223 return fec_mdio_read(bus->priv, phyaddr, regaddr);
Troy Kisky13947f42012-02-07 14:08:47 +0000224}
225
Jagan Teki567173a2016-12-06 00:00:50 +0100226static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
227 int regaddr, u16 data)
Troy Kisky13947f42012-02-07 14:08:47 +0000228{
Jagan Teki567173a2016-12-06 00:00:50 +0100229 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
Troy Kisky13947f42012-02-07 14:08:47 +0000230}
231
232#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400233static int miiphy_restart_aneg(struct eth_device *dev)
234{
Stefano Babicb774fe92012-02-22 00:24:35 +0000235 int ret = 0;
236#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200237 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000238 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200239
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400240 /*
241 * Wake up from sleep if necessary
242 * Reset PHY, then delay 300ns
243 */
John Rigbycb17b922010-01-25 23:12:55 -0700244#ifdef CONFIG_MX27
Troy Kisky13947f42012-02-07 14:08:47 +0000245 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbycb17b922010-01-25 23:12:55 -0700246#endif
Troy Kisky13947f42012-02-07 14:08:47 +0000247 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400248 udelay(1000);
249
Jagan Teki567173a2016-12-06 00:00:50 +0100250 /* Set the auto-negotiation advertisement register bits */
Troy Kisky13947f42012-02-07 14:08:47 +0000251 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Jagan Teki567173a2016-12-06 00:00:50 +0100252 LPA_100FULL | LPA_100HALF | LPA_10FULL |
253 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky13947f42012-02-07 14:08:47 +0000254 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Jagan Teki567173a2016-12-06 00:00:50 +0100255 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut2e5f4422011-09-11 18:05:36 +0000256
257 if (fec->mii_postcall)
258 ret = fec->mii_postcall(fec->phy_id);
259
Stefano Babicb774fe92012-02-22 00:24:35 +0000260#endif
Marek Vasut2e5f4422011-09-11 18:05:36 +0000261 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400262}
263
Hannes Schmelzer07507012016-06-22 12:07:14 +0200264#ifndef CONFIG_FEC_FIXED_SPEED
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400265static int miiphy_wait_aneg(struct eth_device *dev)
266{
267 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +0000268 int status;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200269 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000270 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400271
Jagan Teki567173a2016-12-06 00:00:50 +0100272 /* Wait for AN completion */
Graeme Russa60d1e52011-07-15 23:31:37 +0000273 start = get_timer(0);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400274 do {
275 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
276 printf("%s: Autonegotiation timeout\n", dev->name);
277 return -1;
278 }
279
Troy Kisky13947f42012-02-07 14:08:47 +0000280 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
281 if (status < 0) {
282 printf("%s: Autonegotiation failed. status: %d\n",
Jagan Teki567173a2016-12-06 00:00:50 +0100283 dev->name, status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400284 return -1;
285 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500286 } while (!(status & BMSR_LSTATUS));
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400287
288 return 0;
289}
Hannes Schmelzer07507012016-06-22 12:07:14 +0200290#endif /* CONFIG_FEC_FIXED_SPEED */
Troy Kisky13947f42012-02-07 14:08:47 +0000291#endif
292
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400293static int fec_rx_task_enable(struct fec_priv *fec)
294{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000295 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400296 return 0;
297}
298
299static int fec_rx_task_disable(struct fec_priv *fec)
300{
301 return 0;
302}
303
304static int fec_tx_task_enable(struct fec_priv *fec)
305{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000306 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400307 return 0;
308}
309
310static int fec_tx_task_disable(struct fec_priv *fec)
311{
312 return 0;
313}
314
315/**
316 * Initialize receive task's buffer descriptors
317 * @param[in] fec all we know about the device yet
318 * @param[in] count receive buffer count to be allocated
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000319 * @param[in] dsize desired size of each receive buffer
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400320 * @return 0 on success
321 *
Marek Vasut79e5f272013-10-12 20:36:25 +0200322 * Init all RX descriptors to default values.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400323 */
Marek Vasut79e5f272013-10-12 20:36:25 +0200324static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400325{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000326 uint32_t size;
Ye Lif24e4822018-01-10 13:20:44 +0800327 ulong data;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000328 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400329
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400330 /*
Marek Vasut79e5f272013-10-12 20:36:25 +0200331 * Reload the RX descriptors with default values and wipe
332 * the RX buffers.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400333 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000334 size = roundup(dsize, ARCH_DMA_MINALIGN);
335 for (i = 0; i < count; i++) {
Ye Lif24e4822018-01-10 13:20:44 +0800336 data = fec->rbd_base[i].data_pointer;
337 memset((void *)data, 0, dsize);
338 flush_dcache_range(data, data + size);
Marek Vasut79e5f272013-10-12 20:36:25 +0200339
340 fec->rbd_base[i].status = FEC_RBD_EMPTY;
341 fec->rbd_base[i].data_length = 0;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000342 }
343
344 /* Mark the last RBD to close the ring. */
Marek Vasut79e5f272013-10-12 20:36:25 +0200345 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400346 fec->rbd_index = 0;
347
Ye Lif24e4822018-01-10 13:20:44 +0800348 flush_dcache_range((ulong)fec->rbd_base,
349 (ulong)fec->rbd_base + size);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400350}
351
352/**
353 * Initialize transmit task's buffer descriptors
354 * @param[in] fec all we know about the device yet
355 *
356 * Transmit buffers are created externally. We only have to init the BDs here.\n
357 * Note: There is a race condition in the hardware. When only one BD is in
358 * use it must be marked with the WRAP bit to use it for every transmitt.
359 * This bit in combination with the READY bit results into double transmit
360 * of each data buffer. It seems the state machine checks READY earlier then
361 * resetting it after the first transfer.
362 * Using two BDs solves this issue.
363 */
364static void fec_tbd_init(struct fec_priv *fec)
365{
Ye Lif24e4822018-01-10 13:20:44 +0800366 ulong addr = (ulong)fec->tbd_base;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000367 unsigned size = roundup(2 * sizeof(struct fec_bd),
368 ARCH_DMA_MINALIGN);
Marek Vasut79e5f272013-10-12 20:36:25 +0200369
370 memset(fec->tbd_base, 0, size);
371 fec->tbd_base[0].status = 0;
372 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400373 fec->tbd_index = 0;
Marek Vasut79e5f272013-10-12 20:36:25 +0200374 flush_dcache_range(addr, addr + size);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400375}
376
377/**
378 * Mark the given read buffer descriptor as free
379 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
Jagan Teki567173a2016-12-06 00:00:50 +0100380 * @param[in] prbd buffer descriptor to mark free again
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400381 */
Jagan Teki567173a2016-12-06 00:00:50 +0100382static void fec_rbd_clean(int last, struct fec_bd *prbd)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400383{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000384 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400385 if (last)
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000386 flags |= FEC_RBD_WRAP;
Jagan Teki567173a2016-12-06 00:00:50 +0100387 writew(flags, &prbd->status);
388 writew(0, &prbd->data_length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400389}
390
Jagan Tekif54183e2016-12-06 00:00:48 +0100391static int fec_get_hwaddr(int dev_id, unsigned char *mac)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400392{
Fabio Estevambe252b62011-12-20 05:46:31 +0000393 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500394 return !is_valid_ethaddr(mac);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400395}
396
Jagan Teki60752ca2016-12-06 00:00:49 +0100397#ifdef CONFIG_DM_ETH
398static int fecmxc_set_hwaddr(struct udevice *dev)
399#else
Stefano Babic4294b242010-02-01 14:51:30 +0100400static int fec_set_hwaddr(struct eth_device *dev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100401#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400402{
Jagan Teki60752ca2016-12-06 00:00:49 +0100403#ifdef CONFIG_DM_ETH
404 struct fec_priv *fec = dev_get_priv(dev);
405 struct eth_pdata *pdata = dev_get_platdata(dev);
406 uchar *mac = pdata->enetaddr;
407#else
Stefano Babic4294b242010-02-01 14:51:30 +0100408 uchar *mac = dev->enetaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400409 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100410#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400411
412 writel(0, &fec->eth->iaddr1);
413 writel(0, &fec->eth->iaddr2);
414 writel(0, &fec->eth->gaddr1);
415 writel(0, &fec->eth->gaddr2);
416
Jagan Teki567173a2016-12-06 00:00:50 +0100417 /* Set physical address */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400418 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
Jagan Teki567173a2016-12-06 00:00:50 +0100419 &fec->eth->paddr1);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400420 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
421
422 return 0;
423}
424
Jagan Teki567173a2016-12-06 00:00:50 +0100425/* Do initial configuration of the FEC registers */
Marek Vasuta5990b22012-05-01 11:09:41 +0000426static void fec_reg_setup(struct fec_priv *fec)
427{
428 uint32_t rcntrl;
429
Jagan Teki567173a2016-12-06 00:00:50 +0100430 /* Set interrupt mask register */
Marek Vasuta5990b22012-05-01 11:09:41 +0000431 writel(0x00000000, &fec->eth->imask);
432
Jagan Teki567173a2016-12-06 00:00:50 +0100433 /* Clear FEC-Lite interrupt event register(IEVENT) */
Marek Vasuta5990b22012-05-01 11:09:41 +0000434 writel(0xffffffff, &fec->eth->ievent);
435
Jagan Teki567173a2016-12-06 00:00:50 +0100436 /* Set FEC-Lite receive control register(R_CNTRL): */
Marek Vasuta5990b22012-05-01 11:09:41 +0000437
438 /* Start with frame length = 1518, common for all modes. */
439 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advans9d2d9242012-07-19 02:12:46 +0000440 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
441 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
442 if (fec->xcv_type == RGMII)
Marek Vasuta5990b22012-05-01 11:09:41 +0000443 rcntrl |= FEC_RCNTRL_RGMII;
444 else if (fec->xcv_type == RMII)
445 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasuta5990b22012-05-01 11:09:41 +0000446
447 writel(rcntrl, &fec->eth->r_cntrl);
448}
449
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400450/**
451 * Start the FEC engine
452 * @param[in] dev Our device to handle
453 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100454#ifdef CONFIG_DM_ETH
455static int fec_open(struct udevice *dev)
456#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400457static int fec_open(struct eth_device *edev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100458#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400459{
Jagan Teki60752ca2016-12-06 00:00:49 +0100460#ifdef CONFIG_DM_ETH
461 struct fec_priv *fec = dev_get_priv(dev);
462#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400463 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100464#endif
Troy Kisky28774cb2012-02-07 14:08:46 +0000465 int speed;
Ye Lif24e4822018-01-10 13:20:44 +0800466 ulong addr, size;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000467 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400468
469 debug("fec_open: fec_open(dev)\n");
470 /* full-duplex, heartbeat disabled */
471 writel(1 << 2, &fec->eth->x_cntrl);
472 fec->rbd_index = 0;
473
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000474 /* Invalidate all descriptors */
475 for (i = 0; i < FEC_RBD_NUM - 1; i++)
476 fec_rbd_clean(0, &fec->rbd_base[i]);
477 fec_rbd_clean(1, &fec->rbd_base[i]);
478
479 /* Flush the descriptors into RAM */
480 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
481 ARCH_DMA_MINALIGN);
Ye Lif24e4822018-01-10 13:20:44 +0800482 addr = (ulong)fec->rbd_base;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000483 flush_dcache_range(addr, addr + size);
484
Troy Kisky28774cb2012-02-07 14:08:46 +0000485#ifdef FEC_QUIRK_ENET_MAC
Jason Liu2ef2b952011-12-16 05:17:07 +0000486 /* Enable ENET HW endian SWAP */
487 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
Jagan Teki567173a2016-12-06 00:00:50 +0100488 &fec->eth->ecntrl);
Jason Liu2ef2b952011-12-16 05:17:07 +0000489 /* Enable ENET store and forward mode */
490 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
Jagan Teki567173a2016-12-06 00:00:50 +0100491 &fec->eth->x_wmrk);
Jason Liu2ef2b952011-12-16 05:17:07 +0000492#endif
Jagan Teki567173a2016-12-06 00:00:50 +0100493 /* Enable FEC-Lite controller */
John Rigbycb17b922010-01-25 23:12:55 -0700494 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
Jagan Teki567173a2016-12-06 00:00:50 +0100495 &fec->eth->ecntrl);
496
Fabio Estevam7df51fd2013-09-13 00:36:27 -0300497#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby740d6ae2010-01-25 23:12:57 -0700498 udelay(100);
John Rigby740d6ae2010-01-25 23:12:57 -0700499
Jagan Teki567173a2016-12-06 00:00:50 +0100500 /* setup the MII gasket for RMII mode */
John Rigby740d6ae2010-01-25 23:12:57 -0700501 /* disable the gasket */
502 writew(0, &fec->eth->miigsk_enr);
503
504 /* wait for the gasket to be disabled */
505 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
506 udelay(2);
507
508 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
509 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
510
511 /* re-enable the gasket */
512 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
513
514 /* wait until MII gasket is ready */
515 int max_loops = 10;
516 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
517 if (--max_loops <= 0) {
518 printf("WAIT for MII Gasket ready timed out\n");
519 break;
520 }
521 }
522#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400523
Troy Kisky13947f42012-02-07 14:08:47 +0000524#ifdef CONFIG_PHYLIB
Troy Kisky4dc27ee2012-10-22 16:40:45 +0000525 {
Troy Kisky13947f42012-02-07 14:08:47 +0000526 /* Start up the PHY */
Timur Tabi11af8d62012-07-09 08:52:43 +0000527 int ret = phy_startup(fec->phydev);
528
529 if (ret) {
530 printf("Could not initialize PHY %s\n",
531 fec->phydev->dev->name);
532 return ret;
533 }
Troy Kisky13947f42012-02-07 14:08:47 +0000534 speed = fec->phydev->speed;
Troy Kisky13947f42012-02-07 14:08:47 +0000535 }
Hannes Schmelzer07507012016-06-22 12:07:14 +0200536#elif CONFIG_FEC_FIXED_SPEED
537 speed = CONFIG_FEC_FIXED_SPEED;
Troy Kisky13947f42012-02-07 14:08:47 +0000538#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400539 miiphy_wait_aneg(edev);
Troy Kisky28774cb2012-02-07 14:08:46 +0000540 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200541 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky13947f42012-02-07 14:08:47 +0000542#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400543
Troy Kisky28774cb2012-02-07 14:08:46 +0000544#ifdef FEC_QUIRK_ENET_MAC
545 {
546 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wangbcb6e902013-05-27 22:55:43 +0000547 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky28774cb2012-02-07 14:08:46 +0000548 if (speed == _1000BASET)
549 ecr |= FEC_ECNTRL_SPEED;
550 else if (speed != _100BASET)
551 rcr |= FEC_RCNTRL_RMII_10T;
552 writel(ecr, &fec->eth->ecntrl);
553 writel(rcr, &fec->eth->r_cntrl);
554 }
555#endif
556 debug("%s:Speed=%i\n", __func__, speed);
557
Jagan Teki567173a2016-12-06 00:00:50 +0100558 /* Enable SmartDMA receive task */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400559 fec_rx_task_enable(fec);
560
561 udelay(100000);
562 return 0;
563}
564
Jagan Teki60752ca2016-12-06 00:00:49 +0100565#ifdef CONFIG_DM_ETH
566static int fecmxc_init(struct udevice *dev)
567#else
Jagan Teki567173a2016-12-06 00:00:50 +0100568static int fec_init(struct eth_device *dev, bd_t *bd)
Jagan Teki60752ca2016-12-06 00:00:49 +0100569#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400570{
Jagan Teki60752ca2016-12-06 00:00:49 +0100571#ifdef CONFIG_DM_ETH
572 struct fec_priv *fec = dev_get_priv(dev);
573#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400574 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100575#endif
Ye Lif24e4822018-01-10 13:20:44 +0800576 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
577 u8 *i;
578 ulong addr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400579
John Rigbye9319f12010-10-13 14:31:08 -0600580 /* Initialize MAC address */
Jagan Teki60752ca2016-12-06 00:00:49 +0100581#ifdef CONFIG_DM_ETH
582 fecmxc_set_hwaddr(dev);
583#else
John Rigbye9319f12010-10-13 14:31:08 -0600584 fec_set_hwaddr(dev);
Jagan Teki60752ca2016-12-06 00:00:49 +0100585#endif
John Rigbye9319f12010-10-13 14:31:08 -0600586
Jagan Teki567173a2016-12-06 00:00:50 +0100587 /* Setup transmit descriptors, there are two in total. */
Marek Vasut79e5f272013-10-12 20:36:25 +0200588 fec_tbd_init(fec);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400589
Marek Vasut79e5f272013-10-12 20:36:25 +0200590 /* Setup receive descriptors. */
591 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400592
Marek Vasuta5990b22012-05-01 11:09:41 +0000593 fec_reg_setup(fec);
Marek Vasut9eb37702011-09-11 18:05:31 +0000594
benoit.thebaudeau@advansf41471e2012-07-19 02:12:58 +0000595 if (fec->xcv_type != SEVENWIRE)
Troy Kisky575c5cc2012-10-22 16:40:41 +0000596 fec_mii_setspeed(fec->bus->priv);
Marek Vasut9eb37702011-09-11 18:05:31 +0000597
Jagan Teki567173a2016-12-06 00:00:50 +0100598 /* Set Opcode/Pause Duration Register */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400599 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
600 writel(0x2, &fec->eth->x_wmrk);
Jagan Teki567173a2016-12-06 00:00:50 +0100601
602 /* Set multicast address filter */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400603 writel(0x00000000, &fec->eth->gaddr1);
604 writel(0x00000000, &fec->eth->gaddr2);
605
Peng Fan238a53c2018-01-10 13:20:43 +0800606 /* Do not access reserved register */
607 if (!is_mx6ul() && !is_mx6ull() && !is_mx8m()) {
Peng Fanfbecbaa2015-08-12 17:46:51 +0800608 /* clear MIB RAM */
609 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
610 writel(0, i);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400611
Peng Fanfbecbaa2015-08-12 17:46:51 +0800612 /* FIFO receive start register */
613 writel(0x520, &fec->eth->r_fstart);
614 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400615
616 /* size and address of each buffer */
617 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
Ye Lif24e4822018-01-10 13:20:44 +0800618
619 addr = (ulong)fec->tbd_base;
620 writel((uint32_t)addr, &fec->eth->etdsr);
621
622 addr = (ulong)fec->rbd_base;
623 writel((uint32_t)addr, &fec->eth->erdsr);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400624
Troy Kisky13947f42012-02-07 14:08:47 +0000625#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400626 if (fec->xcv_type != SEVENWIRE)
627 miiphy_restart_aneg(dev);
Troy Kisky13947f42012-02-07 14:08:47 +0000628#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400629 fec_open(dev);
630 return 0;
631}
632
633/**
634 * Halt the FEC engine
635 * @param[in] dev Our device to handle
636 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100637#ifdef CONFIG_DM_ETH
638static void fecmxc_halt(struct udevice *dev)
639#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400640static void fec_halt(struct eth_device *dev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100641#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400642{
Jagan Teki60752ca2016-12-06 00:00:49 +0100643#ifdef CONFIG_DM_ETH
644 struct fec_priv *fec = dev_get_priv(dev);
645#else
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200646 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100647#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400648 int counter = 0xffff;
649
Jagan Teki567173a2016-12-06 00:00:50 +0100650 /* issue graceful stop command to the FEC transmitter if necessary */
John Rigbycb17b922010-01-25 23:12:55 -0700651 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Jagan Teki567173a2016-12-06 00:00:50 +0100652 &fec->eth->x_cntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400653
654 debug("eth_halt: wait for stop regs\n");
Jagan Teki567173a2016-12-06 00:00:50 +0100655 /* wait for graceful stop to register */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400656 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbycb17b922010-01-25 23:12:55 -0700657 udelay(1);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400658
Jagan Teki567173a2016-12-06 00:00:50 +0100659 /* Disable SmartDMA tasks */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400660 fec_tx_task_disable(fec);
661 fec_rx_task_disable(fec);
662
663 /*
664 * Disable the Ethernet Controller
665 * Note: this will also reset the BD index counter!
666 */
John Rigby740d6ae2010-01-25 23:12:57 -0700667 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
Jagan Teki567173a2016-12-06 00:00:50 +0100668 &fec->eth->ecntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400669 fec->rbd_index = 0;
670 fec->tbd_index = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400671 debug("eth_halt: done\n");
672}
673
674/**
675 * Transmit one frame
676 * @param[in] dev Our ethernet device to handle
677 * @param[in] packet Pointer to the data to be transmitted
678 * @param[in] length Data count in bytes
679 * @return 0 on success
680 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100681#ifdef CONFIG_DM_ETH
682static int fecmxc_send(struct udevice *dev, void *packet, int length)
683#else
Joe Hershberger442dac42012-05-21 14:45:27 +0000684static int fec_send(struct eth_device *dev, void *packet, int length)
Jagan Teki60752ca2016-12-06 00:00:49 +0100685#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400686{
687 unsigned int status;
Ye Lif24e4822018-01-10 13:20:44 +0800688 u32 size;
689 ulong addr, end;
Marek Vasutbc1ce152012-08-29 03:49:49 +0000690 int timeout = FEC_XFER_TIMEOUT;
691 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400692
693 /*
694 * This routine transmits one frame. This routine only accepts
695 * 6-byte Ethernet addresses.
696 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100697#ifdef CONFIG_DM_ETH
698 struct fec_priv *fec = dev_get_priv(dev);
699#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400700 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100701#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400702
703 /*
704 * Check for valid length of data.
705 */
706 if ((length > 1500) || (length <= 0)) {
Stefano Babic4294b242010-02-01 14:51:30 +0100707 printf("Payload (%d) too large\n", length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400708 return -1;
709 }
710
711 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000712 * Setup the transmit buffer. We are always using the first buffer for
713 * transmission, the second will be empty and only used to stop the DMA
714 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400715 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000716#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000717 swap_packet((uint32_t *)packet, length);
718#endif
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000719
Ye Lif24e4822018-01-10 13:20:44 +0800720 addr = (ulong)packet;
Marek Vasutefe24d22012-08-26 10:19:21 +0000721 end = roundup(addr + length, ARCH_DMA_MINALIGN);
722 addr &= ~(ARCH_DMA_MINALIGN - 1);
723 flush_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000724
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400725 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Ye Lif24e4822018-01-10 13:20:44 +0800726 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000727
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400728 /*
729 * update BD's status now
730 * This block:
731 * - is always the last in a chain (means no chain)
732 * - should transmitt the CRC
733 * - might be the last BD in the list, so the address counter should
734 * wrap (-> keep the WRAP flag)
735 */
736 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
737 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
738 writew(status, &fec->tbd_base[fec->tbd_index].status);
739
740 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000741 * Flush data cache. This code flushes both TX descriptors to RAM.
742 * After this code, the descriptors will be safely in RAM and we
743 * can start DMA.
744 */
745 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
Ye Lif24e4822018-01-10 13:20:44 +0800746 addr = (ulong)fec->tbd_base;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000747 flush_dcache_range(addr, addr + size);
748
749 /*
Marek Vasutab94cd42013-07-12 01:03:04 +0200750 * Below we read the DMA descriptor's last four bytes back from the
751 * DRAM. This is important in order to make sure that all WRITE
752 * operations on the bus that were triggered by previous cache FLUSH
753 * have completed.
754 *
755 * Otherwise, on MX28, it is possible to observe a corruption of the
756 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
757 * for the bus structure of MX28. The scenario is as follows:
758 *
759 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
760 * to DRAM due to flush_dcache_range()
761 * 2) ARM core writes the FEC registers via AHB_ARB2
762 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
763 *
764 * Note that 2) does sometimes finish before 1) due to reordering of
765 * WRITE accesses on the AHB bus, therefore triggering 3) before the
766 * DMA descriptor is fully written into DRAM. This results in occasional
767 * corruption of the DMA descriptor.
768 */
769 readl(addr + size - 4);
770
Jagan Teki567173a2016-12-06 00:00:50 +0100771 /* Enable SmartDMA transmit task */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400772 fec_tx_task_enable(fec);
773
774 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000775 * Wait until frame is sent. On each turn of the wait cycle, we must
776 * invalidate data cache to see what's really in RAM. Also, we need
777 * barrier here.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400778 */
Marek Vasut67449092012-08-29 03:49:50 +0000779 while (--timeout) {
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000780 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasutbc1ce152012-08-29 03:49:49 +0000781 break;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400782 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000783
Fabio Estevamf5992882014-08-25 13:34:17 -0300784 if (!timeout) {
785 ret = -EINVAL;
786 goto out;
787 }
788
789 /*
790 * The TDAR bit is cleared when the descriptors are all out from TX
791 * but on mx6solox we noticed that the READY bit is still not cleared
792 * right after TDAR.
793 * These are two distinct signals, and in IC simulation, we found that
794 * TDAR always gets cleared prior than the READY bit of last BD becomes
795 * cleared.
796 * In mx6solox, we use a later version of FEC IP. It looks like that
797 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
798 * version.
799 *
800 * Fix this by polling the READY bit of BD after the TDAR polling,
801 * which covers the mx6solox case and does not harm the other SoCs.
802 */
803 timeout = FEC_XFER_TIMEOUT;
804 while (--timeout) {
805 invalidate_dcache_range(addr, addr + size);
806 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
807 FEC_TBD_READY))
808 break;
809 }
810
Marek Vasut67449092012-08-29 03:49:50 +0000811 if (!timeout)
812 ret = -EINVAL;
813
Fabio Estevamf5992882014-08-25 13:34:17 -0300814out:
Marek Vasut67449092012-08-29 03:49:50 +0000815 debug("fec_send: status 0x%x index %d ret %i\n",
Jagan Teki567173a2016-12-06 00:00:50 +0100816 readw(&fec->tbd_base[fec->tbd_index].status),
817 fec->tbd_index, ret);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400818 /* for next transmission use the other buffer */
819 if (fec->tbd_index)
820 fec->tbd_index = 0;
821 else
822 fec->tbd_index = 1;
823
Marek Vasutbc1ce152012-08-29 03:49:49 +0000824 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400825}
826
827/**
828 * Pull one frame from the card
829 * @param[in] dev Our ethernet device to handle
830 * @return Length of packet read
831 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100832#ifdef CONFIG_DM_ETH
833static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
834#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400835static int fec_recv(struct eth_device *dev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100836#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400837{
Jagan Teki60752ca2016-12-06 00:00:49 +0100838#ifdef CONFIG_DM_ETH
839 struct fec_priv *fec = dev_get_priv(dev);
840#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400841 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100842#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400843 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
844 unsigned long ievent;
845 int frame_length, len = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400846 uint16_t bd_status;
Ye Lif24e4822018-01-10 13:20:44 +0800847 ulong addr, size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000848 int i;
Ye Li07763ac2018-03-28 20:54:11 +0800849
850#ifdef CONFIG_DM_ETH
851 *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
852 if (*packetp == 0) {
853 printf("%s: error allocating packetp\n", __func__);
854 return -ENOMEM;
855 }
856#else
Fabio Estevamfd37f192013-09-17 23:13:10 -0300857 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ye Li07763ac2018-03-28 20:54:11 +0800858#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400859
Jagan Teki567173a2016-12-06 00:00:50 +0100860 /* Check if any critical events have happened */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400861 ievent = readl(&fec->eth->ievent);
862 writel(ievent, &fec->eth->ievent);
Marek Vasuteda959f2011-10-24 23:40:03 +0000863 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400864 if (ievent & FEC_IEVENT_BABR) {
Jagan Teki60752ca2016-12-06 00:00:49 +0100865#ifdef CONFIG_DM_ETH
866 fecmxc_halt(dev);
867 fecmxc_init(dev);
868#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400869 fec_halt(dev);
870 fec_init(dev, fec->bd);
Jagan Teki60752ca2016-12-06 00:00:49 +0100871#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400872 printf("some error: 0x%08lx\n", ievent);
873 return 0;
874 }
875 if (ievent & FEC_IEVENT_HBERR) {
876 /* Heartbeat error */
877 writel(0x00000001 | readl(&fec->eth->x_cntrl),
Jagan Teki567173a2016-12-06 00:00:50 +0100878 &fec->eth->x_cntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400879 }
880 if (ievent & FEC_IEVENT_GRA) {
881 /* Graceful stop complete */
882 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
Jagan Teki60752ca2016-12-06 00:00:49 +0100883#ifdef CONFIG_DM_ETH
884 fecmxc_halt(dev);
885#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400886 fec_halt(dev);
Jagan Teki60752ca2016-12-06 00:00:49 +0100887#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400888 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
Jagan Teki567173a2016-12-06 00:00:50 +0100889 &fec->eth->x_cntrl);
Jagan Teki60752ca2016-12-06 00:00:49 +0100890#ifdef CONFIG_DM_ETH
891 fecmxc_init(dev);
892#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400893 fec_init(dev, fec->bd);
Jagan Teki60752ca2016-12-06 00:00:49 +0100894#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400895 }
896 }
897
898 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000899 * Read the buffer status. Before the status can be read, the data cache
900 * must be invalidated, because the data in RAM might have been changed
901 * by DMA. The descriptors are properly aligned to cachelines so there's
902 * no need to worry they'd overlap.
903 *
904 * WARNING: By invalidating the descriptor here, we also invalidate
905 * the descriptors surrounding this one. Therefore we can NOT change the
906 * contents of this descriptor nor the surrounding ones. The problem is
907 * that in order to mark the descriptor as processed, we need to change
908 * the descriptor. The solution is to mark the whole cache line when all
909 * descriptors in the cache line are processed.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400910 */
Ye Lif24e4822018-01-10 13:20:44 +0800911 addr = (ulong)rbd;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000912 addr &= ~(ARCH_DMA_MINALIGN - 1);
913 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
914 invalidate_dcache_range(addr, addr + size);
915
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400916 bd_status = readw(&rbd->status);
917 debug("fec_recv: status 0x%x\n", bd_status);
918
919 if (!(bd_status & FEC_RBD_EMPTY)) {
920 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
Jagan Teki567173a2016-12-06 00:00:50 +0100921 ((readw(&rbd->data_length) - 4) > 14)) {
922 /* Get buffer address and size */
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200923 addr = readl(&rbd->data_pointer);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400924 frame_length = readw(&rbd->data_length) - 4;
Jagan Teki567173a2016-12-06 00:00:50 +0100925 /* Invalidate data cache over the buffer */
Marek Vasutefe24d22012-08-26 10:19:21 +0000926 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
927 addr &= ~(ARCH_DMA_MINALIGN - 1);
928 invalidate_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000929
Jagan Teki567173a2016-12-06 00:00:50 +0100930 /* Fill the buffer and pass it to upper layers */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000931#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200932 swap_packet((uint32_t *)addr, frame_length);
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000933#endif
Ye Li07763ac2018-03-28 20:54:11 +0800934
935#ifdef CONFIG_DM_ETH
936 memcpy(*packetp, (char *)addr, frame_length);
937#else
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200938 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500939 net_process_received_packet(buff, frame_length);
Ye Li07763ac2018-03-28 20:54:11 +0800940#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400941 len = frame_length;
942 } else {
943 if (bd_status & FEC_RBD_ERR)
Ye Lif24e4822018-01-10 13:20:44 +0800944 debug("error frame: 0x%08lx 0x%08x\n",
945 addr, bd_status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400946 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000947
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400948 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000949 * Free the current buffer, restart the engine and move forward
950 * to the next buffer. Here we check if the whole cacheline of
951 * descriptors was already processed and if so, we mark it free
952 * as whole.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400953 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000954 size = RXDESC_PER_CACHELINE - 1;
955 if ((fec->rbd_index & size) == size) {
956 i = fec->rbd_index - size;
Ye Lif24e4822018-01-10 13:20:44 +0800957 addr = (ulong)&fec->rbd_base[i];
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000958 for (; i <= fec->rbd_index ; i++) {
959 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
960 &fec->rbd_base[i]);
961 }
962 flush_dcache_range(addr,
Jagan Teki567173a2016-12-06 00:00:50 +0100963 addr + ARCH_DMA_MINALIGN);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000964 }
965
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400966 fec_rx_task_enable(fec);
967 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
968 }
969 debug("fec_recv: stop\n");
970
971 return len;
972}
973
Troy Kiskyef8e3a32012-10-22 16:40:44 +0000974static void fec_set_dev_name(char *dest, int dev_id)
975{
976 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
977}
978
Marek Vasut79e5f272013-10-12 20:36:25 +0200979static int fec_alloc_descs(struct fec_priv *fec)
980{
981 unsigned int size;
982 int i;
983 uint8_t *data;
Ye Lif24e4822018-01-10 13:20:44 +0800984 ulong addr;
Marek Vasut79e5f272013-10-12 20:36:25 +0200985
986 /* Allocate TX descriptors. */
987 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
988 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
989 if (!fec->tbd_base)
990 goto err_tx;
991
992 /* Allocate RX descriptors. */
993 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
994 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
995 if (!fec->rbd_base)
996 goto err_rx;
997
998 memset(fec->rbd_base, 0, size);
999
1000 /* Allocate RX buffers. */
1001
1002 /* Maximum RX buffer size. */
Fabio Estevamdb5b7f52014-08-25 13:34:16 -03001003 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut79e5f272013-10-12 20:36:25 +02001004 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevamdb5b7f52014-08-25 13:34:16 -03001005 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut79e5f272013-10-12 20:36:25 +02001006 if (!data) {
1007 printf("%s: error allocating rxbuf %d\n", __func__, i);
1008 goto err_ring;
1009 }
1010
1011 memset(data, 0, size);
1012
Ye Lif24e4822018-01-10 13:20:44 +08001013 addr = (ulong)data;
1014 fec->rbd_base[i].data_pointer = (uint32_t)addr;
Marek Vasut79e5f272013-10-12 20:36:25 +02001015 fec->rbd_base[i].status = FEC_RBD_EMPTY;
1016 fec->rbd_base[i].data_length = 0;
1017 /* Flush the buffer to memory. */
Ye Lif24e4822018-01-10 13:20:44 +08001018 flush_dcache_range(addr, addr + size);
Marek Vasut79e5f272013-10-12 20:36:25 +02001019 }
1020
1021 /* Mark the last RBD to close the ring. */
1022 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1023
1024 fec->rbd_index = 0;
1025 fec->tbd_index = 0;
1026
1027 return 0;
1028
1029err_ring:
Ye Lif24e4822018-01-10 13:20:44 +08001030 for (; i >= 0; i--) {
1031 addr = fec->rbd_base[i].data_pointer;
1032 free((void *)addr);
1033 }
Marek Vasut79e5f272013-10-12 20:36:25 +02001034 free(fec->rbd_base);
1035err_rx:
1036 free(fec->tbd_base);
1037err_tx:
1038 return -ENOMEM;
1039}
1040
1041static void fec_free_descs(struct fec_priv *fec)
1042{
1043 int i;
Ye Lif24e4822018-01-10 13:20:44 +08001044 ulong addr;
Marek Vasut79e5f272013-10-12 20:36:25 +02001045
Ye Lif24e4822018-01-10 13:20:44 +08001046 for (i = 0; i < FEC_RBD_NUM; i++) {
1047 addr = fec->rbd_base[i].data_pointer;
1048 free((void *)addr);
1049 }
Marek Vasut79e5f272013-10-12 20:36:25 +02001050 free(fec->rbd_base);
1051 free(fec->tbd_base);
1052}
1053
Peng Fan1bcabd72018-03-28 20:54:12 +08001054struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
Jagan Teki60752ca2016-12-06 00:00:49 +01001055{
Peng Fan1bcabd72018-03-28 20:54:12 +08001056 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
Jagan Teki60752ca2016-12-06 00:00:49 +01001057 struct mii_dev *bus;
1058 int ret;
1059
1060 bus = mdio_alloc();
1061 if (!bus) {
1062 printf("mdio_alloc failed\n");
1063 return NULL;
1064 }
1065 bus->read = fec_phy_read;
1066 bus->write = fec_phy_write;
1067 bus->priv = eth;
1068 fec_set_dev_name(bus->name, dev_id);
1069
1070 ret = mdio_register(bus);
1071 if (ret) {
1072 printf("mdio_register failed\n");
1073 free(bus);
1074 return NULL;
1075 }
1076 fec_mii_setspeed(eth);
1077 return bus;
1078}
1079
1080#ifndef CONFIG_DM_ETH
Troy Kiskyfe428b92012-10-22 16:40:46 +00001081#ifdef CONFIG_PHYLIB
1082int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1083 struct mii_dev *bus, struct phy_device *phydev)
1084#else
1085static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1086 struct mii_dev *bus, int phy_id)
1087#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001088{
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001089 struct eth_device *edev;
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001090 struct fec_priv *fec;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001091 unsigned char ethaddr[6];
Andy Duan979a5892017-04-10 19:44:35 +08001092 char mac[16];
Marek Vasute382fb42011-09-11 18:05:37 +00001093 uint32_t start;
1094 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001095
1096 /* create and fill edev struct */
1097 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1098 if (!edev) {
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001099 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasute382fb42011-09-11 18:05:37 +00001100 ret = -ENOMEM;
1101 goto err1;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001102 }
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001103
1104 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1105 if (!fec) {
1106 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasute382fb42011-09-11 18:05:37 +00001107 ret = -ENOMEM;
1108 goto err2;
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001109 }
1110
Nobuhiro Iwamatsude0b9572010-10-19 14:03:42 +09001111 memset(edev, 0, sizeof(*edev));
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001112 memset(fec, 0, sizeof(*fec));
1113
Marek Vasut79e5f272013-10-12 20:36:25 +02001114 ret = fec_alloc_descs(fec);
1115 if (ret)
1116 goto err3;
1117
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001118 edev->priv = fec;
1119 edev->init = fec_init;
1120 edev->send = fec_send;
1121 edev->recv = fec_recv;
1122 edev->halt = fec_halt;
Heiko Schocherfb57ec92010-04-27 07:43:52 +02001123 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001124
Ye Lif24e4822018-01-10 13:20:44 +08001125 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001126 fec->bd = bd;
1127
Marek Vasut392b8502011-09-11 18:05:33 +00001128 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001129
1130 /* Reset chip. */
John Rigbycb17b922010-01-25 23:12:55 -07001131 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasute382fb42011-09-11 18:05:37 +00001132 start = get_timer(0);
1133 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1134 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
Vagrant Cascadian3450a852016-10-23 20:45:19 -07001135 printf("FEC MXC: Timeout resetting chip\n");
Marek Vasut79e5f272013-10-12 20:36:25 +02001136 goto err4;
Marek Vasute382fb42011-09-11 18:05:37 +00001137 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001138 udelay(10);
Marek Vasute382fb42011-09-11 18:05:37 +00001139 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001140
Marek Vasuta5990b22012-05-01 11:09:41 +00001141 fec_reg_setup(fec);
Troy Kiskyef8e3a32012-10-22 16:40:44 +00001142 fec_set_dev_name(edev->name, dev_id);
1143 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kisky13947f42012-02-07 14:08:47 +00001144 fec->bus = bus;
Troy Kiskyfe428b92012-10-22 16:40:46 +00001145 fec_mii_setspeed(bus->priv);
1146#ifdef CONFIG_PHYLIB
1147 fec->phydev = phydev;
1148 phy_connect_dev(phydev, edev);
1149 /* Configure phy */
1150 phy_config(phydev);
1151#else
1152 fec->phy_id = phy_id;
1153#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001154 eth_register(edev);
Andy Duan979a5892017-04-10 19:44:35 +08001155 /* only support one eth device, the index number pointed by dev_id */
1156 edev->index = fec->dev_id;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001157
Andy Duanf01e4e12017-04-10 19:44:34 +08001158 if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1159 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
Stefano Babic4294b242010-02-01 14:51:30 +01001160 memcpy(edev->enetaddr, ethaddr, 6);
Andy Duan979a5892017-04-10 19:44:35 +08001161 if (fec->dev_id)
1162 sprintf(mac, "eth%daddr", fec->dev_id);
1163 else
1164 strcpy(mac, "ethaddr");
Simon Glass00caae62017-08-03 12:22:12 -06001165 if (!env_get(mac))
Simon Glassfd1e9592017-08-03 12:22:11 -06001166 eth_env_set_enetaddr(mac, ethaddr);
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001167 }
Marek Vasute382fb42011-09-11 18:05:37 +00001168 return ret;
Marek Vasut79e5f272013-10-12 20:36:25 +02001169err4:
1170 fec_free_descs(fec);
Marek Vasute382fb42011-09-11 18:05:37 +00001171err3:
1172 free(fec);
1173err2:
1174 free(edev);
1175err1:
1176 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001177}
1178
Troy Kiskyeef24482012-10-22 16:40:42 +00001179int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1180{
Troy Kiskyfe428b92012-10-22 16:40:46 +00001181 uint32_t base_mii;
1182 struct mii_dev *bus = NULL;
1183#ifdef CONFIG_PHYLIB
1184 struct phy_device *phydev = NULL;
1185#endif
1186 int ret;
1187
Peng Fanfbada482018-03-28 20:54:14 +08001188#ifdef CONFIG_FEC_MXC_MDIO_BASE
Troy Kiskyfe428b92012-10-22 16:40:46 +00001189 /*
1190 * The i.MX28 has two ethernet interfaces, but they are not equal.
1191 * Only the first one can access the MDIO bus.
1192 */
Peng Fanfbada482018-03-28 20:54:14 +08001193 base_mii = CONFIG_FEC_MXC_MDIO_BASE;
Troy Kiskyfe428b92012-10-22 16:40:46 +00001194#else
1195 base_mii = addr;
1196#endif
Troy Kiskyeef24482012-10-22 16:40:42 +00001197 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001198 bus = fec_get_miibus(base_mii, dev_id);
1199 if (!bus)
1200 return -ENOMEM;
1201#ifdef CONFIG_PHYLIB
1202 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1203 if (!phydev) {
Måns Rullgård845a57b2015-12-08 15:38:46 +00001204 mdio_unregister(bus);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001205 free(bus);
1206 return -ENOMEM;
1207 }
1208 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1209#else
1210 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1211#endif
1212 if (ret) {
1213#ifdef CONFIG_PHYLIB
1214 free(phydev);
1215#endif
Måns Rullgård845a57b2015-12-08 15:38:46 +00001216 mdio_unregister(bus);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001217 free(bus);
1218 }
1219 return ret;
Troy Kiskyeef24482012-10-22 16:40:42 +00001220}
1221
Troy Kisky09439c32012-10-22 16:40:40 +00001222#ifdef CONFIG_FEC_MXC_PHYADDR
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001223int fecmxc_initialize(bd_t *bd)
1224{
Troy Kiskyeef24482012-10-22 16:40:42 +00001225 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1226 IMX_FEC_BASE);
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001227}
1228#endif
1229
Troy Kisky13947f42012-02-07 14:08:47 +00001230#ifndef CONFIG_PHYLIB
Marek Vasut2e5f4422011-09-11 18:05:36 +00001231int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1232{
1233 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1234 fec->mii_postcall = cb;
1235 return 0;
1236}
Troy Kisky13947f42012-02-07 14:08:47 +00001237#endif
Jagan Teki60752ca2016-12-06 00:00:49 +01001238
1239#else
1240
Jagan Teki1ed25702016-12-06 00:00:51 +01001241static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1242{
1243 struct fec_priv *priv = dev_get_priv(dev);
1244 struct eth_pdata *pdata = dev_get_platdata(dev);
1245
1246 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1247}
1248
Ye Li07763ac2018-03-28 20:54:11 +08001249static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
1250{
1251 if (packet)
1252 free(packet);
1253
1254 return 0;
1255}
1256
Jagan Teki60752ca2016-12-06 00:00:49 +01001257static const struct eth_ops fecmxc_ops = {
1258 .start = fecmxc_init,
1259 .send = fecmxc_send,
1260 .recv = fecmxc_recv,
Ye Li07763ac2018-03-28 20:54:11 +08001261 .free_pkt = fecmxc_free_pkt,
Jagan Teki60752ca2016-12-06 00:00:49 +01001262 .stop = fecmxc_halt,
1263 .write_hwaddr = fecmxc_set_hwaddr,
Jagan Teki1ed25702016-12-06 00:00:51 +01001264 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
Jagan Teki60752ca2016-12-06 00:00:49 +01001265};
1266
1267static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1268{
1269 struct phy_device *phydev;
1270 int mask = 0xffffffff;
1271
Lukasz Majewski178d4f02018-04-15 21:45:54 +02001272#ifdef CONFIG_FEC_MXC_PHYADDR
Jagan Teki60752ca2016-12-06 00:00:49 +01001273 mask = 1 << CONFIG_FEC_MXC_PHYADDR;
1274#endif
1275
1276 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
1277 if (!phydev)
1278 return -ENODEV;
1279
1280 phy_connect_dev(phydev, dev);
1281
1282 priv->phydev = phydev;
1283 phy_config(phydev);
1284
1285 return 0;
1286}
1287
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001288#ifdef CONFIG_DM_GPIO
1289/* FEC GPIO reset */
1290static void fec_gpio_reset(struct fec_priv *priv)
1291{
1292 debug("fec_gpio_reset: fec_gpio_reset(dev)\n");
1293 if (dm_gpio_is_valid(&priv->phy_reset_gpio)) {
1294 dm_gpio_set_value(&priv->phy_reset_gpio, 1);
Martin Fuzzey9b8b9182018-10-04 19:59:18 +02001295 mdelay(priv->reset_delay);
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001296 dm_gpio_set_value(&priv->phy_reset_gpio, 0);
1297 }
1298}
1299#endif
1300
Jagan Teki60752ca2016-12-06 00:00:49 +01001301static int fecmxc_probe(struct udevice *dev)
1302{
1303 struct eth_pdata *pdata = dev_get_platdata(dev);
1304 struct fec_priv *priv = dev_get_priv(dev);
1305 struct mii_dev *bus = NULL;
Jagan Teki60752ca2016-12-06 00:00:49 +01001306 uint32_t start;
1307 int ret;
1308
Anatolij Gustschin58ec4d32018-10-18 16:15:11 +02001309 if (IS_ENABLED(CONFIG_IMX8)) {
1310 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1311 if (ret < 0) {
1312 debug("Can't get FEC ipg clk: %d\n", ret);
1313 return ret;
1314 }
1315 ret = clk_enable(&priv->ipg_clk);
1316 if (ret < 0) {
1317 debug("Can't enable FEC ipg clk: %d\n", ret);
1318 return ret;
1319 }
1320
1321 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
1322 }
1323
Jagan Teki60752ca2016-12-06 00:00:49 +01001324 ret = fec_alloc_descs(priv);
1325 if (ret)
1326 return ret;
1327
Martin Fuzzeyad8c43c2018-10-04 19:59:20 +02001328#ifdef CONFIG_DM_REGULATOR
1329 if (priv->phy_supply) {
1330 ret = regulator_autoset(priv->phy_supply);
1331 if (ret) {
1332 printf("%s: Error enabling phy supply\n", dev->name);
1333 return ret;
1334 }
1335 }
1336#endif
1337
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001338#ifdef CONFIG_DM_GPIO
1339 fec_gpio_reset(priv);
1340#endif
Jagan Teki60752ca2016-12-06 00:00:49 +01001341 /* Reset chip. */
Jagan Teki567173a2016-12-06 00:00:50 +01001342 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1343 &priv->eth->ecntrl);
Jagan Teki60752ca2016-12-06 00:00:49 +01001344 start = get_timer(0);
1345 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1346 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1347 printf("FEC MXC: Timeout reseting chip\n");
1348 goto err_timeout;
1349 }
1350 udelay(10);
1351 }
1352
1353 fec_reg_setup(priv);
Jagan Teki60752ca2016-12-06 00:00:49 +01001354
Peng Fan8b203862018-03-28 20:54:13 +08001355 priv->dev_id = dev->seq;
Peng Fanfbada482018-03-28 20:54:14 +08001356#ifdef CONFIG_FEC_MXC_MDIO_BASE
1357 bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, dev->seq);
1358#else
Peng Fan8b203862018-03-28 20:54:13 +08001359 bus = fec_get_miibus((ulong)priv->eth, dev->seq);
Peng Fanfbada482018-03-28 20:54:14 +08001360#endif
Lothar Waßmann306dd7d2017-06-27 15:23:16 +02001361 if (!bus) {
1362 ret = -ENOMEM;
1363 goto err_mii;
1364 }
1365
1366 priv->bus = bus;
Lothar Waßmann306dd7d2017-06-27 15:23:16 +02001367 priv->interface = pdata->phy_interface;
Martin Fuzzey0126c642018-10-04 19:59:21 +02001368 switch (priv->interface) {
1369 case PHY_INTERFACE_MODE_MII:
1370 priv->xcv_type = MII100;
1371 break;
1372 case PHY_INTERFACE_MODE_RMII:
1373 priv->xcv_type = RMII;
1374 break;
1375 case PHY_INTERFACE_MODE_RGMII:
1376 case PHY_INTERFACE_MODE_RGMII_ID:
1377 case PHY_INTERFACE_MODE_RGMII_RXID:
1378 case PHY_INTERFACE_MODE_RGMII_TXID:
1379 priv->xcv_type = RGMII;
1380 break;
1381 default:
1382 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1383 printf("Unsupported interface type %d defaulting to %d\n",
1384 priv->interface, priv->xcv_type);
1385 break;
1386 }
1387
Lothar Waßmann306dd7d2017-06-27 15:23:16 +02001388 ret = fec_phy_init(priv, dev);
1389 if (ret)
1390 goto err_phy;
1391
Jagan Teki60752ca2016-12-06 00:00:49 +01001392 return 0;
1393
Jagan Teki60752ca2016-12-06 00:00:49 +01001394err_phy:
1395 mdio_unregister(bus);
1396 free(bus);
1397err_mii:
Ye Li2087eac2018-03-28 20:54:16 +08001398err_timeout:
Jagan Teki60752ca2016-12-06 00:00:49 +01001399 fec_free_descs(priv);
1400 return ret;
1401}
1402
1403static int fecmxc_remove(struct udevice *dev)
1404{
1405 struct fec_priv *priv = dev_get_priv(dev);
1406
1407 free(priv->phydev);
1408 fec_free_descs(priv);
1409 mdio_unregister(priv->bus);
1410 mdio_free(priv->bus);
1411
Martin Fuzzeyad8c43c2018-10-04 19:59:20 +02001412#ifdef CONFIG_DM_REGULATOR
1413 if (priv->phy_supply)
1414 regulator_set_enable(priv->phy_supply, false);
1415#endif
1416
Jagan Teki60752ca2016-12-06 00:00:49 +01001417 return 0;
1418}
1419
1420static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1421{
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001422 int ret = 0;
Jagan Teki60752ca2016-12-06 00:00:49 +01001423 struct eth_pdata *pdata = dev_get_platdata(dev);
1424 struct fec_priv *priv = dev_get_priv(dev);
1425 const char *phy_mode;
1426
Simon Glassa821c4a2017-05-17 17:18:05 -06001427 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
Jagan Teki60752ca2016-12-06 00:00:49 +01001428 priv->eth = (struct ethernet_regs *)pdata->iobase;
1429
1430 pdata->phy_interface = -1;
Simon Glasse160f7d2017-01-17 16:52:55 -07001431 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1432 NULL);
Jagan Teki60752ca2016-12-06 00:00:49 +01001433 if (phy_mode)
1434 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1435 if (pdata->phy_interface == -1) {
1436 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1437 return -EINVAL;
1438 }
1439
Martin Fuzzeyad8c43c2018-10-04 19:59:20 +02001440#ifdef CONFIG_DM_REGULATOR
1441 device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
1442#endif
1443
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001444#ifdef CONFIG_DM_GPIO
1445 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
Martin Fuzzey331fcab2018-10-04 19:59:19 +02001446 &priv->phy_reset_gpio, GPIOD_IS_OUT);
1447 if (ret < 0)
1448 return 0; /* property is optional, don't return error! */
Jagan Teki60752ca2016-12-06 00:00:49 +01001449
Martin Fuzzey331fcab2018-10-04 19:59:19 +02001450 priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1);
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001451 if (priv->reset_delay > 1000) {
Martin Fuzzey331fcab2018-10-04 19:59:19 +02001452 printf("FEC MXC: phy reset duration should be <= 1000ms\n");
1453 /* property value wrong, use default value */
1454 priv->reset_delay = 1;
Michael Trimarchiefd0b792018-06-17 15:22:39 +02001455 }
1456#endif
1457
Martin Fuzzey331fcab2018-10-04 19:59:19 +02001458 return 0;
Jagan Teki60752ca2016-12-06 00:00:49 +01001459}
1460
1461static const struct udevice_id fecmxc_ids[] = {
1462 { .compatible = "fsl,imx6q-fec" },
Peng Fan979e0fc2018-03-28 20:54:15 +08001463 { .compatible = "fsl,imx6sl-fec" },
1464 { .compatible = "fsl,imx6sx-fec" },
1465 { .compatible = "fsl,imx6ul-fec" },
Lukasz Majewski948239e2018-04-15 21:54:22 +02001466 { .compatible = "fsl,imx53-fec" },
Anatolij Gustschin58ec4d32018-10-18 16:15:11 +02001467 { .compatible = "fsl,imx7d-fec" },
Jagan Teki60752ca2016-12-06 00:00:49 +01001468 { }
1469};
1470
1471U_BOOT_DRIVER(fecmxc_gem) = {
1472 .name = "fecmxc",
1473 .id = UCLASS_ETH,
1474 .of_match = fecmxc_ids,
1475 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1476 .probe = fecmxc_probe,
1477 .remove = fecmxc_remove,
1478 .ops = &fecmxc_ops,
1479 .priv_auto_alloc_size = sizeof(struct fec_priv),
1480 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1481};
1482#endif