blob: 131d1998a75e61cae50716ccd2157b6cfaf6ad0b [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>
Jagan Teki484f0212016-12-06 00:00:49 +010011#include <dm.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040013#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060014#include <memalign.h>
Jagan Tekic6cd8d52016-12-06 00:00:50 +010015#include <miiphy.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040016#include <net.h>
Jeroen Hofstee120f43f2014-10-08 22:57:40 +020017#include <netdev.h>
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +020018#include <power/regulator.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040019
Ilya Yanoke93a4a52009-07-21 19:32:21 +040020#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090021#include <linux/errno.h>
Marek Vasut4d85b032012-08-26 10:19:20 +000022#include <linux/compiler.h>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040023
Jagan Tekic6cd8d52016-12-06 00:00:50 +010024#include <asm/arch/clock.h>
25#include <asm/arch/imx-regs.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020026#include <asm/mach-imx/sys_proto.h>
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +020027#include <asm-generic/gpio.h>
28
29#include "fec_mxc.h"
Jagan Tekic6cd8d52016-12-06 00:00:50 +010030
Ilya Yanoke93a4a52009-07-21 19:32:21 +040031DECLARE_GLOBAL_DATA_PTR;
32
Marek Vasut5f1631d2012-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 Estevam8b798b22014-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 Yanoke93a4a52009-07-21 19:32:21 +040047#ifndef CONFIG_MII
48#error "CONFIG_MII has to be defined!"
49#endif
50
Eric Nelson3d2f7272012-03-15 18:33:25 +000051#ifndef CONFIG_FEC_XCV_TYPE
52#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasutdbb4fce2011-09-11 18:05:33 +000053#endif
54
Marek Vasut6a5fd4c2011-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 Nelson3d2f7272012-03-15 18:33:25 +000059#ifdef CONFIG_MX28
60#define CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000061#endif
62
Eric Nelson3d2f7272012-03-15 18:33:25 +000063#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!"
73#endif
74
Ilya Yanoke93a4a52009-07-21 19:32:21 +040075#undef DEBUG
76
Eric Nelson3d2f7272012-03-15 18:33:25 +000077#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-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 Tekic6cd8d52016-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 Yanoke93a4a52009-07-21 19:32:21 +040090{
Ilya Yanoke93a4a52009-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 Kisky2000c662012-02-07 14:08:47 +000094 int val;
Ilya Yanoke93a4a52009-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 Vasutbf2386b2011-09-11 18:05:34 +0000100 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100101 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
102 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400103
104 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000105 phy | reg, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400106
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100107 /* wait for the related interrupt */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000108 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000109 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-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 Tekic6cd8d52016-12-06 00:00:50 +0100116 /* clear mii interrupt bit */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000117 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400118
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100119 /* it's now safe to read the PHY's register */
Troy Kisky2000c662012-02-07 14:08:47 +0000120 val = (unsigned short)readl(&eth->mii_data);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100121 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
122 regaddr, val);
Troy Kisky2000c662012-02-07 14:08:47 +0000123 return val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400124}
125
Peng Fandcf5e1b2019-10-25 09:48:02 +0000126#ifndef imx_get_fecclk
127u32 __weak imx_get_fecclk(void)
128{
129 return 0;
130}
131#endif
132
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200133static int fec_get_clk_rate(void *udev, int idx)
134{
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200135 struct fec_priv *fec;
136 struct udevice *dev;
137 int ret;
138
Peng Fandcf5e1b2019-10-25 09:48:02 +0000139 if (IS_ENABLED(CONFIG_IMX8) ||
140 CONFIG_IS_ENABLED(CLK_CCF)) {
141 dev = udev;
142 if (!dev) {
143 ret = uclass_get_device(UCLASS_ETH, idx, &dev);
144 if (ret < 0) {
145 debug("Can't get FEC udev: %d\n", ret);
146 return ret;
147 }
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200148 }
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200149
Peng Fandcf5e1b2019-10-25 09:48:02 +0000150 fec = dev_get_priv(dev);
151 if (fec)
152 return fec->clk_rate;
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200153
Peng Fandcf5e1b2019-10-25 09:48:02 +0000154 return -EINVAL;
155 } else {
156 return imx_get_fecclk();
157 }
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200158}
159
Troy Kisky5e762652012-10-22 16:40:41 +0000160static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic889f2e22010-02-01 14:51:30 +0100161{
162 /*
163 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
164 * and do not drop the Preamble.
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000165 *
166 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
167 * MII_SPEED) register that defines the MDIO output hold time. Earlier
168 * versions are RAZ there, so just ignore the difference and write the
169 * register always.
170 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
171 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
172 * output.
173 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
174 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
175 * holdtime cannot result in a value greater than 3.
Stefano Babic889f2e22010-02-01 14:51:30 +0100176 */
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +0200177 u32 pclk;
178 u32 speed;
179 u32 hold;
180 int ret;
181
182 ret = fec_get_clk_rate(NULL, 0);
183 if (ret < 0) {
184 printf("Can't find FEC0 clk rate: %d\n", ret);
185 return;
186 }
187 pclk = ret;
188 speed = DIV_ROUND_UP(pclk, 5000000);
189 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
190
Markus Niebel1af82742014-02-05 10:54:11 +0100191#ifdef FEC_QUIRK_ENET_MAC
192 speed--;
193#endif
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000194 writel(speed << 1 | hold << 8, &eth->mii_speed);
Troy Kisky5e762652012-10-22 16:40:41 +0000195 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic889f2e22010-02-01 14:51:30 +0100196}
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400197
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100198static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
199 uint8_t regaddr, uint16_t data)
Troy Kisky2000c662012-02-07 14:08:47 +0000200{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400201 uint32_t reg; /* convenient holder for the PHY register */
202 uint32_t phy; /* convenient holder for the PHY */
203 uint32_t start;
204
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100205 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
206 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400207
208 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000209 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400210
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100211 /* wait for the MII interrupt */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000212 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000213 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400214 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
215 printf("Write MDIO failed...\n");
216 return -1;
217 }
218 }
219
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100220 /* clear MII interrupt bit */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000221 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100222 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
223 regaddr, data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400224
225 return 0;
226}
227
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100228static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
229 int regaddr)
Troy Kisky2000c662012-02-07 14:08:47 +0000230{
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100231 return fec_mdio_read(bus->priv, phyaddr, regaddr);
Troy Kisky2000c662012-02-07 14:08:47 +0000232}
233
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100234static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
235 int regaddr, u16 data)
Troy Kisky2000c662012-02-07 14:08:47 +0000236{
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100237 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
Troy Kisky2000c662012-02-07 14:08:47 +0000238}
239
240#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400241static int miiphy_restart_aneg(struct eth_device *dev)
242{
Stefano Babicd6228172012-02-22 00:24:35 +0000243 int ret = 0;
244#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasutedcd6c02011-09-16 01:13:47 +0200245 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000246 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200247
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400248 /*
249 * Wake up from sleep if necessary
250 * Reset PHY, then delay 300ns
251 */
John Rigbye650e492010-01-25 23:12:55 -0700252#ifdef CONFIG_MX27
Troy Kisky2000c662012-02-07 14:08:47 +0000253 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700254#endif
Troy Kisky2000c662012-02-07 14:08:47 +0000255 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400256 udelay(1000);
257
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100258 /* Set the auto-negotiation advertisement register bits */
Troy Kisky2000c662012-02-07 14:08:47 +0000259 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100260 LPA_100FULL | LPA_100HALF | LPA_10FULL |
261 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky2000c662012-02-07 14:08:47 +0000262 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100263 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut539ecee2011-09-11 18:05:36 +0000264
265 if (fec->mii_postcall)
266 ret = fec->mii_postcall(fec->phy_id);
267
Stefano Babicd6228172012-02-22 00:24:35 +0000268#endif
Marek Vasut539ecee2011-09-11 18:05:36 +0000269 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400270}
271
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200272#ifndef CONFIG_FEC_FIXED_SPEED
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400273static int miiphy_wait_aneg(struct eth_device *dev)
274{
275 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000276 int status;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200277 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000278 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400279
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100280 /* Wait for AN completion */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000281 start = get_timer(0);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400282 do {
283 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
284 printf("%s: Autonegotiation timeout\n", dev->name);
285 return -1;
286 }
287
Troy Kisky2000c662012-02-07 14:08:47 +0000288 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
289 if (status < 0) {
290 printf("%s: Autonegotiation failed. status: %d\n",
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100291 dev->name, status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400292 return -1;
293 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500294 } while (!(status & BMSR_LSTATUS));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400295
296 return 0;
297}
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200298#endif /* CONFIG_FEC_FIXED_SPEED */
Troy Kisky2000c662012-02-07 14:08:47 +0000299#endif
300
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400301static int fec_rx_task_enable(struct fec_priv *fec)
302{
Marek Vasutc1582c02012-08-29 03:49:51 +0000303 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400304 return 0;
305}
306
307static int fec_rx_task_disable(struct fec_priv *fec)
308{
309 return 0;
310}
311
312static int fec_tx_task_enable(struct fec_priv *fec)
313{
Marek Vasutc1582c02012-08-29 03:49:51 +0000314 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400315 return 0;
316}
317
318static int fec_tx_task_disable(struct fec_priv *fec)
319{
320 return 0;
321}
322
323/**
324 * Initialize receive task's buffer descriptors
325 * @param[in] fec all we know about the device yet
326 * @param[in] count receive buffer count to be allocated
Eric Nelson3d2f7272012-03-15 18:33:25 +0000327 * @param[in] dsize desired size of each receive buffer
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400328 * @return 0 on success
329 *
Marek Vasut03880452013-10-12 20:36:25 +0200330 * Init all RX descriptors to default values.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400331 */
Marek Vasut03880452013-10-12 20:36:25 +0200332static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400333{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000334 uint32_t size;
Ye Lie2670912018-01-10 13:20:44 +0800335 ulong data;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000336 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400337
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400338 /*
Marek Vasut03880452013-10-12 20:36:25 +0200339 * Reload the RX descriptors with default values and wipe
340 * the RX buffers.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400341 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000342 size = roundup(dsize, ARCH_DMA_MINALIGN);
343 for (i = 0; i < count; i++) {
Ye Lie2670912018-01-10 13:20:44 +0800344 data = fec->rbd_base[i].data_pointer;
345 memset((void *)data, 0, dsize);
346 flush_dcache_range(data, data + size);
Marek Vasut03880452013-10-12 20:36:25 +0200347
348 fec->rbd_base[i].status = FEC_RBD_EMPTY;
349 fec->rbd_base[i].data_length = 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000350 }
351
352 /* Mark the last RBD to close the ring. */
Marek Vasut03880452013-10-12 20:36:25 +0200353 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400354 fec->rbd_index = 0;
355
Ye Lie2670912018-01-10 13:20:44 +0800356 flush_dcache_range((ulong)fec->rbd_base,
357 (ulong)fec->rbd_base + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400358}
359
360/**
361 * Initialize transmit task's buffer descriptors
362 * @param[in] fec all we know about the device yet
363 *
364 * Transmit buffers are created externally. We only have to init the BDs here.\n
365 * Note: There is a race condition in the hardware. When only one BD is in
366 * use it must be marked with the WRAP bit to use it for every transmitt.
367 * This bit in combination with the READY bit results into double transmit
368 * of each data buffer. It seems the state machine checks READY earlier then
369 * resetting it after the first transfer.
370 * Using two BDs solves this issue.
371 */
372static void fec_tbd_init(struct fec_priv *fec)
373{
Ye Lie2670912018-01-10 13:20:44 +0800374 ulong addr = (ulong)fec->tbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000375 unsigned size = roundup(2 * sizeof(struct fec_bd),
376 ARCH_DMA_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +0200377
378 memset(fec->tbd_base, 0, size);
379 fec->tbd_base[0].status = 0;
380 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400381 fec->tbd_index = 0;
Marek Vasut03880452013-10-12 20:36:25 +0200382 flush_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400383}
384
385/**
386 * Mark the given read buffer descriptor as free
387 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100388 * @param[in] prbd buffer descriptor to mark free again
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400389 */
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100390static void fec_rbd_clean(int last, struct fec_bd *prbd)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400391{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000392 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400393 if (last)
Eric Nelson3d2f7272012-03-15 18:33:25 +0000394 flags |= FEC_RBD_WRAP;
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100395 writew(flags, &prbd->status);
396 writew(0, &prbd->data_length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400397}
398
Jagan Tekibc5fb462016-12-06 00:00:48 +0100399static int fec_get_hwaddr(int dev_id, unsigned char *mac)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400400{
Fabio Estevam04fc1282011-12-20 05:46:31 +0000401 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500402 return !is_valid_ethaddr(mac);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400403}
404
Jagan Teki484f0212016-12-06 00:00:49 +0100405#ifdef CONFIG_DM_ETH
406static int fecmxc_set_hwaddr(struct udevice *dev)
407#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100408static int fec_set_hwaddr(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100409#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400410{
Jagan Teki484f0212016-12-06 00:00:49 +0100411#ifdef CONFIG_DM_ETH
412 struct fec_priv *fec = dev_get_priv(dev);
413 struct eth_pdata *pdata = dev_get_platdata(dev);
414 uchar *mac = pdata->enetaddr;
415#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100416 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400417 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100418#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400419
420 writel(0, &fec->eth->iaddr1);
421 writel(0, &fec->eth->iaddr2);
422 writel(0, &fec->eth->gaddr1);
423 writel(0, &fec->eth->gaddr2);
424
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100425 /* Set physical address */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400426 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100427 &fec->eth->paddr1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400428 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
429
430 return 0;
431}
432
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100433/* Do initial configuration of the FEC registers */
Marek Vasut335cbd22012-05-01 11:09:41 +0000434static void fec_reg_setup(struct fec_priv *fec)
435{
436 uint32_t rcntrl;
437
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100438 /* Set interrupt mask register */
Marek Vasut335cbd22012-05-01 11:09:41 +0000439 writel(0x00000000, &fec->eth->imask);
440
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100441 /* Clear FEC-Lite interrupt event register(IEVENT) */
Marek Vasut335cbd22012-05-01 11:09:41 +0000442 writel(0xffffffff, &fec->eth->ievent);
443
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100444 /* Set FEC-Lite receive control register(R_CNTRL): */
Marek Vasut335cbd22012-05-01 11:09:41 +0000445
446 /* Start with frame length = 1518, common for all modes. */
447 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advansacc7a282012-07-19 02:12:46 +0000448 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
449 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
450 if (fec->xcv_type == RGMII)
Marek Vasut335cbd22012-05-01 11:09:41 +0000451 rcntrl |= FEC_RCNTRL_RGMII;
452 else if (fec->xcv_type == RMII)
453 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasut335cbd22012-05-01 11:09:41 +0000454
455 writel(rcntrl, &fec->eth->r_cntrl);
456}
457
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400458/**
459 * Start the FEC engine
460 * @param[in] dev Our device to handle
461 */
Jagan Teki484f0212016-12-06 00:00:49 +0100462#ifdef CONFIG_DM_ETH
463static int fec_open(struct udevice *dev)
464#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400465static int fec_open(struct eth_device *edev)
Jagan Teki484f0212016-12-06 00:00:49 +0100466#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400467{
Jagan Teki484f0212016-12-06 00:00:49 +0100468#ifdef CONFIG_DM_ETH
469 struct fec_priv *fec = dev_get_priv(dev);
470#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400471 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100472#endif
Troy Kisky01112132012-02-07 14:08:46 +0000473 int speed;
Ye Lie2670912018-01-10 13:20:44 +0800474 ulong addr, size;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000475 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400476
477 debug("fec_open: fec_open(dev)\n");
478 /* full-duplex, heartbeat disabled */
479 writel(1 << 2, &fec->eth->x_cntrl);
480 fec->rbd_index = 0;
481
Eric Nelson3d2f7272012-03-15 18:33:25 +0000482 /* Invalidate all descriptors */
483 for (i = 0; i < FEC_RBD_NUM - 1; i++)
484 fec_rbd_clean(0, &fec->rbd_base[i]);
485 fec_rbd_clean(1, &fec->rbd_base[i]);
486
487 /* Flush the descriptors into RAM */
488 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
489 ARCH_DMA_MINALIGN);
Ye Lie2670912018-01-10 13:20:44 +0800490 addr = (ulong)fec->rbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000491 flush_dcache_range(addr, addr + size);
492
Troy Kisky01112132012-02-07 14:08:46 +0000493#ifdef FEC_QUIRK_ENET_MAC
Jason Liubbcef6c2011-12-16 05:17:07 +0000494 /* Enable ENET HW endian SWAP */
495 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100496 &fec->eth->ecntrl);
Jason Liubbcef6c2011-12-16 05:17:07 +0000497 /* Enable ENET store and forward mode */
498 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100499 &fec->eth->x_wmrk);
Jason Liubbcef6c2011-12-16 05:17:07 +0000500#endif
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100501 /* Enable FEC-Lite controller */
John Rigbye650e492010-01-25 23:12:55 -0700502 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100503 &fec->eth->ecntrl);
504
Fabio Estevam84c1f522013-09-13 00:36:27 -0300505#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby99d5fed2010-01-25 23:12:57 -0700506 udelay(100);
John Rigby99d5fed2010-01-25 23:12:57 -0700507
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100508 /* setup the MII gasket for RMII mode */
John Rigby99d5fed2010-01-25 23:12:57 -0700509 /* disable the gasket */
510 writew(0, &fec->eth->miigsk_enr);
511
512 /* wait for the gasket to be disabled */
513 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
514 udelay(2);
515
516 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
517 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
518
519 /* re-enable the gasket */
520 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
521
522 /* wait until MII gasket is ready */
523 int max_loops = 10;
524 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
525 if (--max_loops <= 0) {
526 printf("WAIT for MII Gasket ready timed out\n");
527 break;
528 }
529 }
530#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400531
Troy Kisky2000c662012-02-07 14:08:47 +0000532#ifdef CONFIG_PHYLIB
Troy Kisky2c42b3c2012-10-22 16:40:45 +0000533 {
Troy Kisky2000c662012-02-07 14:08:47 +0000534 /* Start up the PHY */
Timur Tabi42387462012-07-09 08:52:43 +0000535 int ret = phy_startup(fec->phydev);
536
537 if (ret) {
538 printf("Could not initialize PHY %s\n",
539 fec->phydev->dev->name);
540 return ret;
541 }
Troy Kisky2000c662012-02-07 14:08:47 +0000542 speed = fec->phydev->speed;
Troy Kisky2000c662012-02-07 14:08:47 +0000543 }
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200544#elif CONFIG_FEC_FIXED_SPEED
545 speed = CONFIG_FEC_FIXED_SPEED;
Troy Kisky2000c662012-02-07 14:08:47 +0000546#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400547 miiphy_wait_aneg(edev);
Troy Kisky01112132012-02-07 14:08:46 +0000548 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasutedcd6c02011-09-16 01:13:47 +0200549 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky2000c662012-02-07 14:08:47 +0000550#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400551
Troy Kisky01112132012-02-07 14:08:46 +0000552#ifdef FEC_QUIRK_ENET_MAC
553 {
554 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wang89d932a2013-05-27 22:55:43 +0000555 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky01112132012-02-07 14:08:46 +0000556 if (speed == _1000BASET)
557 ecr |= FEC_ECNTRL_SPEED;
558 else if (speed != _100BASET)
559 rcr |= FEC_RCNTRL_RMII_10T;
560 writel(ecr, &fec->eth->ecntrl);
561 writel(rcr, &fec->eth->r_cntrl);
562 }
563#endif
564 debug("%s:Speed=%i\n", __func__, speed);
565
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100566 /* Enable SmartDMA receive task */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400567 fec_rx_task_enable(fec);
568
569 udelay(100000);
570 return 0;
571}
572
Jagan Teki484f0212016-12-06 00:00:49 +0100573#ifdef CONFIG_DM_ETH
574static int fecmxc_init(struct udevice *dev)
575#else
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100576static int fec_init(struct eth_device *dev, bd_t *bd)
Jagan Teki484f0212016-12-06 00:00:49 +0100577#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400578{
Jagan Teki484f0212016-12-06 00:00:49 +0100579#ifdef CONFIG_DM_ETH
580 struct fec_priv *fec = dev_get_priv(dev);
581#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400582 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100583#endif
Ye Lie2670912018-01-10 13:20:44 +0800584 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
585 u8 *i;
586 ulong addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400587
John Rigbya4a30552010-10-13 14:31:08 -0600588 /* Initialize MAC address */
Jagan Teki484f0212016-12-06 00:00:49 +0100589#ifdef CONFIG_DM_ETH
590 fecmxc_set_hwaddr(dev);
591#else
John Rigbya4a30552010-10-13 14:31:08 -0600592 fec_set_hwaddr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100593#endif
John Rigbya4a30552010-10-13 14:31:08 -0600594
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100595 /* Setup transmit descriptors, there are two in total. */
Marek Vasut03880452013-10-12 20:36:25 +0200596 fec_tbd_init(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400597
Marek Vasut03880452013-10-12 20:36:25 +0200598 /* Setup receive descriptors. */
599 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400600
Marek Vasut335cbd22012-05-01 11:09:41 +0000601 fec_reg_setup(fec);
Marek Vasutb8f88562011-09-11 18:05:31 +0000602
benoit.thebaudeau@advans551bb362012-07-19 02:12:58 +0000603 if (fec->xcv_type != SEVENWIRE)
Troy Kisky5e762652012-10-22 16:40:41 +0000604 fec_mii_setspeed(fec->bus->priv);
Marek Vasutb8f88562011-09-11 18:05:31 +0000605
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100606 /* Set Opcode/Pause Duration Register */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400607 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
608 writel(0x2, &fec->eth->x_wmrk);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100609
610 /* Set multicast address filter */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400611 writel(0x00000000, &fec->eth->gaddr1);
612 writel(0x00000000, &fec->eth->gaddr2);
613
Peng Fanbf8e58b2018-01-10 13:20:43 +0800614 /* Do not access reserved register */
Peng Fan6146a082019-04-15 05:18:33 +0000615 if (!is_mx6ul() && !is_mx6ull() && !is_imx8() && !is_imx8m()) {
Peng Fan13433fd2015-08-12 17:46:51 +0800616 /* clear MIB RAM */
617 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
618 writel(0, i);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400619
Peng Fan13433fd2015-08-12 17:46:51 +0800620 /* FIFO receive start register */
621 writel(0x520, &fec->eth->r_fstart);
622 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400623
624 /* size and address of each buffer */
625 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
Ye Lie2670912018-01-10 13:20:44 +0800626
627 addr = (ulong)fec->tbd_base;
628 writel((uint32_t)addr, &fec->eth->etdsr);
629
630 addr = (ulong)fec->rbd_base;
631 writel((uint32_t)addr, &fec->eth->erdsr);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400632
Troy Kisky2000c662012-02-07 14:08:47 +0000633#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400634 if (fec->xcv_type != SEVENWIRE)
635 miiphy_restart_aneg(dev);
Troy Kisky2000c662012-02-07 14:08:47 +0000636#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400637 fec_open(dev);
638 return 0;
639}
640
641/**
642 * Halt the FEC engine
643 * @param[in] dev Our device to handle
644 */
Jagan Teki484f0212016-12-06 00:00:49 +0100645#ifdef CONFIG_DM_ETH
646static void fecmxc_halt(struct udevice *dev)
647#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400648static void fec_halt(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100649#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400650{
Jagan Teki484f0212016-12-06 00:00:49 +0100651#ifdef CONFIG_DM_ETH
652 struct fec_priv *fec = dev_get_priv(dev);
653#else
Marek Vasutedcd6c02011-09-16 01:13:47 +0200654 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100655#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400656 int counter = 0xffff;
657
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100658 /* issue graceful stop command to the FEC transmitter if necessary */
John Rigbye650e492010-01-25 23:12:55 -0700659 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100660 &fec->eth->x_cntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400661
662 debug("eth_halt: wait for stop regs\n");
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100663 /* wait for graceful stop to register */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400664 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700665 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400666
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100667 /* Disable SmartDMA tasks */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400668 fec_tx_task_disable(fec);
669 fec_rx_task_disable(fec);
670
671 /*
672 * Disable the Ethernet Controller
673 * Note: this will also reset the BD index counter!
674 */
John Rigby99d5fed2010-01-25 23:12:57 -0700675 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100676 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400677 fec->rbd_index = 0;
678 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400679 debug("eth_halt: done\n");
680}
681
682/**
683 * Transmit one frame
684 * @param[in] dev Our ethernet device to handle
685 * @param[in] packet Pointer to the data to be transmitted
686 * @param[in] length Data count in bytes
687 * @return 0 on success
688 */
Jagan Teki484f0212016-12-06 00:00:49 +0100689#ifdef CONFIG_DM_ETH
690static int fecmxc_send(struct udevice *dev, void *packet, int length)
691#else
Joe Hershberger7c31bd12012-05-21 14:45:27 +0000692static int fec_send(struct eth_device *dev, void *packet, int length)
Jagan Teki484f0212016-12-06 00:00:49 +0100693#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400694{
695 unsigned int status;
Ye Lie2670912018-01-10 13:20:44 +0800696 u32 size;
697 ulong addr, end;
Marek Vasut5f1631d2012-08-29 03:49:49 +0000698 int timeout = FEC_XFER_TIMEOUT;
699 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400700
701 /*
702 * This routine transmits one frame. This routine only accepts
703 * 6-byte Ethernet addresses.
704 */
Jagan Teki484f0212016-12-06 00:00:49 +0100705#ifdef CONFIG_DM_ETH
706 struct fec_priv *fec = dev_get_priv(dev);
707#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400708 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100709#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400710
711 /*
712 * Check for valid length of data.
713 */
714 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100715 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400716 return -1;
717 }
718
719 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000720 * Setup the transmit buffer. We are always using the first buffer for
721 * transmission, the second will be empty and only used to stop the DMA
722 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400723 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000724#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000725 swap_packet((uint32_t *)packet, length);
726#endif
Eric Nelson3d2f7272012-03-15 18:33:25 +0000727
Ye Lie2670912018-01-10 13:20:44 +0800728 addr = (ulong)packet;
Marek Vasut4325d242012-08-26 10:19:21 +0000729 end = roundup(addr + length, ARCH_DMA_MINALIGN);
730 addr &= ~(ARCH_DMA_MINALIGN - 1);
731 flush_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000732
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400733 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Ye Lie2670912018-01-10 13:20:44 +0800734 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000735
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400736 /*
737 * update BD's status now
738 * This block:
739 * - is always the last in a chain (means no chain)
740 * - should transmitt the CRC
741 * - might be the last BD in the list, so the address counter should
742 * wrap (-> keep the WRAP flag)
743 */
744 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
745 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
746 writew(status, &fec->tbd_base[fec->tbd_index].status);
747
748 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000749 * Flush data cache. This code flushes both TX descriptors to RAM.
750 * After this code, the descriptors will be safely in RAM and we
751 * can start DMA.
752 */
753 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
Ye Lie2670912018-01-10 13:20:44 +0800754 addr = (ulong)fec->tbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000755 flush_dcache_range(addr, addr + size);
756
757 /*
Marek Vasutd521b3c2013-07-12 01:03:04 +0200758 * Below we read the DMA descriptor's last four bytes back from the
759 * DRAM. This is important in order to make sure that all WRITE
760 * operations on the bus that were triggered by previous cache FLUSH
761 * have completed.
762 *
763 * Otherwise, on MX28, it is possible to observe a corruption of the
764 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
765 * for the bus structure of MX28. The scenario is as follows:
766 *
767 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
768 * to DRAM due to flush_dcache_range()
769 * 2) ARM core writes the FEC registers via AHB_ARB2
770 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
771 *
772 * Note that 2) does sometimes finish before 1) due to reordering of
773 * WRITE accesses on the AHB bus, therefore triggering 3) before the
774 * DMA descriptor is fully written into DRAM. This results in occasional
775 * corruption of the DMA descriptor.
776 */
777 readl(addr + size - 4);
778
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100779 /* Enable SmartDMA transmit task */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400780 fec_tx_task_enable(fec);
781
782 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000783 * Wait until frame is sent. On each turn of the wait cycle, we must
784 * invalidate data cache to see what's really in RAM. Also, we need
785 * barrier here.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400786 */
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000787 while (--timeout) {
Marek Vasutc1582c02012-08-29 03:49:51 +0000788 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasut5f1631d2012-08-29 03:49:49 +0000789 break;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400790 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000791
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300792 if (!timeout) {
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000793 ret = -EINVAL;
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300794 goto out;
795 }
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000796
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300797 /*
798 * The TDAR bit is cleared when the descriptors are all out from TX
799 * but on mx6solox we noticed that the READY bit is still not cleared
800 * right after TDAR.
801 * These are two distinct signals, and in IC simulation, we found that
802 * TDAR always gets cleared prior than the READY bit of last BD becomes
803 * cleared.
804 * In mx6solox, we use a later version of FEC IP. It looks like that
805 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
806 * version.
807 *
808 * Fix this by polling the READY bit of BD after the TDAR polling,
809 * which covers the mx6solox case and does not harm the other SoCs.
810 */
811 timeout = FEC_XFER_TIMEOUT;
812 while (--timeout) {
813 invalidate_dcache_range(addr, addr + size);
814 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
815 FEC_TBD_READY))
816 break;
817 }
818
819 if (!timeout)
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000820 ret = -EINVAL;
821
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300822out:
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000823 debug("fec_send: status 0x%x index %d ret %i\n",
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100824 readw(&fec->tbd_base[fec->tbd_index].status),
825 fec->tbd_index, ret);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400826 /* for next transmission use the other buffer */
827 if (fec->tbd_index)
828 fec->tbd_index = 0;
829 else
830 fec->tbd_index = 1;
831
Marek Vasut5f1631d2012-08-29 03:49:49 +0000832 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400833}
834
835/**
836 * Pull one frame from the card
837 * @param[in] dev Our ethernet device to handle
838 * @return Length of packet read
839 */
Jagan Teki484f0212016-12-06 00:00:49 +0100840#ifdef CONFIG_DM_ETH
841static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
842#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400843static int fec_recv(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100844#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400845{
Jagan Teki484f0212016-12-06 00:00:49 +0100846#ifdef CONFIG_DM_ETH
847 struct fec_priv *fec = dev_get_priv(dev);
848#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400849 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100850#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400851 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
852 unsigned long ievent;
853 int frame_length, len = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400854 uint16_t bd_status;
Ye Lie2670912018-01-10 13:20:44 +0800855 ulong addr, size, end;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000856 int i;
Ye Libd7e5382018-03-28 20:54:11 +0800857
858#ifdef CONFIG_DM_ETH
859 *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
860 if (*packetp == 0) {
861 printf("%s: error allocating packetp\n", __func__);
862 return -ENOMEM;
863 }
864#else
Fabio Estevamcc956082013-09-17 23:13:10 -0300865 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ye Libd7e5382018-03-28 20:54:11 +0800866#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400867
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100868 /* Check if any critical events have happened */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400869 ievent = readl(&fec->eth->ievent);
870 writel(ievent, &fec->eth->ievent);
Marek Vasut478e2d02011-10-24 23:40:03 +0000871 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400872 if (ievent & FEC_IEVENT_BABR) {
Jagan Teki484f0212016-12-06 00:00:49 +0100873#ifdef CONFIG_DM_ETH
874 fecmxc_halt(dev);
875 fecmxc_init(dev);
876#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400877 fec_halt(dev);
878 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100879#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400880 printf("some error: 0x%08lx\n", ievent);
881 return 0;
882 }
883 if (ievent & FEC_IEVENT_HBERR) {
884 /* Heartbeat error */
885 writel(0x00000001 | readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100886 &fec->eth->x_cntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400887 }
888 if (ievent & FEC_IEVENT_GRA) {
889 /* Graceful stop complete */
890 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
Jagan Teki484f0212016-12-06 00:00:49 +0100891#ifdef CONFIG_DM_ETH
892 fecmxc_halt(dev);
893#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400894 fec_halt(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100895#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400896 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100897 &fec->eth->x_cntrl);
Jagan Teki484f0212016-12-06 00:00:49 +0100898#ifdef CONFIG_DM_ETH
899 fecmxc_init(dev);
900#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400901 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100902#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400903 }
904 }
905
906 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000907 * Read the buffer status. Before the status can be read, the data cache
908 * must be invalidated, because the data in RAM might have been changed
909 * by DMA. The descriptors are properly aligned to cachelines so there's
910 * no need to worry they'd overlap.
911 *
912 * WARNING: By invalidating the descriptor here, we also invalidate
913 * the descriptors surrounding this one. Therefore we can NOT change the
914 * contents of this descriptor nor the surrounding ones. The problem is
915 * that in order to mark the descriptor as processed, we need to change
916 * the descriptor. The solution is to mark the whole cache line when all
917 * descriptors in the cache line are processed.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400918 */
Ye Lie2670912018-01-10 13:20:44 +0800919 addr = (ulong)rbd;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000920 addr &= ~(ARCH_DMA_MINALIGN - 1);
921 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
922 invalidate_dcache_range(addr, addr + size);
923
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400924 bd_status = readw(&rbd->status);
925 debug("fec_recv: status 0x%x\n", bd_status);
926
927 if (!(bd_status & FEC_RBD_EMPTY)) {
928 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100929 ((readw(&rbd->data_length) - 4) > 14)) {
930 /* Get buffer address and size */
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200931 addr = readl(&rbd->data_pointer);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400932 frame_length = readw(&rbd->data_length) - 4;
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100933 /* Invalidate data cache over the buffer */
Marek Vasut4325d242012-08-26 10:19:21 +0000934 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
935 addr &= ~(ARCH_DMA_MINALIGN - 1);
936 invalidate_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000937
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100938 /* Fill the buffer and pass it to upper layers */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000939#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200940 swap_packet((uint32_t *)addr, frame_length);
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000941#endif
Ye Libd7e5382018-03-28 20:54:11 +0800942
943#ifdef CONFIG_DM_ETH
944 memcpy(*packetp, (char *)addr, frame_length);
945#else
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200946 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500947 net_process_received_packet(buff, frame_length);
Ye Libd7e5382018-03-28 20:54:11 +0800948#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400949 len = frame_length;
950 } else {
951 if (bd_status & FEC_RBD_ERR)
Ye Lie2670912018-01-10 13:20:44 +0800952 debug("error frame: 0x%08lx 0x%08x\n",
953 addr, bd_status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400954 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000955
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400956 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000957 * Free the current buffer, restart the engine and move forward
958 * to the next buffer. Here we check if the whole cacheline of
959 * descriptors was already processed and if so, we mark it free
960 * as whole.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400961 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000962 size = RXDESC_PER_CACHELINE - 1;
963 if ((fec->rbd_index & size) == size) {
964 i = fec->rbd_index - size;
Ye Lie2670912018-01-10 13:20:44 +0800965 addr = (ulong)&fec->rbd_base[i];
Eric Nelson3d2f7272012-03-15 18:33:25 +0000966 for (; i <= fec->rbd_index ; i++) {
967 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
968 &fec->rbd_base[i]);
969 }
970 flush_dcache_range(addr,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100971 addr + ARCH_DMA_MINALIGN);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000972 }
973
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400974 fec_rx_task_enable(fec);
975 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
976 }
977 debug("fec_recv: stop\n");
978
979 return len;
980}
981
Troy Kisky4c2ddec2012-10-22 16:40:44 +0000982static void fec_set_dev_name(char *dest, int dev_id)
983{
984 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
985}
986
Marek Vasut03880452013-10-12 20:36:25 +0200987static int fec_alloc_descs(struct fec_priv *fec)
988{
989 unsigned int size;
990 int i;
991 uint8_t *data;
Ye Lie2670912018-01-10 13:20:44 +0800992 ulong addr;
Marek Vasut03880452013-10-12 20:36:25 +0200993
994 /* Allocate TX descriptors. */
995 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
996 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
997 if (!fec->tbd_base)
998 goto err_tx;
999
1000 /* Allocate RX descriptors. */
1001 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
1002 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
1003 if (!fec->rbd_base)
1004 goto err_rx;
1005
1006 memset(fec->rbd_base, 0, size);
1007
1008 /* Allocate RX buffers. */
1009
1010 /* Maximum RX buffer size. */
Fabio Estevam8b798b22014-08-25 13:34:16 -03001011 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +02001012 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevam8b798b22014-08-25 13:34:16 -03001013 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut03880452013-10-12 20:36:25 +02001014 if (!data) {
1015 printf("%s: error allocating rxbuf %d\n", __func__, i);
1016 goto err_ring;
1017 }
1018
1019 memset(data, 0, size);
1020
Ye Lie2670912018-01-10 13:20:44 +08001021 addr = (ulong)data;
1022 fec->rbd_base[i].data_pointer = (uint32_t)addr;
Marek Vasut03880452013-10-12 20:36:25 +02001023 fec->rbd_base[i].status = FEC_RBD_EMPTY;
1024 fec->rbd_base[i].data_length = 0;
1025 /* Flush the buffer to memory. */
Ye Lie2670912018-01-10 13:20:44 +08001026 flush_dcache_range(addr, addr + size);
Marek Vasut03880452013-10-12 20:36:25 +02001027 }
1028
1029 /* Mark the last RBD to close the ring. */
1030 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
1031
1032 fec->rbd_index = 0;
1033 fec->tbd_index = 0;
1034
1035 return 0;
1036
1037err_ring:
Ye Lie2670912018-01-10 13:20:44 +08001038 for (; i >= 0; i--) {
1039 addr = fec->rbd_base[i].data_pointer;
1040 free((void *)addr);
1041 }
Marek Vasut03880452013-10-12 20:36:25 +02001042 free(fec->rbd_base);
1043err_rx:
1044 free(fec->tbd_base);
1045err_tx:
1046 return -ENOMEM;
1047}
1048
1049static void fec_free_descs(struct fec_priv *fec)
1050{
1051 int i;
Ye Lie2670912018-01-10 13:20:44 +08001052 ulong addr;
Marek Vasut03880452013-10-12 20:36:25 +02001053
Ye Lie2670912018-01-10 13:20:44 +08001054 for (i = 0; i < FEC_RBD_NUM; 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);
1059 free(fec->tbd_base);
1060}
1061
Peng Fan0c59c4f2018-03-28 20:54:12 +08001062struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
Jagan Teki484f0212016-12-06 00:00:49 +01001063{
Peng Fan0c59c4f2018-03-28 20:54:12 +08001064 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
Jagan Teki484f0212016-12-06 00:00:49 +01001065 struct mii_dev *bus;
1066 int ret;
1067
1068 bus = mdio_alloc();
1069 if (!bus) {
1070 printf("mdio_alloc failed\n");
1071 return NULL;
1072 }
1073 bus->read = fec_phy_read;
1074 bus->write = fec_phy_write;
1075 bus->priv = eth;
1076 fec_set_dev_name(bus->name, dev_id);
1077
1078 ret = mdio_register(bus);
1079 if (ret) {
1080 printf("mdio_register failed\n");
1081 free(bus);
1082 return NULL;
1083 }
1084 fec_mii_setspeed(eth);
1085 return bus;
1086}
1087
1088#ifndef CONFIG_DM_ETH
Troy Kiskydce4def2012-10-22 16:40:46 +00001089#ifdef CONFIG_PHYLIB
1090int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1091 struct mii_dev *bus, struct phy_device *phydev)
1092#else
1093static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1094 struct mii_dev *bus, int phy_id)
1095#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001096{
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001097 struct eth_device *edev;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001098 struct fec_priv *fec;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001099 unsigned char ethaddr[6];
Andy Duan8f8e4582017-04-10 19:44:35 +08001100 char mac[16];
Marek Vasut43b10302011-09-11 18:05:37 +00001101 uint32_t start;
1102 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001103
1104 /* create and fill edev struct */
1105 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1106 if (!edev) {
Marek Vasutedcd6c02011-09-16 01:13:47 +02001107 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001108 ret = -ENOMEM;
1109 goto err1;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001110 }
1111
1112 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1113 if (!fec) {
1114 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001115 ret = -ENOMEM;
1116 goto err2;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001117 }
Marek Vasutedcd6c02011-09-16 01:13:47 +02001118
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +09001119 memset(edev, 0, sizeof(*edev));
Marek Vasutedcd6c02011-09-16 01:13:47 +02001120 memset(fec, 0, sizeof(*fec));
1121
Marek Vasut03880452013-10-12 20:36:25 +02001122 ret = fec_alloc_descs(fec);
1123 if (ret)
1124 goto err3;
1125
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001126 edev->priv = fec;
1127 edev->init = fec_init;
1128 edev->send = fec_send;
1129 edev->recv = fec_recv;
1130 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +02001131 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001132
Ye Lie2670912018-01-10 13:20:44 +08001133 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001134 fec->bd = bd;
1135
Marek Vasutdbb4fce2011-09-11 18:05:33 +00001136 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001137
1138 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -07001139 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasut43b10302011-09-11 18:05:37 +00001140 start = get_timer(0);
1141 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1142 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -07001143 printf("FEC MXC: Timeout resetting chip\n");
Marek Vasut03880452013-10-12 20:36:25 +02001144 goto err4;
Marek Vasut43b10302011-09-11 18:05:37 +00001145 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001146 udelay(10);
Marek Vasut43b10302011-09-11 18:05:37 +00001147 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001148
Marek Vasut335cbd22012-05-01 11:09:41 +00001149 fec_reg_setup(fec);
Troy Kisky4c2ddec2012-10-22 16:40:44 +00001150 fec_set_dev_name(edev->name, dev_id);
1151 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001152 fec->bus = bus;
1153 fec_mii_setspeed(bus->priv);
1154#ifdef CONFIG_PHYLIB
1155 fec->phydev = phydev;
1156 phy_connect_dev(phydev, edev);
1157 /* Configure phy */
1158 phy_config(phydev);
1159#else
Marek Vasutedcd6c02011-09-16 01:13:47 +02001160 fec->phy_id = phy_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001161#endif
1162 eth_register(edev);
Andy Duan8f8e4582017-04-10 19:44:35 +08001163 /* only support one eth device, the index number pointed by dev_id */
1164 edev->index = fec->dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001165
Andy Duan0eaaf832017-04-10 19:44:34 +08001166 if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1167 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001168 memcpy(edev->enetaddr, ethaddr, 6);
Andy Duan8f8e4582017-04-10 19:44:35 +08001169 if (fec->dev_id)
1170 sprintf(mac, "eth%daddr", fec->dev_id);
1171 else
1172 strcpy(mac, "ethaddr");
Simon Glass64b723f2017-08-03 12:22:12 -06001173 if (!env_get(mac))
Simon Glass8551d552017-08-03 12:22:11 -06001174 eth_env_set_enetaddr(mac, ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001175 }
1176 return ret;
Marek Vasut03880452013-10-12 20:36:25 +02001177err4:
1178 fec_free_descs(fec);
Troy Kiskydce4def2012-10-22 16:40:46 +00001179err3:
1180 free(fec);
1181err2:
1182 free(edev);
1183err1:
1184 return ret;
1185}
1186
Troy Kiskydce4def2012-10-22 16:40:46 +00001187int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1188{
1189 uint32_t base_mii;
1190 struct mii_dev *bus = NULL;
1191#ifdef CONFIG_PHYLIB
1192 struct phy_device *phydev = NULL;
1193#endif
1194 int ret;
1195
Peng Fana65e0362018-03-28 20:54:14 +08001196#ifdef CONFIG_FEC_MXC_MDIO_BASE
Troy Kisky2000c662012-02-07 14:08:47 +00001197 /*
1198 * The i.MX28 has two ethernet interfaces, but they are not equal.
1199 * Only the first one can access the MDIO bus.
1200 */
Peng Fana65e0362018-03-28 20:54:14 +08001201 base_mii = CONFIG_FEC_MXC_MDIO_BASE;
Troy Kisky2000c662012-02-07 14:08:47 +00001202#else
Troy Kiskydce4def2012-10-22 16:40:46 +00001203 base_mii = addr;
Troy Kisky2000c662012-02-07 14:08:47 +00001204#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001205 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1206 bus = fec_get_miibus(base_mii, dev_id);
1207 if (!bus)
1208 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001209#ifdef CONFIG_PHYLIB
Troy Kiskydce4def2012-10-22 16:40:46 +00001210 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001211 if (!phydev) {
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001212 mdio_unregister(bus);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001213 free(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001214 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001215 }
Troy Kiskydce4def2012-10-22 16:40:46 +00001216 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1217#else
1218 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001219#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001220 if (ret) {
1221#ifdef CONFIG_PHYLIB
1222 free(phydev);
1223#endif
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001224 mdio_unregister(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001225 free(bus);
1226 }
Marek Vasut43b10302011-09-11 18:05:37 +00001227 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001228}
1229
Troy Kisky4e0eae62012-10-22 16:40:42 +00001230#ifdef CONFIG_FEC_MXC_PHYADDR
1231int fecmxc_initialize(bd_t *bd)
1232{
1233 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1234 IMX_FEC_BASE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001235}
Troy Kisky4e0eae62012-10-22 16:40:42 +00001236#endif
Marek Vasut539ecee2011-09-11 18:05:36 +00001237
Troy Kisky2000c662012-02-07 14:08:47 +00001238#ifndef CONFIG_PHYLIB
Marek Vasut539ecee2011-09-11 18:05:36 +00001239int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1240{
1241 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1242 fec->mii_postcall = cb;
1243 return 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001244}
1245#endif
1246
1247#else
1248
Jagan Teki87e7f352016-12-06 00:00:51 +01001249static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1250{
1251 struct fec_priv *priv = dev_get_priv(dev);
1252 struct eth_pdata *pdata = dev_get_platdata(dev);
1253
1254 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1255}
1256
Ye Libd7e5382018-03-28 20:54:11 +08001257static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
1258{
1259 if (packet)
1260 free(packet);
1261
1262 return 0;
1263}
1264
Jagan Teki484f0212016-12-06 00:00:49 +01001265static const struct eth_ops fecmxc_ops = {
1266 .start = fecmxc_init,
1267 .send = fecmxc_send,
1268 .recv = fecmxc_recv,
Ye Libd7e5382018-03-28 20:54:11 +08001269 .free_pkt = fecmxc_free_pkt,
Jagan Teki484f0212016-12-06 00:00:49 +01001270 .stop = fecmxc_halt,
1271 .write_hwaddr = fecmxc_set_hwaddr,
Jagan Teki87e7f352016-12-06 00:00:51 +01001272 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
Jagan Teki484f0212016-12-06 00:00:49 +01001273};
1274
Martyn Welchd1ac23f2018-12-11 11:34:45 +00001275static int device_get_phy_addr(struct udevice *dev)
1276{
1277 struct ofnode_phandle_args phandle_args;
1278 int reg;
1279
1280 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1281 &phandle_args)) {
1282 debug("Failed to find phy-handle");
1283 return -ENODEV;
1284 }
1285
1286 reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
1287
1288 return reg;
1289}
1290
Jagan Teki484f0212016-12-06 00:00:49 +01001291static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1292{
1293 struct phy_device *phydev;
Martyn Welchd1ac23f2018-12-11 11:34:45 +00001294 int addr;
Jagan Teki484f0212016-12-06 00:00:49 +01001295
Martyn Welchd1ac23f2018-12-11 11:34:45 +00001296 addr = device_get_phy_addr(dev);
Lukasz Majewski07b75a32018-04-15 21:45:54 +02001297#ifdef CONFIG_FEC_MXC_PHYADDR
Hannes Schmelzerf7694302019-02-15 10:30:18 +01001298 addr = CONFIG_FEC_MXC_PHYADDR;
Jagan Teki484f0212016-12-06 00:00:49 +01001299#endif
1300
Hannes Schmelzerf7694302019-02-15 10:30:18 +01001301 phydev = phy_connect(priv->bus, addr, dev, priv->interface);
Jagan Teki484f0212016-12-06 00:00:49 +01001302 if (!phydev)
1303 return -ENODEV;
1304
Jagan Teki484f0212016-12-06 00:00:49 +01001305 priv->phydev = phydev;
1306 phy_config(phydev);
1307
1308 return 0;
1309}
1310
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001311#ifdef CONFIG_DM_GPIO
1312/* FEC GPIO reset */
1313static void fec_gpio_reset(struct fec_priv *priv)
1314{
1315 debug("fec_gpio_reset: fec_gpio_reset(dev)\n");
1316 if (dm_gpio_is_valid(&priv->phy_reset_gpio)) {
1317 dm_gpio_set_value(&priv->phy_reset_gpio, 1);
Martin Fuzzey9c3f97a2018-10-04 19:59:18 +02001318 mdelay(priv->reset_delay);
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001319 dm_gpio_set_value(&priv->phy_reset_gpio, 0);
Andrejs Cainikovs24b6aac2019-03-01 13:27:59 +00001320 if (priv->reset_post_delay)
1321 mdelay(priv->reset_post_delay);
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001322 }
1323}
1324#endif
1325
Jagan Teki484f0212016-12-06 00:00:49 +01001326static int fecmxc_probe(struct udevice *dev)
1327{
1328 struct eth_pdata *pdata = dev_get_platdata(dev);
1329 struct fec_priv *priv = dev_get_priv(dev);
1330 struct mii_dev *bus = NULL;
Jagan Teki484f0212016-12-06 00:00:49 +01001331 uint32_t start;
1332 int ret;
1333
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +02001334 if (IS_ENABLED(CONFIG_IMX8)) {
1335 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1336 if (ret < 0) {
1337 debug("Can't get FEC ipg clk: %d\n", ret);
1338 return ret;
1339 }
1340 ret = clk_enable(&priv->ipg_clk);
1341 if (ret < 0) {
1342 debug("Can't enable FEC ipg clk: %d\n", ret);
1343 return ret;
1344 }
1345
1346 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
Peng Fandcf5e1b2019-10-25 09:48:02 +00001347 } else if (CONFIG_IS_ENABLED(CLK_CCF)) {
1348 ret = clk_get_by_name(dev, "ipg", &priv->ipg_clk);
1349 if (ret < 0) {
1350 debug("Can't get FEC ipg clk: %d\n", ret);
1351 return ret;
1352 }
1353 ret = clk_enable(&priv->ipg_clk);
1354 if(ret)
1355 return ret;
1356
1357 ret = clk_get_by_name(dev, "ahb", &priv->ahb_clk);
1358 if (ret < 0) {
1359 debug("Can't get FEC ahb clk: %d\n", ret);
1360 return ret;
1361 }
1362 ret = clk_enable(&priv->ahb_clk);
1363 if (ret)
1364 return ret;
1365
1366 ret = clk_get_by_name(dev, "enet_out", &priv->clk_enet_out);
1367 if (!ret) {
1368 ret = clk_enable(&priv->clk_enet_out);
1369 if (ret)
1370 return ret;
1371 }
1372
1373 ret = clk_get_by_name(dev, "enet_clk_ref", &priv->clk_ref);
1374 if (!ret) {
1375 ret = clk_enable(&priv->clk_ref);
1376 if (ret)
1377 return ret;
1378 }
1379
1380 ret = clk_get_by_name(dev, "ptp", &priv->clk_ptp);
1381 if (!ret) {
1382 ret = clk_enable(&priv->clk_ptp);
1383 if (ret)
1384 return ret;
1385 }
1386
1387 priv->clk_rate = clk_get_rate(&priv->ipg_clk);
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +02001388 }
1389
Jagan Teki484f0212016-12-06 00:00:49 +01001390 ret = fec_alloc_descs(priv);
1391 if (ret)
1392 return ret;
1393
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001394#ifdef CONFIG_DM_REGULATOR
1395 if (priv->phy_supply) {
Adam Fordb3301b62019-01-15 11:26:48 -06001396 ret = regulator_set_enable(priv->phy_supply, true);
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001397 if (ret) {
1398 printf("%s: Error enabling phy supply\n", dev->name);
1399 return ret;
1400 }
1401 }
1402#endif
1403
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001404#ifdef CONFIG_DM_GPIO
1405 fec_gpio_reset(priv);
1406#endif
Jagan Teki484f0212016-12-06 00:00:49 +01001407 /* Reset chip. */
Jagan Tekic6cd8d52016-12-06 00:00:50 +01001408 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1409 &priv->eth->ecntrl);
Jagan Teki484f0212016-12-06 00:00:49 +01001410 start = get_timer(0);
1411 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1412 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1413 printf("FEC MXC: Timeout reseting chip\n");
1414 goto err_timeout;
1415 }
1416 udelay(10);
1417 }
1418
1419 fec_reg_setup(priv);
Jagan Teki484f0212016-12-06 00:00:49 +01001420
Peng Fanbd3e8cb2018-03-28 20:54:13 +08001421 priv->dev_id = dev->seq;
Peng Fana65e0362018-03-28 20:54:14 +08001422#ifdef CONFIG_FEC_MXC_MDIO_BASE
1423 bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, dev->seq);
1424#else
Peng Fanbd3e8cb2018-03-28 20:54:13 +08001425 bus = fec_get_miibus((ulong)priv->eth, dev->seq);
Peng Fana65e0362018-03-28 20:54:14 +08001426#endif
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001427 if (!bus) {
1428 ret = -ENOMEM;
1429 goto err_mii;
1430 }
1431
1432 priv->bus = bus;
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001433 priv->interface = pdata->phy_interface;
Martin Fuzzeyf08eb3d2018-10-04 19:59:21 +02001434 switch (priv->interface) {
1435 case PHY_INTERFACE_MODE_MII:
1436 priv->xcv_type = MII100;
1437 break;
1438 case PHY_INTERFACE_MODE_RMII:
1439 priv->xcv_type = RMII;
1440 break;
1441 case PHY_INTERFACE_MODE_RGMII:
1442 case PHY_INTERFACE_MODE_RGMII_ID:
1443 case PHY_INTERFACE_MODE_RGMII_RXID:
1444 case PHY_INTERFACE_MODE_RGMII_TXID:
1445 priv->xcv_type = RGMII;
1446 break;
1447 default:
1448 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1449 printf("Unsupported interface type %d defaulting to %d\n",
1450 priv->interface, priv->xcv_type);
1451 break;
1452 }
1453
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001454 ret = fec_phy_init(priv, dev);
1455 if (ret)
1456 goto err_phy;
1457
Jagan Teki484f0212016-12-06 00:00:49 +01001458 return 0;
1459
Jagan Teki484f0212016-12-06 00:00:49 +01001460err_phy:
1461 mdio_unregister(bus);
1462 free(bus);
1463err_mii:
Ye Li5fa556c2018-03-28 20:54:16 +08001464err_timeout:
Jagan Teki484f0212016-12-06 00:00:49 +01001465 fec_free_descs(priv);
1466 return ret;
Marek Vasut539ecee2011-09-11 18:05:36 +00001467}
Jagan Teki484f0212016-12-06 00:00:49 +01001468
1469static int fecmxc_remove(struct udevice *dev)
1470{
1471 struct fec_priv *priv = dev_get_priv(dev);
1472
1473 free(priv->phydev);
1474 fec_free_descs(priv);
1475 mdio_unregister(priv->bus);
1476 mdio_free(priv->bus);
1477
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001478#ifdef CONFIG_DM_REGULATOR
1479 if (priv->phy_supply)
1480 regulator_set_enable(priv->phy_supply, false);
1481#endif
1482
Jagan Teki484f0212016-12-06 00:00:49 +01001483 return 0;
1484}
1485
1486static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1487{
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001488 int ret = 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001489 struct eth_pdata *pdata = dev_get_platdata(dev);
1490 struct fec_priv *priv = dev_get_priv(dev);
1491 const char *phy_mode;
1492
Simon Glassba1dea42017-05-17 17:18:05 -06001493 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +01001494 priv->eth = (struct ethernet_regs *)pdata->iobase;
1495
1496 pdata->phy_interface = -1;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001497 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1498 NULL);
Jagan Teki484f0212016-12-06 00:00:49 +01001499 if (phy_mode)
1500 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1501 if (pdata->phy_interface == -1) {
1502 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1503 return -EINVAL;
1504 }
1505
Martin Fuzzey9a6a2c92018-10-04 19:59:20 +02001506#ifdef CONFIG_DM_REGULATOR
1507 device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
1508#endif
1509
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001510#ifdef CONFIG_DM_GPIO
1511 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001512 &priv->phy_reset_gpio, GPIOD_IS_OUT);
1513 if (ret < 0)
1514 return 0; /* property is optional, don't return error! */
Jagan Teki484f0212016-12-06 00:00:49 +01001515
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001516 priv->reset_delay = dev_read_u32_default(dev, "phy-reset-duration", 1);
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001517 if (priv->reset_delay > 1000) {
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001518 printf("FEC MXC: phy reset duration should be <= 1000ms\n");
1519 /* property value wrong, use default value */
1520 priv->reset_delay = 1;
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001521 }
Andrejs Cainikovs24b6aac2019-03-01 13:27:59 +00001522
1523 priv->reset_post_delay = dev_read_u32_default(dev,
1524 "phy-reset-post-delay",
1525 0);
1526 if (priv->reset_post_delay > 1000) {
1527 printf("FEC MXC: phy reset post delay should be <= 1000ms\n");
1528 /* property value wrong, use default value */
1529 priv->reset_post_delay = 0;
1530 }
Michael Trimarchi0e5cccf2018-06-17 15:22:39 +02001531#endif
1532
Martin Fuzzey185e3b82018-10-04 19:59:19 +02001533 return 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001534}
1535
1536static const struct udevice_id fecmxc_ids[] = {
Lukasz Majewski8a8f5a62019-06-19 17:31:03 +02001537 { .compatible = "fsl,imx28-fec" },
Jagan Teki484f0212016-12-06 00:00:49 +01001538 { .compatible = "fsl,imx6q-fec" },
Peng Fan56406302018-03-28 20:54:15 +08001539 { .compatible = "fsl,imx6sl-fec" },
1540 { .compatible = "fsl,imx6sx-fec" },
1541 { .compatible = "fsl,imx6ul-fec" },
Lukasz Majewski47311222018-04-15 21:54:22 +02001542 { .compatible = "fsl,imx53-fec" },
Anatolij Gustschinb71fc5e2018-10-18 16:15:11 +02001543 { .compatible = "fsl,imx7d-fec" },
Lukasz Majewski6b94b0e2019-02-13 22:46:38 +01001544 { .compatible = "fsl,mvf600-fec" },
Jagan Teki484f0212016-12-06 00:00:49 +01001545 { }
1546};
1547
1548U_BOOT_DRIVER(fecmxc_gem) = {
1549 .name = "fecmxc",
1550 .id = UCLASS_ETH,
1551 .of_match = fecmxc_ids,
1552 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1553 .probe = fecmxc_probe,
1554 .remove = fecmxc_remove,
1555 .ops = &fecmxc_ops,
1556 .priv_auto_alloc_size = sizeof(struct fec_priv),
1557 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1558};
Troy Kisky2000c662012-02-07 14:08:47 +00001559#endif