blob: ec21157d71052766e5ce93c82aa6c7b66cdfa555 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ilya Yanoke93a4a52009-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 Yanoke93a4a52009-07-21 19:32:21 +04008 */
9
10#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Jagan Teki484f0212016-12-06 00:00:49 +010012#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060013#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040015#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060016#include <memalign.h>
Jagan Tekic6cd8d52016-12-06 00:00:50 +010017#include <miiphy.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040018#include <net.h>
Jeroen Hofstee120f43f2014-10-08 22:57:40 +020019#include <netdev.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +020023#include <power/regulator.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040024
Ilya Yanoke93a4a52009-07-21 19:32:21 +040025#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090026#include <linux/errno.h>
Marek Vasut4d85b032012-08-26 10:19:20 +000027#include <linux/compiler.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040028
Jagan Tekic6cd8d52016-12-06 00:00:50 +010029#include <asm/arch/clock.h>
30#include <asm/arch/imx-regs.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020031#include <asm/mach-imx/sys_proto.h>
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +020032#include <asm-generic/gpio.h>
33
34#include "fec_mxc.h"
Ye Liad122b72020-05-03 22:41:15 +080035#include <eth_phy.h>
Jagan Tekic6cd8d52016-12-06 00:00:50 +010036
Ilya Yanoke93a4a52009-07-21 19:32:21 +040037DECLARE_GLOBAL_DATA_PTR;
38
Marek Vasut5f1631d2012-08-29 03:49:49 +000039/*
40 * Timeout the transfer after 5 mS. This is usually a bit more, since
41 * the code in the tightloops this timeout is used in adds some overhead.
42 */
43#define FEC_XFER_TIMEOUT 5000
44
Fabio Estevam8b798b22014-08-25 13:34:16 -030045/*
46 * The standard 32-byte DMA alignment does not work on mx6solox, which requires
47 * 64-byte alignment in the DMA RX FEC buffer.
48 * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
49 * satisfies the alignment on other SoCs (32-bytes)
50 */
51#define FEC_DMA_RX_MINALIGN 64
52
Ilya Yanoke93a4a52009-07-21 19:32:21 +040053#ifndef CONFIG_MII
54#error "CONFIG_MII has to be defined!"
55#endif
56
Eric Nelson3d2f7272012-03-15 18:33:25 +000057#ifndef CONFIG_FEC_XCV_TYPE
58#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasutdbb4fce2011-09-11 18:05:33 +000059#endif
60
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000061/*
62 * The i.MX28 operates with packets in big endian. We need to swap them before
63 * sending and after receiving.
64 */
Eric Nelson3d2f7272012-03-15 18:33:25 +000065#ifdef CONFIG_MX28
66#define CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000067#endif
68
Eric Nelson3d2f7272012-03-15 18:33:25 +000069#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
70
71/* Check various alignment issues at compile time */
72#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
73#error "ARCH_DMA_MINALIGN must be multiple of 16!"
74#endif
75
76#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
77 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
78#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
79#endif
80
Ilya Yanoke93a4a52009-07-21 19:32:21 +040081#undef DEBUG
82
Eric Nelson3d2f7272012-03-15 18:33:25 +000083#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000084static void swap_packet(uint32_t *packet, int length)
85{
86 int i;
87
88 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
89 packet[i] = __swab32(packet[i]);
90}
91#endif
92
Jagan Tekic6cd8d52016-12-06 00:00:50 +010093/* MII-interface related functions */
94static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr,
95 uint8_t regaddr)
Ilya Yanoke93a4a52009-07-21 19:32:21 +040096{
Ilya Yanoke93a4a52009-07-21 19:32:21 +040097 uint32_t reg; /* convenient holder for the PHY register */
98 uint32_t phy; /* convenient holder for the PHY */
99 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000100 int val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400101
102 /*
103 * reading from any PHY's register is done by properly
104 * programming the FEC's MII data register.
105 */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000106 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100107 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
108 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400109
110 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000111 phy | reg, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400112
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100113 /* wait for the related interrupt */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000114 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000115 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400116 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
117 printf("Read MDIO failed...\n");
118 return -1;
119 }
120 }
121
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100122 /* clear mii interrupt bit */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000123 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400124
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100125 /* it's now safe to read the PHY's register */
Troy Kisky2000c662012-02-07 14:08:47 +0000126 val = (unsigned short)readl(&eth->mii_data);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100127 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
128 regaddr, val);
Troy Kisky2000c662012-02-07 14:08:47 +0000129 return val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400130}
131
Peng Fandcf5e1b2019-10-25 09:48:02 +0000132#ifndef imx_get_fecclk
133u32 __weak imx_get_fecclk(void)
134{
135 return 0;
136}
137#endif
138
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200139static int fec_get_clk_rate(void *udev, int idx)
140{
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200141 struct fec_priv *fec;
142 struct udevice *dev;
143 int ret;
144
Peng Fandcf5e1b2019-10-25 09:48:02 +0000145 if (IS_ENABLED(CONFIG_IMX8) ||
146 CONFIG_IS_ENABLED(CLK_CCF)) {
147 dev = udev;
148 if (!dev) {
149 ret = uclass_get_device(UCLASS_ETH, idx, &dev);
150 if (ret < 0) {
151 debug("Can't get FEC udev: %d\n", ret);
152 return ret;
153 }
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200154 }
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200155
Peng Fandcf5e1b2019-10-25 09:48:02 +0000156 fec = dev_get_priv(dev);
157 if (fec)
158 return fec->clk_rate;
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200159
Peng Fandcf5e1b2019-10-25 09:48:02 +0000160 return -EINVAL;
161 } else {
162 return imx_get_fecclk();
163 }
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200164}
165
Troy Kisky5e762652012-10-22 16:40:41 +0000166static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic889f2e22010-02-01 14:51:30 +0100167{
168 /*
169 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
170 * and do not drop the Preamble.
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000171 *
172 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
173 * MII_SPEED) register that defines the MDIO output hold time. Earlier
174 * versions are RAZ there, so just ignore the difference and write the
175 * register always.
176 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
177 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
178 * output.
179 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
180 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
181 * holdtime cannot result in a value greater than 3.
Stefano Babic889f2e22010-02-01 14:51:30 +0100182 */
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200183 u32 pclk;
184 u32 speed;
185 u32 hold;
186 int ret;
187
188 ret = fec_get_clk_rate(NULL, 0);
189 if (ret < 0) {
190 printf("Can't find FEC0 clk rate: %d\n", ret);
191 return;
192 }
193 pclk = ret;
194 speed = DIV_ROUND_UP(pclk, 5000000);
195 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
196
Markus Niebel1af82742014-02-05 10:54:11 +0100197#ifdef FEC_QUIRK_ENET_MAC
198 speed--;
199#endif
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000200 writel(speed << 1 | hold << 8, &eth->mii_speed);
Troy Kisky5e762652012-10-22 16:40:41 +0000201 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic889f2e22010-02-01 14:51:30 +0100202}
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400203
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100204static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
205 uint8_t regaddr, uint16_t data)
Troy Kisky2000c662012-02-07 14:08:47 +0000206{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400207 uint32_t reg; /* convenient holder for the PHY register */
208 uint32_t phy; /* convenient holder for the PHY */
209 uint32_t start;
210
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100211 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
212 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400213
214 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000215 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400216
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100217 /* wait for the MII interrupt */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000218 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000219 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400220 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
221 printf("Write MDIO failed...\n");
222 return -1;
223 }
224 }
225
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100226 /* clear MII interrupt bit */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000227 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100228 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
229 regaddr, data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400230
231 return 0;
232}
233
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100234static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
235 int regaddr)
Troy Kisky2000c662012-02-07 14:08:47 +0000236{
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100237 return fec_mdio_read(bus->priv, phyaddr, regaddr);
Troy Kisky2000c662012-02-07 14:08:47 +0000238}
239
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100240static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
241 int regaddr, u16 data)
Troy Kisky2000c662012-02-07 14:08:47 +0000242{
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100243 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
Troy Kisky2000c662012-02-07 14:08:47 +0000244}
245
246#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400247static int miiphy_restart_aneg(struct eth_device *dev)
248{
Stefano Babicd6228172012-02-22 00:24:35 +0000249 int ret = 0;
250#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasutedcd6c02011-09-16 01:13:47 +0200251 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000252 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200253
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400254 /*
255 * Wake up from sleep if necessary
256 * Reset PHY, then delay 300ns
257 */
John Rigbye650e492010-01-25 23:12:55 -0700258#ifdef CONFIG_MX27
Troy Kisky2000c662012-02-07 14:08:47 +0000259 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700260#endif
Troy Kisky2000c662012-02-07 14:08:47 +0000261 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400262 udelay(1000);
263
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100264 /* Set the auto-negotiation advertisement register bits */
Troy Kisky2000c662012-02-07 14:08:47 +0000265 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100266 LPA_100FULL | LPA_100HALF | LPA_10FULL |
267 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky2000c662012-02-07 14:08:47 +0000268 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100269 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut539ecee2011-09-11 18:05:36 +0000270
271 if (fec->mii_postcall)
272 ret = fec->mii_postcall(fec->phy_id);
273
Stefano Babicd6228172012-02-22 00:24:35 +0000274#endif
Marek Vasut539ecee2011-09-11 18:05:36 +0000275 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400276}
277
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200278#ifndef CONFIG_FEC_FIXED_SPEED
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400279static int miiphy_wait_aneg(struct eth_device *dev)
280{
281 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000282 int status;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200283 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000284 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400285
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100286 /* Wait for AN completion */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000287 start = get_timer(0);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400288 do {
289 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
290 printf("%s: Autonegotiation timeout\n", dev->name);
291 return -1;
292 }
293
Troy Kisky2000c662012-02-07 14:08:47 +0000294 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
295 if (status < 0) {
296 printf("%s: Autonegotiation failed. status: %d\n",
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100297 dev->name, status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400298 return -1;
299 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500300 } while (!(status & BMSR_LSTATUS));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400301
302 return 0;
303}
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200304#endif /* CONFIG_FEC_FIXED_SPEED */
Troy Kisky2000c662012-02-07 14:08:47 +0000305#endif
306
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400307static int fec_rx_task_enable(struct fec_priv *fec)
308{
Marek Vasutc1582c02012-08-29 03:49:51 +0000309 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400310 return 0;
311}
312
313static int fec_rx_task_disable(struct fec_priv *fec)
314{
315 return 0;
316}
317
318static int fec_tx_task_enable(struct fec_priv *fec)
319{
Marek Vasutc1582c02012-08-29 03:49:51 +0000320 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400321 return 0;
322}
323
324static int fec_tx_task_disable(struct fec_priv *fec)
325{
326 return 0;
327}
328
329/**
330 * Initialize receive task's buffer descriptors
331 * @param[in] fec all we know about the device yet
332 * @param[in] count receive buffer count to be allocated
Eric Nelson3d2f7272012-03-15 18:33:25 +0000333 * @param[in] dsize desired size of each receive buffer
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400334 * @return 0 on success
335 *
Marek Vasut03880452013-10-12 20:36:25 +0200336 * Init all RX descriptors to default values.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400337 */
Marek Vasut03880452013-10-12 20:36:25 +0200338static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400339{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000340 uint32_t size;
Ye Lie2670912018-01-10 13:20:44 +0800341 ulong data;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000342 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400343
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400344 /*
Marek Vasut03880452013-10-12 20:36:25 +0200345 * Reload the RX descriptors with default values and wipe
346 * the RX buffers.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400347 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000348 size = roundup(dsize, ARCH_DMA_MINALIGN);
349 for (i = 0; i < count; i++) {
Ye Lie2670912018-01-10 13:20:44 +0800350 data = fec->rbd_base[i].data_pointer;
351 memset((void *)data, 0, dsize);
352 flush_dcache_range(data, data + size);
Marek Vasut03880452013-10-12 20:36:25 +0200353
354 fec->rbd_base[i].status = FEC_RBD_EMPTY;
355 fec->rbd_base[i].data_length = 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000356 }
357
358 /* Mark the last RBD to close the ring. */
Marek Vasut03880452013-10-12 20:36:25 +0200359 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400360 fec->rbd_index = 0;
361
Ye Lie2670912018-01-10 13:20:44 +0800362 flush_dcache_range((ulong)fec->rbd_base,
363 (ulong)fec->rbd_base + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400364}
365
366/**
367 * Initialize transmit task's buffer descriptors
368 * @param[in] fec all we know about the device yet
369 *
370 * Transmit buffers are created externally. We only have to init the BDs here.\n
371 * Note: There is a race condition in the hardware. When only one BD is in
372 * use it must be marked with the WRAP bit to use it for every transmitt.
373 * This bit in combination with the READY bit results into double transmit
374 * of each data buffer. It seems the state machine checks READY earlier then
375 * resetting it after the first transfer.
376 * Using two BDs solves this issue.
377 */
378static void fec_tbd_init(struct fec_priv *fec)
379{
Ye Lie2670912018-01-10 13:20:44 +0800380 ulong addr = (ulong)fec->tbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000381 unsigned size = roundup(2 * sizeof(struct fec_bd),
382 ARCH_DMA_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +0200383
384 memset(fec->tbd_base, 0, size);
385 fec->tbd_base[0].status = 0;
386 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400387 fec->tbd_index = 0;
Marek Vasut03880452013-10-12 20:36:25 +0200388 flush_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400389}
390
391/**
392 * Mark the given read buffer descriptor as free
393 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100394 * @param[in] prbd buffer descriptor to mark free again
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400395 */
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100396static void fec_rbd_clean(int last, struct fec_bd *prbd)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400397{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000398 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400399 if (last)
Eric Nelson3d2f7272012-03-15 18:33:25 +0000400 flags |= FEC_RBD_WRAP;
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100401 writew(flags, &prbd->status);
402 writew(0, &prbd->data_length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400403}
404
Jagan Tekibc5fb462016-12-06 00:00:48 +0100405static int fec_get_hwaddr(int dev_id, unsigned char *mac)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400406{
Fabio Estevam04fc1282011-12-20 05:46:31 +0000407 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500408 return !is_valid_ethaddr(mac);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400409}
410
Jagan Teki484f0212016-12-06 00:00:49 +0100411#ifdef CONFIG_DM_ETH
412static int fecmxc_set_hwaddr(struct udevice *dev)
413#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100414static int fec_set_hwaddr(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100415#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400416{
Jagan Teki484f0212016-12-06 00:00:49 +0100417#ifdef CONFIG_DM_ETH
418 struct fec_priv *fec = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700419 struct eth_pdata *pdata = dev_get_plat(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100420 uchar *mac = pdata->enetaddr;
421#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100422 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400423 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100424#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400425
426 writel(0, &fec->eth->iaddr1);
427 writel(0, &fec->eth->iaddr2);
428 writel(0, &fec->eth->gaddr1);
429 writel(0, &fec->eth->gaddr2);
430
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100431 /* Set physical address */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400432 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100433 &fec->eth->paddr1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400434 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
435
436 return 0;
437}
438
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100439/* Do initial configuration of the FEC registers */
Marek Vasut335cbd22012-05-01 11:09:41 +0000440static void fec_reg_setup(struct fec_priv *fec)
441{
442 uint32_t rcntrl;
443
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100444 /* Set interrupt mask register */
Marek Vasut335cbd22012-05-01 11:09:41 +0000445 writel(0x00000000, &fec->eth->imask);
446
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100447 /* Clear FEC-Lite interrupt event register(IEVENT) */
Marek Vasut335cbd22012-05-01 11:09:41 +0000448 writel(0xffffffff, &fec->eth->ievent);
449
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100450 /* Set FEC-Lite receive control register(R_CNTRL): */
Marek Vasut335cbd22012-05-01 11:09:41 +0000451
452 /* Start with frame length = 1518, common for all modes. */
453 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advansacc7a282012-07-19 02:12:46 +0000454 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
455 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
456 if (fec->xcv_type == RGMII)
Marek Vasut335cbd22012-05-01 11:09:41 +0000457 rcntrl |= FEC_RCNTRL_RGMII;
458 else if (fec->xcv_type == RMII)
459 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasut335cbd22012-05-01 11:09:41 +0000460
461 writel(rcntrl, &fec->eth->r_cntrl);
462}
463
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400464/**
465 * Start the FEC engine
466 * @param[in] dev Our device to handle
467 */
Jagan Teki484f0212016-12-06 00:00:49 +0100468#ifdef CONFIG_DM_ETH
469static int fec_open(struct udevice *dev)
470#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400471static int fec_open(struct eth_device *edev)
Jagan Teki484f0212016-12-06 00:00:49 +0100472#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400473{
Jagan Teki484f0212016-12-06 00:00:49 +0100474#ifdef CONFIG_DM_ETH
475 struct fec_priv *fec = dev_get_priv(dev);
476#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400477 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100478#endif
Troy Kisky01112132012-02-07 14:08:46 +0000479 int speed;
Ye Lie2670912018-01-10 13:20:44 +0800480 ulong addr, size;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000481 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400482
483 debug("fec_open: fec_open(dev)\n");
484 /* full-duplex, heartbeat disabled */
485 writel(1 << 2, &fec->eth->x_cntrl);
486 fec->rbd_index = 0;
487
Eric Nelson3d2f7272012-03-15 18:33:25 +0000488 /* Invalidate all descriptors */
489 for (i = 0; i < FEC_RBD_NUM - 1; i++)
490 fec_rbd_clean(0, &fec->rbd_base[i]);
491 fec_rbd_clean(1, &fec->rbd_base[i]);
492
493 /* Flush the descriptors into RAM */
494 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
495 ARCH_DMA_MINALIGN);
Ye Lie2670912018-01-10 13:20:44 +0800496 addr = (ulong)fec->rbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000497 flush_dcache_range(addr, addr + size);
498
Troy Kisky01112132012-02-07 14:08:46 +0000499#ifdef FEC_QUIRK_ENET_MAC
Jason Liubbcef6c2011-12-16 05:17:07 +0000500 /* Enable ENET HW endian SWAP */
501 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100502 &fec->eth->ecntrl);
Jason Liubbcef6c2011-12-16 05:17:07 +0000503 /* Enable ENET store and forward mode */
504 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100505 &fec->eth->x_wmrk);
Jason Liubbcef6c2011-12-16 05:17:07 +0000506#endif
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100507 /* Enable FEC-Lite controller */
John Rigbye650e492010-01-25 23:12:55 -0700508 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100509 &fec->eth->ecntrl);
510
Philippe Schenker7b8ee9b2020-03-11 11:52:58 +0100511#ifdef FEC_ENET_ENABLE_TXC_DELAY
512 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_TXC_DLY,
513 &fec->eth->ecntrl);
514#endif
515
516#ifdef FEC_ENET_ENABLE_RXC_DELAY
517 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RXC_DLY,
518 &fec->eth->ecntrl);
519#endif
520
Fabio Estevam84c1f522013-09-13 00:36:27 -0300521#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby99d5fed2010-01-25 23:12:57 -0700522 udelay(100);
John Rigby99d5fed2010-01-25 23:12:57 -0700523
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100524 /* setup the MII gasket for RMII mode */
John Rigby99d5fed2010-01-25 23:12:57 -0700525 /* disable the gasket */
526 writew(0, &fec->eth->miigsk_enr);
527
528 /* wait for the gasket to be disabled */
529 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
530 udelay(2);
531
532 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
533 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
534
535 /* re-enable the gasket */
536 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
537
538 /* wait until MII gasket is ready */
539 int max_loops = 10;
540 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
541 if (--max_loops <= 0) {
542 printf("WAIT for MII Gasket ready timed out\n");
543 break;
544 }
545 }
546#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400547
Troy Kisky2000c662012-02-07 14:08:47 +0000548#ifdef CONFIG_PHYLIB
Troy Kisky2c42b3c2012-10-22 16:40:45 +0000549 {
Troy Kisky2000c662012-02-07 14:08:47 +0000550 /* Start up the PHY */
Timur Tabi42387462012-07-09 08:52:43 +0000551 int ret = phy_startup(fec->phydev);
552
553 if (ret) {
554 printf("Could not initialize PHY %s\n",
555 fec->phydev->dev->name);
556 return ret;
557 }
Troy Kisky2000c662012-02-07 14:08:47 +0000558 speed = fec->phydev->speed;
Troy Kisky2000c662012-02-07 14:08:47 +0000559 }
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200560#elif CONFIG_FEC_FIXED_SPEED
561 speed = CONFIG_FEC_FIXED_SPEED;
Troy Kisky2000c662012-02-07 14:08:47 +0000562#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400563 miiphy_wait_aneg(edev);
Troy Kisky01112132012-02-07 14:08:46 +0000564 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasutedcd6c02011-09-16 01:13:47 +0200565 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky2000c662012-02-07 14:08:47 +0000566#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400567
Troy Kisky01112132012-02-07 14:08:46 +0000568#ifdef FEC_QUIRK_ENET_MAC
569 {
570 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wang89d932a2013-05-27 22:55:43 +0000571 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky01112132012-02-07 14:08:46 +0000572 if (speed == _1000BASET)
573 ecr |= FEC_ECNTRL_SPEED;
574 else if (speed != _100BASET)
575 rcr |= FEC_RCNTRL_RMII_10T;
576 writel(ecr, &fec->eth->ecntrl);
577 writel(rcr, &fec->eth->r_cntrl);
578 }
579#endif
580 debug("%s:Speed=%i\n", __func__, speed);
581
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100582 /* Enable SmartDMA receive task */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400583 fec_rx_task_enable(fec);
584
585 udelay(100000);
586 return 0;
587}
588
Jagan Teki484f0212016-12-06 00:00:49 +0100589#ifdef CONFIG_DM_ETH
590static int fecmxc_init(struct udevice *dev)
591#else
Masahiro Yamada940a9222020-06-26 15:13:34 +0900592static int fec_init(struct eth_device *dev, struct bd_info *bd)
Jagan Teki484f0212016-12-06 00:00:49 +0100593#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400594{
Jagan Teki484f0212016-12-06 00:00:49 +0100595#ifdef CONFIG_DM_ETH
596 struct fec_priv *fec = dev_get_priv(dev);
597#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400598 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100599#endif
Ye Lie2670912018-01-10 13:20:44 +0800600 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
601 u8 *i;
602 ulong addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400603
John Rigbya4a30552010-10-13 14:31:08 -0600604 /* Initialize MAC address */
Jagan Teki484f0212016-12-06 00:00:49 +0100605#ifdef CONFIG_DM_ETH
606 fecmxc_set_hwaddr(dev);
607#else
John Rigbya4a30552010-10-13 14:31:08 -0600608 fec_set_hwaddr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100609#endif
John Rigbya4a30552010-10-13 14:31:08 -0600610
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100611 /* Setup transmit descriptors, there are two in total. */
Marek Vasut03880452013-10-12 20:36:25 +0200612 fec_tbd_init(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400613
Marek Vasut03880452013-10-12 20:36:25 +0200614 /* Setup receive descriptors. */
615 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400616
Marek Vasut335cbd22012-05-01 11:09:41 +0000617 fec_reg_setup(fec);
Marek Vasutb8f88562011-09-11 18:05:31 +0000618
benoit.thebaudeau@advans551bb362012-07-19 02:12:58 +0000619 if (fec->xcv_type != SEVENWIRE)
Troy Kisky5e762652012-10-22 16:40:41 +0000620 fec_mii_setspeed(fec->bus->priv);
Marek Vasutb8f88562011-09-11 18:05:31 +0000621
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100622 /* Set Opcode/Pause Duration Register */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400623 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
624 writel(0x2, &fec->eth->x_wmrk);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100625
626 /* Set multicast address filter */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400627 writel(0x00000000, &fec->eth->gaddr1);
628 writel(0x00000000, &fec->eth->gaddr2);
629
Peng Fanbf8e58b2018-01-10 13:20:43 +0800630 /* Do not access reserved register */
Peng Fan6146a082019-04-15 05:18:33 +0000631 if (!is_mx6ul() && !is_mx6ull() && !is_imx8() && !is_imx8m()) {
Peng Fan13433fd2015-08-12 17:46:51 +0800632 /* clear MIB RAM */
633 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
634 writel(0, i);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400635
Peng Fan13433fd2015-08-12 17:46:51 +0800636 /* FIFO receive start register */
637 writel(0x520, &fec->eth->r_fstart);
638 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400639
640 /* size and address of each buffer */
641 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
Ye Lie2670912018-01-10 13:20:44 +0800642
643 addr = (ulong)fec->tbd_base;
644 writel((uint32_t)addr, &fec->eth->etdsr);
645
646 addr = (ulong)fec->rbd_base;
647 writel((uint32_t)addr, &fec->eth->erdsr);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400648
Troy Kisky2000c662012-02-07 14:08:47 +0000649#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400650 if (fec->xcv_type != SEVENWIRE)
651 miiphy_restart_aneg(dev);
Troy Kisky2000c662012-02-07 14:08:47 +0000652#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400653 fec_open(dev);
654 return 0;
655}
656
657/**
658 * Halt the FEC engine
659 * @param[in] dev Our device to handle
660 */
Jagan Teki484f0212016-12-06 00:00:49 +0100661#ifdef CONFIG_DM_ETH
662static void fecmxc_halt(struct udevice *dev)
663#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400664static void fec_halt(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100665#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400666{
Jagan Teki484f0212016-12-06 00:00:49 +0100667#ifdef CONFIG_DM_ETH
668 struct fec_priv *fec = dev_get_priv(dev);
669#else
Marek Vasutedcd6c02011-09-16 01:13:47 +0200670 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100671#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400672 int counter = 0xffff;
673
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100674 /* issue graceful stop command to the FEC transmitter if necessary */
John Rigbye650e492010-01-25 23:12:55 -0700675 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100676 &fec->eth->x_cntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400677
678 debug("eth_halt: wait for stop regs\n");
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100679 /* wait for graceful stop to register */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400680 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700681 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400682
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100683 /* Disable SmartDMA tasks */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400684 fec_tx_task_disable(fec);
685 fec_rx_task_disable(fec);
686
687 /*
688 * Disable the Ethernet Controller
689 * Note: this will also reset the BD index counter!
690 */
John Rigby99d5fed2010-01-25 23:12:57 -0700691 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100692 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400693 fec->rbd_index = 0;
694 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400695 debug("eth_halt: done\n");
696}
697
698/**
699 * Transmit one frame
700 * @param[in] dev Our ethernet device to handle
701 * @param[in] packet Pointer to the data to be transmitted
702 * @param[in] length Data count in bytes
703 * @return 0 on success
704 */
Jagan Teki484f0212016-12-06 00:00:49 +0100705#ifdef CONFIG_DM_ETH
706static int fecmxc_send(struct udevice *dev, void *packet, int length)
707#else
Joe Hershberger7c31bd12012-05-21 14:45:27 +0000708static int fec_send(struct eth_device *dev, void *packet, int length)
Jagan Teki484f0212016-12-06 00:00:49 +0100709#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400710{
711 unsigned int status;
Ye Lie2670912018-01-10 13:20:44 +0800712 u32 size;
713 ulong addr, end;
Marek Vasut5f1631d2012-08-29 03:49:49 +0000714 int timeout = FEC_XFER_TIMEOUT;
715 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400716
717 /*
718 * This routine transmits one frame. This routine only accepts
719 * 6-byte Ethernet addresses.
720 */
Jagan Teki484f0212016-12-06 00:00:49 +0100721#ifdef CONFIG_DM_ETH
722 struct fec_priv *fec = dev_get_priv(dev);
723#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400724 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100725#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400726
727 /*
728 * Check for valid length of data.
729 */
730 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100731 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400732 return -1;
733 }
734
735 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000736 * Setup the transmit buffer. We are always using the first buffer for
737 * transmission, the second will be empty and only used to stop the DMA
738 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400739 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000740#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000741 swap_packet((uint32_t *)packet, length);
742#endif
Eric Nelson3d2f7272012-03-15 18:33:25 +0000743
Ye Lie2670912018-01-10 13:20:44 +0800744 addr = (ulong)packet;
Marek Vasut4325d242012-08-26 10:19:21 +0000745 end = roundup(addr + length, ARCH_DMA_MINALIGN);
746 addr &= ~(ARCH_DMA_MINALIGN - 1);
747 flush_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000748
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400749 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Ye Lie2670912018-01-10 13:20:44 +0800750 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000751
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400752 /*
753 * update BD's status now
754 * This block:
755 * - is always the last in a chain (means no chain)
756 * - should transmitt the CRC
757 * - might be the last BD in the list, so the address counter should
758 * wrap (-> keep the WRAP flag)
759 */
760 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
761 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
762 writew(status, &fec->tbd_base[fec->tbd_index].status);
763
764 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000765 * Flush data cache. This code flushes both TX descriptors to RAM.
766 * After this code, the descriptors will be safely in RAM and we
767 * can start DMA.
768 */
769 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
Ye Lie2670912018-01-10 13:20:44 +0800770 addr = (ulong)fec->tbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000771 flush_dcache_range(addr, addr + size);
772
773 /*
Marek Vasutd521b3c2013-07-12 01:03:04 +0200774 * Below we read the DMA descriptor's last four bytes back from the
775 * DRAM. This is important in order to make sure that all WRITE
776 * operations on the bus that were triggered by previous cache FLUSH
777 * have completed.
778 *
779 * Otherwise, on MX28, it is possible to observe a corruption of the
780 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
781 * for the bus structure of MX28. The scenario is as follows:
782 *
783 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
784 * to DRAM due to flush_dcache_range()
785 * 2) ARM core writes the FEC registers via AHB_ARB2
786 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
787 *
788 * Note that 2) does sometimes finish before 1) due to reordering of
789 * WRITE accesses on the AHB bus, therefore triggering 3) before the
790 * DMA descriptor is fully written into DRAM. This results in occasional
791 * corruption of the DMA descriptor.
792 */
793 readl(addr + size - 4);
794
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100795 /* Enable SmartDMA transmit task */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400796 fec_tx_task_enable(fec);
797
798 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000799 * Wait until frame is sent. On each turn of the wait cycle, we must
800 * invalidate data cache to see what's really in RAM. Also, we need
801 * barrier here.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400802 */
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000803 while (--timeout) {
Marek Vasutc1582c02012-08-29 03:49:51 +0000804 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasut5f1631d2012-08-29 03:49:49 +0000805 break;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400806 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000807
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300808 if (!timeout) {
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000809 ret = -EINVAL;
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300810 goto out;
811 }
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000812
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300813 /*
814 * The TDAR bit is cleared when the descriptors are all out from TX
815 * but on mx6solox we noticed that the READY bit is still not cleared
816 * right after TDAR.
817 * These are two distinct signals, and in IC simulation, we found that
818 * TDAR always gets cleared prior than the READY bit of last BD becomes
819 * cleared.
820 * In mx6solox, we use a later version of FEC IP. It looks like that
821 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
822 * version.
823 *
824 * Fix this by polling the READY bit of BD after the TDAR polling,
825 * which covers the mx6solox case and does not harm the other SoCs.
826 */
827 timeout = FEC_XFER_TIMEOUT;
828 while (--timeout) {
829 invalidate_dcache_range(addr, addr + size);
830 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
831 FEC_TBD_READY))
832 break;
833 }
834
835 if (!timeout)
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000836 ret = -EINVAL;
837
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300838out:
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000839 debug("fec_send: status 0x%x index %d ret %i\n",
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100840 readw(&fec->tbd_base[fec->tbd_index].status),
841 fec->tbd_index, ret);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400842 /* for next transmission use the other buffer */
843 if (fec->tbd_index)
844 fec->tbd_index = 0;
845 else
846 fec->tbd_index = 1;
847
Marek Vasut5f1631d2012-08-29 03:49:49 +0000848 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400849}
850
851/**
852 * Pull one frame from the card
853 * @param[in] dev Our ethernet device to handle
854 * @return Length of packet read
855 */
Jagan Teki484f0212016-12-06 00:00:49 +0100856#ifdef CONFIG_DM_ETH
857static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
858#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400859static int fec_recv(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100860#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400861{
Jagan Teki484f0212016-12-06 00:00:49 +0100862#ifdef CONFIG_DM_ETH
863 struct fec_priv *fec = dev_get_priv(dev);
864#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400865 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100866#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400867 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
868 unsigned long ievent;
869 int frame_length, len = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400870 uint16_t bd_status;
Ye Lie2670912018-01-10 13:20:44 +0800871 ulong addr, size, end;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000872 int i;
Ye Libd7e5382018-03-28 20:54:11 +0800873
874#ifdef CONFIG_DM_ETH
875 *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
876 if (*packetp == 0) {
877 printf("%s: error allocating packetp\n", __func__);
878 return -ENOMEM;
879 }
880#else
Fabio Estevamcc956082013-09-17 23:13:10 -0300881 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ye Libd7e5382018-03-28 20:54:11 +0800882#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400883
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100884 /* Check if any critical events have happened */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400885 ievent = readl(&fec->eth->ievent);
886 writel(ievent, &fec->eth->ievent);
Marek Vasut478e2d02011-10-24 23:40:03 +0000887 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400888 if (ievent & FEC_IEVENT_BABR) {
Jagan Teki484f0212016-12-06 00:00:49 +0100889#ifdef CONFIG_DM_ETH
890 fecmxc_halt(dev);
891 fecmxc_init(dev);
892#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400893 fec_halt(dev);
894 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100895#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400896 printf("some error: 0x%08lx\n", ievent);
897 return 0;
898 }
899 if (ievent & FEC_IEVENT_HBERR) {
900 /* Heartbeat error */
901 writel(0x00000001 | readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100902 &fec->eth->x_cntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400903 }
904 if (ievent & FEC_IEVENT_GRA) {
905 /* Graceful stop complete */
906 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
Jagan Teki484f0212016-12-06 00:00:49 +0100907#ifdef CONFIG_DM_ETH
908 fecmxc_halt(dev);
909#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400910 fec_halt(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100911#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400912 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100913 &fec->eth->x_cntrl);
Jagan Teki484f0212016-12-06 00:00:49 +0100914#ifdef CONFIG_DM_ETH
915 fecmxc_init(dev);
916#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400917 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100918#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400919 }
920 }
921
922 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000923 * Read the buffer status. Before the status can be read, the data cache
924 * must be invalidated, because the data in RAM might have been changed
925 * by DMA. The descriptors are properly aligned to cachelines so there's
926 * no need to worry they'd overlap.
927 *
928 * WARNING: By invalidating the descriptor here, we also invalidate
929 * the descriptors surrounding this one. Therefore we can NOT change the
930 * contents of this descriptor nor the surrounding ones. The problem is
931 * that in order to mark the descriptor as processed, we need to change
932 * the descriptor. The solution is to mark the whole cache line when all
933 * descriptors in the cache line are processed.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400934 */
Ye Lie2670912018-01-10 13:20:44 +0800935 addr = (ulong)rbd;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000936 addr &= ~(ARCH_DMA_MINALIGN - 1);
937 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
938 invalidate_dcache_range(addr, addr + size);
939
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400940 bd_status = readw(&rbd->status);
941 debug("fec_recv: status 0x%x\n", bd_status);
942
943 if (!(bd_status & FEC_RBD_EMPTY)) {
944 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100945 ((readw(&rbd->data_length) - 4) > 14)) {
946 /* Get buffer address and size */
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200947 addr = readl(&rbd->data_pointer);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400948 frame_length = readw(&rbd->data_length) - 4;
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100949 /* Invalidate data cache over the buffer */
Marek Vasut4325d242012-08-26 10:19:21 +0000950 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
951 addr &= ~(ARCH_DMA_MINALIGN - 1);
952 invalidate_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000953
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100954 /* Fill the buffer and pass it to upper layers */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000955#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200956 swap_packet((uint32_t *)addr, frame_length);
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000957#endif
Ye Libd7e5382018-03-28 20:54:11 +0800958
959#ifdef CONFIG_DM_ETH
960 memcpy(*packetp, (char *)addr, frame_length);
961#else
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200962 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500963 net_process_received_packet(buff, frame_length);
Ye Libd7e5382018-03-28 20:54:11 +0800964#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400965 len = frame_length;
966 } else {
967 if (bd_status & FEC_RBD_ERR)
Ye Lie2670912018-01-10 13:20:44 +0800968 debug("error frame: 0x%08lx 0x%08x\n",
969 addr, bd_status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400970 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000971
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400972 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000973 * Free the current buffer, restart the engine and move forward
974 * to the next buffer. Here we check if the whole cacheline of
975 * descriptors was already processed and if so, we mark it free
976 * as whole.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400977 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000978 size = RXDESC_PER_CACHELINE - 1;
979 if ((fec->rbd_index & size) == size) {
980 i = fec->rbd_index - size;
Ye Lie2670912018-01-10 13:20:44 +0800981 addr = (ulong)&fec->rbd_base[i];
Eric Nelson3d2f7272012-03-15 18:33:25 +0000982 for (; i <= fec->rbd_index ; i++) {
983 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
984 &fec->rbd_base[i]);
985 }
986 flush_dcache_range(addr,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100987 addr + ARCH_DMA_MINALIGN);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000988 }
989
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400990 fec_rx_task_enable(fec);
991 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
992 }
993 debug("fec_recv: stop\n");
994
995 return len;
996}
997
Troy Kisky4c2ddec2012-10-22 16:40:44 +0000998static void fec_set_dev_name(char *dest, int dev_id)
999{
1000 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
1001}
1002
Marek Vasut03880452013-10-12 20:36:25 +02001003static int fec_alloc_descs(struct fec_priv *fec)
1004{
1005 unsigned int size;
1006 int i;
1007 uint8_t *data;
Ye Lie2670912018-01-10 13:20:44 +08001008 ulong addr;
Marek Vasut03880452013-10-12 20:36:25 +02001009
1010 /* Allocate TX descriptors. */
1011 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
1012 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
1013 if (!fec->tbd_base)
1014 goto err_tx;
1015
1016 /* Allocate RX descriptors. */
1017 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
1018 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
1019 if (!fec->rbd_base)
1020 goto err_rx;
1021
1022 memset(fec->rbd_base, 0, size);
1023
1024 /* Allocate RX buffers. */
1025
1026 /* Maximum RX buffer size. */
Fabio Estevam8b798b22014-08-25 13:34:16 -03001027 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +02001028 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevam8b798b22014-08-25 13:34:16 -03001029 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut03880452013-10-12 20:36:25 +02001030 if (!data) {
1031 printf("%s: error allocating rxbuf %d\n", __func__, i);
1032 goto err_ring;
1033 }
1034
1035 memset(data, 0, size);
1036
Ye Lie2670912018-01-10 13:20:44 +08001037 addr = (ulong)data;
1038 fec->rbd_base[i].data_pointer = (uint32_t)addr;
Marek Vasut03880452013-10-12 20:36:25 +02001039 fec->rbd_base[i].status = FEC_RBD_EMPTY;
1040 fec->rbd_base[i].data_length = 0;
1041 /* Flush the buffer to memory. */
Ye Lie2670912018-01-10 13:20:44 +08001042 flush_dcache_range(addr, addr + size);
Marek Vasut03880452013-10-12 20:36:25 +02001043 }
1044
1045 /* Mark the last RBD to close the ring. */
1046 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1047
1048 fec->rbd_index = 0;
1049 fec->tbd_index = 0;
1050
1051 return 0;
1052
1053err_ring:
Ye Lie2670912018-01-10 13:20:44 +08001054 for (; i >= 0; i--) {
1055 addr = fec->rbd_base[i].data_pointer;
1056 free((void *)addr);
1057 }
Marek Vasut03880452013-10-12 20:36:25 +02001058 free(fec->rbd_base);
1059err_rx:
1060 free(fec->tbd_base);
1061err_tx:
1062 return -ENOMEM;
1063}
1064
1065static void fec_free_descs(struct fec_priv *fec)
1066{
1067 int i;
Ye Lie2670912018-01-10 13:20:44 +08001068 ulong addr;
Marek Vasut03880452013-10-12 20:36:25 +02001069
Ye Lie2670912018-01-10 13:20:44 +08001070 for (i = 0; i < FEC_RBD_NUM; i++) {
1071 addr = fec->rbd_base[i].data_pointer;
1072 free((void *)addr);
1073 }
Marek Vasut03880452013-10-12 20:36:25 +02001074 free(fec->rbd_base);
1075 free(fec->tbd_base);
1076}
1077
Peng Fan0c59c4f2018-03-28 20:54:12 +08001078struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
Jagan Teki484f0212016-12-06 00:00:49 +01001079{
Peng Fan0c59c4f2018-03-28 20:54:12 +08001080 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
Jagan Teki484f0212016-12-06 00:00:49 +01001081 struct mii_dev *bus;
1082 int ret;
1083
1084 bus = mdio_alloc();
1085 if (!bus) {
1086 printf("mdio_alloc failed\n");
1087 return NULL;
1088 }
1089 bus->read = fec_phy_read;
1090 bus->write = fec_phy_write;
1091 bus->priv = eth;
1092 fec_set_dev_name(bus->name, dev_id);
1093
1094 ret = mdio_register(bus);
1095 if (ret) {
1096 printf("mdio_register failed\n");
1097 free(bus);
1098 return NULL;
1099 }
1100 fec_mii_setspeed(eth);
1101 return bus;
1102}
1103
1104#ifndef CONFIG_DM_ETH
Troy Kiskydce4def2012-10-22 16:40:46 +00001105#ifdef CONFIG_PHYLIB
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001106int fec_probe(struct bd_info *bd, int dev_id, uint32_t base_addr,
Troy Kiskydce4def2012-10-22 16:40:46 +00001107 struct mii_dev *bus, struct phy_device *phydev)
1108#else
Masahiro Yamada940a9222020-06-26 15:13:34 +09001109static int fec_probe(struct bd_info *bd, int dev_id, uint32_t base_addr,
Troy Kiskydce4def2012-10-22 16:40:46 +00001110 struct mii_dev *bus, int phy_id)
1111#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001112{
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001113 struct eth_device *edev;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001114 struct fec_priv *fec;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001115 unsigned char ethaddr[6];
Andy Duan8f8e4582017-04-10 19:44:35 +08001116 char mac[16];
Marek Vasut43b10302011-09-11 18:05:37 +00001117 uint32_t start;
1118 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001119
1120 /* create and fill edev struct */
1121 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1122 if (!edev) {
Marek Vasutedcd6c02011-09-16 01:13:47 +02001123 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001124 ret = -ENOMEM;
1125 goto err1;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001126 }
1127
1128 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1129 if (!fec) {
1130 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001131 ret = -ENOMEM;
1132 goto err2;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001133 }
Marek Vasutedcd6c02011-09-16 01:13:47 +02001134
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +09001135 memset(edev, 0, sizeof(*edev));
Marek Vasutedcd6c02011-09-16 01:13:47 +02001136 memset(fec, 0, sizeof(*fec));
1137
Marek Vasut03880452013-10-12 20:36:25 +02001138 ret = fec_alloc_descs(fec);
1139 if (ret)
1140 goto err3;
1141
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001142 edev->priv = fec;
1143 edev->init = fec_init;
1144 edev->send = fec_send;
1145 edev->recv = fec_recv;
1146 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +02001147 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001148
Ye Lie2670912018-01-10 13:20:44 +08001149 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001150 fec->bd = bd;
1151
Marek Vasutdbb4fce2011-09-11 18:05:33 +00001152 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001153
1154 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -07001155 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasut43b10302011-09-11 18:05:37 +00001156 start = get_timer(0);
1157 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1158 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -07001159 printf("FEC MXC: Timeout resetting chip\n");
Marek Vasut03880452013-10-12 20:36:25 +02001160 goto err4;
Marek Vasut43b10302011-09-11 18:05:37 +00001161 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001162 udelay(10);
Marek Vasut43b10302011-09-11 18:05:37 +00001163 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001164
Marek Vasut335cbd22012-05-01 11:09:41 +00001165 fec_reg_setup(fec);
Troy Kisky4c2ddec2012-10-22 16:40:44 +00001166 fec_set_dev_name(edev->name, dev_id);
1167 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001168 fec->bus = bus;
1169 fec_mii_setspeed(bus->priv);
1170#ifdef CONFIG_PHYLIB
1171 fec->phydev = phydev;
1172 phy_connect_dev(phydev, edev);
1173 /* Configure phy */
1174 phy_config(phydev);
1175#else
Marek Vasutedcd6c02011-09-16 01:13:47 +02001176 fec->phy_id = phy_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001177#endif
1178 eth_register(edev);
Andy Duan8f8e4582017-04-10 19:44:35 +08001179 /* only support one eth device, the index number pointed by dev_id */
1180 edev->index = fec->dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001181
Andy Duan0eaaf832017-04-10 19:44:34 +08001182 if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1183 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001184 memcpy(edev->enetaddr, ethaddr, 6);
Andy Duan8f8e4582017-04-10 19:44:35 +08001185 if (fec->dev_id)
1186 sprintf(mac, "eth%daddr", fec->dev_id);
1187 else
1188 strcpy(mac, "ethaddr");
Simon Glass64b723f2017-08-03 12:22:12 -06001189 if (!env_get(mac))
Simon Glass8551d552017-08-03 12:22:11 -06001190 eth_env_set_enetaddr(mac, ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001191 }
1192 return ret;
Marek Vasut03880452013-10-12 20:36:25 +02001193err4:
1194 fec_free_descs(fec);
Troy Kiskydce4def2012-10-22 16:40:46 +00001195err3:
1196 free(fec);
1197err2:
1198 free(edev);
1199err1:
1200 return ret;
1201}
1202
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001203int fecmxc_initialize_multi(struct bd_info *bd, int dev_id, int phy_id,
1204 uint32_t addr)
Troy Kiskydce4def2012-10-22 16:40:46 +00001205{
1206 uint32_t base_mii;
1207 struct mii_dev *bus = NULL;
1208#ifdef CONFIG_PHYLIB
1209 struct phy_device *phydev = NULL;
1210#endif
1211 int ret;
1212
Peng Fan075497c2020-05-01 22:08:37 +08001213 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
1214 if (enet_fused((ulong)addr)) {
1215 printf("SoC fuse indicates Ethernet@0x%x is unavailable.\n", addr);
1216 return -ENODEV;
1217 }
1218 }
1219
Peng Fana65e0362018-03-28 20:54:14 +08001220#ifdef CONFIG_FEC_MXC_MDIO_BASE
Troy Kisky2000c662012-02-07 14:08:47 +00001221 /*
1222 * The i.MX28 has two ethernet interfaces, but they are not equal.
1223 * Only the first one can access the MDIO bus.
1224 */
Peng Fana65e0362018-03-28 20:54:14 +08001225 base_mii = CONFIG_FEC_MXC_MDIO_BASE;
Troy Kisky2000c662012-02-07 14:08:47 +00001226#else
Troy Kiskydce4def2012-10-22 16:40:46 +00001227 base_mii = addr;
Troy Kisky2000c662012-02-07 14:08:47 +00001228#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001229 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1230 bus = fec_get_miibus(base_mii, dev_id);
1231 if (!bus)
1232 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001233#ifdef CONFIG_PHYLIB
Troy Kiskydce4def2012-10-22 16:40:46 +00001234 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001235 if (!phydev) {
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001236 mdio_unregister(bus);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001237 free(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001238 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001239 }
Troy Kiskydce4def2012-10-22 16:40:46 +00001240 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1241#else
1242 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001243#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001244 if (ret) {
1245#ifdef CONFIG_PHYLIB
1246 free(phydev);
1247#endif
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001248 mdio_unregister(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001249 free(bus);
1250 }
Marek Vasut43b10302011-09-11 18:05:37 +00001251 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001252}
1253
Troy Kisky4e0eae62012-10-22 16:40:42 +00001254#ifdef CONFIG_FEC_MXC_PHYADDR
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +09001255int fecmxc_initialize(struct bd_info *bd)
Troy Kisky4e0eae62012-10-22 16:40:42 +00001256{
1257 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1258 IMX_FEC_BASE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001259}
Troy Kisky4e0eae62012-10-22 16:40:42 +00001260#endif
Marek Vasut539ecee2011-09-11 18:05:36 +00001261
Troy Kisky2000c662012-02-07 14:08:47 +00001262#ifndef CONFIG_PHYLIB
Marek Vasut539ecee2011-09-11 18:05:36 +00001263int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1264{
1265 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1266 fec->mii_postcall = cb;
1267 return 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001268}
1269#endif
1270
1271#else
1272
Jagan Teki87e7f352016-12-06 00:00:51 +01001273static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1274{
1275 struct fec_priv *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -07001276 struct eth_pdata *pdata = dev_get_plat(dev);
Jagan Teki87e7f352016-12-06 00:00:51 +01001277
1278 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1279}
1280
Ye Libd7e5382018-03-28 20:54:11 +08001281static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
1282{
1283 if (packet)
1284 free(packet);
1285
1286 return 0;
1287}
1288
Jagan Teki484f0212016-12-06 00:00:49 +01001289static const struct eth_ops fecmxc_ops = {
1290 .start = fecmxc_init,
1291 .send = fecmxc_send,
1292 .recv = fecmxc_recv,
Ye Libd7e5382018-03-28 20:54:11 +08001293 .free_pkt = fecmxc_free_pkt,
Jagan Teki484f0212016-12-06 00:00:49 +01001294 .stop = fecmxc_halt,
1295 .write_hwaddr = fecmxc_set_hwaddr,
Jagan Teki87e7f352016-12-06 00:00:51 +01001296 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
Jagan Teki484f0212016-12-06 00:00:49 +01001297};
1298
Fabio Estevamc9eb5202020-06-18 20:21:18 -03001299static int device_get_phy_addr(struct fec_priv *priv, struct udevice *dev)
Martyn Welchd1ac23f2018-12-11 11:34:45 +00001300{
1301 struct ofnode_phandle_args phandle_args;
1302 int reg;
1303
1304 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1305 &phandle_args)) {
1306 debug("Failed to find phy-handle");
1307 return -ENODEV;
1308 }
1309
Fabio Estevamc9eb5202020-06-18 20:21:18 -03001310 priv->phy_of_node = phandle_args.node;
1311
Martyn Welchd1ac23f2018-12-11 11:34:45 +00001312 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
1313
1314 return reg;
1315}
1316
Jagan Teki484f0212016-12-06 00:00:49 +01001317static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1318{
1319 struct phy_device *phydev;
Martyn Welchd1ac23f2018-12-11 11:34:45 +00001320 int addr;
Jagan Teki484f0212016-12-06 00:00:49 +01001321
Fabio Estevamc9eb5202020-06-18 20:21:18 -03001322 addr = device_get_phy_addr(priv, dev);
Lukasz Majewski07b75a32018-04-15 21:45:54 +02001323#ifdef CONFIG_FEC_MXC_PHYADDR
Hannes Schmelzerf7694302019-02-15 10:30:18 +01001324 addr = CONFIG_FEC_MXC_PHYADDR;
Jagan Teki484f0212016-12-06 00:00:49 +01001325#endif
1326
Hannes Schmelzerf7694302019-02-15 10:30:18 +01001327 phydev = phy_connect(priv->bus, addr, dev, priv->interface);
Jagan Teki484f0212016-12-06 00:00:49 +01001328 if (!phydev)
1329 return -ENODEV;
1330
Jagan Teki484f0212016-12-06 00:00:49 +01001331 priv->phydev = phydev;
Fabio Estevamc9eb5202020-06-18 20:21:18 -03001332 priv->phydev->node = priv->phy_of_node;
Jagan Teki484f0212016-12-06 00:00:49 +01001333 phy_config(phydev);
1334
1335 return 0;
1336}
1337
Simon Glassfa4689a2019-12-06 21:41:35 -07001338#if CONFIG_IS_ENABLED(DM_GPIO)
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001339/* FEC GPIO reset */
1340static void fec_gpio_reset(struct fec_priv *priv)
1341{
1342 debug("fec_gpio_reset: fec_gpio_reset(dev)\n");
1343 if (dm_gpio_is_valid(&priv->phy_reset_gpio)) {
1344 dm_gpio_set_value(&priv->phy_reset_gpio, 1);
Martin Fuzzey9c3f97a2018-10-04 19:59:18 +02001345 mdelay(priv->reset_delay);
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001346 dm_gpio_set_value(&priv->phy_reset_gpio, 0);
Andrejs Cainikovs24b6aac2019-03-01 13:27:59 +00001347 if (priv->reset_post_delay)
1348 mdelay(priv->reset_post_delay);
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001349 }
1350}
1351#endif
1352
Jagan Teki484f0212016-12-06 00:00:49 +01001353static int fecmxc_probe(struct udevice *dev)
1354{
Simon Glassfa20e932020-12-03 16:55:20 -07001355 struct eth_pdata *pdata = dev_get_plat(dev);
Jagan Teki484f0212016-12-06 00:00:49 +01001356 struct fec_priv *priv = dev_get_priv(dev);
1357 struct mii_dev *bus = NULL;
Jagan Teki484f0212016-12-06 00:00:49 +01001358 uint32_t start;
1359 int ret;
1360
Peng Fan075497c2020-05-01 22:08:37 +08001361 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
1362 if (enet_fused((ulong)priv->eth)) {
1363 printf("SoC fuse indicates Ethernet@0x%lx is unavailable.\n", (ulong)priv->eth);
1364 return -ENODEV;
1365 }
1366 }
1367
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +02001368 if (IS_ENABLED(CONFIG_IMX8)) {
1369 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1370 if (ret < 0) {
1371 debug("Can't get FEC ipg clk: %d\n", ret);
1372 return ret;
1373 }
1374 ret = clk_enable(&priv->ipg_clk);
1375 if (ret < 0) {
1376 debug("Can't enable FEC ipg clk: %d\n", ret);
1377 return ret;
1378 }
1379
1380 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
Peng Fandcf5e1b2019-10-25 09:48:02 +00001381 } else if (CONFIG_IS_ENABLED(CLK_CCF)) {
1382 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1383 if (ret < 0) {
1384 debug("Can't get FEC ipg clk: %d\n", ret);
1385 return ret;
1386 }
1387 ret = clk_enable(&priv->ipg_clk);
1388 if(ret)
1389 return ret;
1390
1391 ret = clk_get_by_name(dev, "ahb", &priv->ahb_clk);
1392 if (ret < 0) {
1393 debug("Can't get FEC ahb clk: %d\n", ret);
1394 return ret;
1395 }
1396 ret = clk_enable(&priv->ahb_clk);
1397 if (ret)
1398 return ret;
1399
1400 ret = clk_get_by_name(dev, "enet_out", &priv->clk_enet_out);
1401 if (!ret) {
1402 ret = clk_enable(&priv->clk_enet_out);
1403 if (ret)
1404 return ret;
1405 }
1406
1407 ret = clk_get_by_name(dev, "enet_clk_ref", &priv->clk_ref);
1408 if (!ret) {
1409 ret = clk_enable(&priv->clk_ref);
1410 if (ret)
1411 return ret;
1412 }
1413
1414 ret = clk_get_by_name(dev, "ptp", &priv->clk_ptp);
1415 if (!ret) {
1416 ret = clk_enable(&priv->clk_ptp);
1417 if (ret)
1418 return ret;
1419 }
1420
1421 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +02001422 }
1423
Jagan Teki484f0212016-12-06 00:00:49 +01001424 ret = fec_alloc_descs(priv);
1425 if (ret)
1426 return ret;
1427
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001428#ifdef CONFIG_DM_REGULATOR
1429 if (priv->phy_supply) {
Adam Fordb3301b62019-01-15 11:26:48 -06001430 ret = regulator_set_enable(priv->phy_supply, true);
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001431 if (ret) {
1432 printf("%s: Error enabling phy supply\n", dev->name);
1433 return ret;
1434 }
1435 }
1436#endif
1437
Simon Glassfa4689a2019-12-06 21:41:35 -07001438#if CONFIG_IS_ENABLED(DM_GPIO)
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001439 fec_gpio_reset(priv);
1440#endif
Jagan Teki484f0212016-12-06 00:00:49 +01001441 /* Reset chip. */
Jagan Tekic6cd8d52016-12-06 00:00:50 +01001442 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1443 &priv->eth->ecntrl);
Jagan Teki484f0212016-12-06 00:00:49 +01001444 start = get_timer(0);
1445 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1446 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1447 printf("FEC MXC: Timeout reseting chip\n");
1448 goto err_timeout;
1449 }
1450 udelay(10);
1451 }
1452
1453 fec_reg_setup(priv);
Jagan Teki484f0212016-12-06 00:00:49 +01001454
Simon Glass75e534b2020-12-16 21:20:07 -07001455 priv->dev_id = dev_seq(dev);
Ye Liad122b72020-05-03 22:41:15 +08001456
1457#ifdef CONFIG_DM_ETH_PHY
1458 bus = eth_phy_get_mdio_bus(dev);
1459#endif
1460
1461 if (!bus) {
Peng Fana65e0362018-03-28 20:54:14 +08001462#ifdef CONFIG_FEC_MXC_MDIO_BASE
Simon Glass75e534b2020-12-16 21:20:07 -07001463 bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE,
1464 dev_seq(dev));
Peng Fana65e0362018-03-28 20:54:14 +08001465#else
Simon Glass75e534b2020-12-16 21:20:07 -07001466 bus = fec_get_miibus((ulong)priv->eth, dev_seq(dev));
Peng Fana65e0362018-03-28 20:54:14 +08001467#endif
Ye Liad122b72020-05-03 22:41:15 +08001468 }
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001469 if (!bus) {
1470 ret = -ENOMEM;
1471 goto err_mii;
1472 }
1473
Ye Liad122b72020-05-03 22:41:15 +08001474#ifdef CONFIG_DM_ETH_PHY
1475 eth_phy_set_mdio_bus(dev, bus);
1476#endif
1477
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001478 priv->bus = bus;
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001479 priv->interface = pdata->phy_interface;
Martin Fuzzeyf08eb3d2018-10-04 19:59:21 +02001480 switch (priv->interface) {
1481 case PHY_INTERFACE_MODE_MII:
1482 priv->xcv_type = MII100;
1483 break;
1484 case PHY_INTERFACE_MODE_RMII:
1485 priv->xcv_type = RMII;
1486 break;
1487 case PHY_INTERFACE_MODE_RGMII:
1488 case PHY_INTERFACE_MODE_RGMII_ID:
1489 case PHY_INTERFACE_MODE_RGMII_RXID:
1490 case PHY_INTERFACE_MODE_RGMII_TXID:
1491 priv->xcv_type = RGMII;
1492 break;
1493 default:
1494 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1495 printf("Unsupported interface type %d defaulting to %d\n",
1496 priv->interface, priv->xcv_type);
1497 break;
1498 }
1499
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001500 ret = fec_phy_init(priv, dev);
1501 if (ret)
1502 goto err_phy;
1503
Jagan Teki484f0212016-12-06 00:00:49 +01001504 return 0;
1505
Jagan Teki484f0212016-12-06 00:00:49 +01001506err_phy:
1507 mdio_unregister(bus);
1508 free(bus);
1509err_mii:
Ye Li5fa556c2018-03-28 20:54:16 +08001510err_timeout:
Jagan Teki484f0212016-12-06 00:00:49 +01001511 fec_free_descs(priv);
1512 return ret;
Marek Vasut539ecee2011-09-11 18:05:36 +00001513}
Jagan Teki484f0212016-12-06 00:00:49 +01001514
1515static int fecmxc_remove(struct udevice *dev)
1516{
1517 struct fec_priv *priv = dev_get_priv(dev);
1518
1519 free(priv->phydev);
1520 fec_free_descs(priv);
1521 mdio_unregister(priv->bus);
1522 mdio_free(priv->bus);
1523
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001524#ifdef CONFIG_DM_REGULATOR
1525 if (priv->phy_supply)
1526 regulator_set_enable(priv->phy_supply, false);
1527#endif
1528
Jagan Teki484f0212016-12-06 00:00:49 +01001529 return 0;
1530}
1531
Simon Glassaad29ae2020-12-03 16:55:21 -07001532static int fecmxc_of_to_plat(struct udevice *dev)
Jagan Teki484f0212016-12-06 00:00:49 +01001533{
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001534 int ret = 0;
Simon Glassfa20e932020-12-03 16:55:20 -07001535 struct eth_pdata *pdata = dev_get_plat(dev);
Jagan Teki484f0212016-12-06 00:00:49 +01001536 struct fec_priv *priv = dev_get_priv(dev);
1537 const char *phy_mode;
1538
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001539 pdata->iobase = dev_read_addr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +01001540 priv->eth = (struct ethernet_regs *)pdata->iobase;
1541
1542 pdata->phy_interface = -1;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001543 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1544 NULL);
Jagan Teki484f0212016-12-06 00:00:49 +01001545 if (phy_mode)
1546 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1547 if (pdata->phy_interface == -1) {
1548 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1549 return -EINVAL;
1550 }
1551
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001552#ifdef CONFIG_DM_REGULATOR
1553 device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
1554#endif
1555
Simon Glassfa4689a2019-12-06 21:41:35 -07001556#if CONFIG_IS_ENABLED(DM_GPIO)
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001557 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001558 &priv->phy_reset_gpio, GPIOD_IS_OUT);
1559 if (ret < 0)
1560 return 0; /* property is optional, don't return error! */
Jagan Teki484f0212016-12-06 00:00:49 +01001561
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001562 priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1);
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001563 if (priv->reset_delay > 1000) {
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001564 printf("FEC MXC: phy reset duration should be <= 1000ms\n");
1565 /* property value wrong, use default value */
1566 priv->reset_delay = 1;
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001567 }
Andrejs Cainikovs24b6aac2019-03-01 13:27:59 +00001568
1569 priv->reset_post_delay = dev_read_u32_default(dev,
1570 "phy-reset-post-delay",
1571 0);
1572 if (priv->reset_post_delay > 1000) {
1573 printf("FEC MXC: phy reset post delay should be <= 1000ms\n");
1574 /* property value wrong, use default value */
1575 priv->reset_post_delay = 0;
1576 }
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001577#endif
1578
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001579 return 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001580}
1581
1582static const struct udevice_id fecmxc_ids[] = {
Lukasz Majewski8a8f5a62019-06-19 17:31:03 +02001583 { .compatible = "fsl,imx28-fec" },
Jagan Teki484f0212016-12-06 00:00:49 +01001584 { .compatible = "fsl,imx6q-fec" },
Peng Fan56406302018-03-28 20:54:15 +08001585 { .compatible = "fsl,imx6sl-fec" },
1586 { .compatible = "fsl,imx6sx-fec" },
1587 { .compatible = "fsl,imx6ul-fec" },
Lukasz Majewski47311222018-04-15 21:54:22 +02001588 { .compatible = "fsl,imx53-fec" },
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +02001589 { .compatible = "fsl,imx7d-fec" },
Lukasz Majewski6b94b0e2019-02-13 22:46:38 +01001590 { .compatible = "fsl,mvf600-fec" },
Jagan Teki484f0212016-12-06 00:00:49 +01001591 { }
1592};
1593
1594U_BOOT_DRIVER(fecmxc_gem) = {
1595 .name = "fecmxc",
1596 .id = UCLASS_ETH,
1597 .of_match = fecmxc_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001598 .of_to_plat = fecmxc_of_to_plat,
Jagan Teki484f0212016-12-06 00:00:49 +01001599 .probe = fecmxc_probe,
1600 .remove = fecmxc_remove,
1601 .ops = &fecmxc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001602 .priv_auto = sizeof(struct fec_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001603 .plat_auto = sizeof(struct eth_pdata),
Jagan Teki484f0212016-12-06 00:00:49 +01001604};
Troy Kisky2000c662012-02-07 14:08:47 +00001605#endif