blob: 694a0b2f7eb6cc9a4e7613f454be939edebce3d6 [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>
Alex Kiernan9c215492018-04-01 09:22:38 +000012#include <environment.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>
Ilya Yanoke93a4a52009-07-21 19:32:21 +040018#include "fec_mxc.h"
19
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>
Jagan Tekic6cd8d52016-12-06 00:00:50 +010027
Ilya Yanoke93a4a52009-07-21 19:32:21 +040028DECLARE_GLOBAL_DATA_PTR;
29
Marek Vasut5f1631d2012-08-29 03:49:49 +000030/*
31 * Timeout the transfer after 5 mS. This is usually a bit more, since
32 * the code in the tightloops this timeout is used in adds some overhead.
33 */
34#define FEC_XFER_TIMEOUT 5000
35
Fabio Estevam8b798b22014-08-25 13:34:16 -030036/*
37 * The standard 32-byte DMA alignment does not work on mx6solox, which requires
38 * 64-byte alignment in the DMA RX FEC buffer.
39 * Introduce the FEC_DMA_RX_MINALIGN which can cover mx6solox needs and also
40 * satisfies the alignment on other SoCs (32-bytes)
41 */
42#define FEC_DMA_RX_MINALIGN 64
43
Ilya Yanoke93a4a52009-07-21 19:32:21 +040044#ifndef CONFIG_MII
45#error "CONFIG_MII has to be defined!"
46#endif
47
Eric Nelson3d2f7272012-03-15 18:33:25 +000048#ifndef CONFIG_FEC_XCV_TYPE
49#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasutdbb4fce2011-09-11 18:05:33 +000050#endif
51
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000052/*
53 * The i.MX28 operates with packets in big endian. We need to swap them before
54 * sending and after receiving.
55 */
Eric Nelson3d2f7272012-03-15 18:33:25 +000056#ifdef CONFIG_MX28
57#define CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000058#endif
59
Eric Nelson3d2f7272012-03-15 18:33:25 +000060#define RXDESC_PER_CACHELINE (ARCH_DMA_MINALIGN/sizeof(struct fec_bd))
61
62/* Check various alignment issues at compile time */
63#if ((ARCH_DMA_MINALIGN < 16) || (ARCH_DMA_MINALIGN % 16 != 0))
64#error "ARCH_DMA_MINALIGN must be multiple of 16!"
65#endif
66
67#if ((PKTALIGN < ARCH_DMA_MINALIGN) || \
68 (PKTALIGN % ARCH_DMA_MINALIGN != 0))
69#error "PKTALIGN must be multiple of ARCH_DMA_MINALIGN!"
70#endif
71
Ilya Yanoke93a4a52009-07-21 19:32:21 +040072#undef DEBUG
73
Eric Nelson3d2f7272012-03-15 18:33:25 +000074#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +000075static void swap_packet(uint32_t *packet, int length)
76{
77 int i;
78
79 for (i = 0; i < DIV_ROUND_UP(length, 4); i++)
80 packet[i] = __swab32(packet[i]);
81}
82#endif
83
Jagan Tekic6cd8d52016-12-06 00:00:50 +010084/* MII-interface related functions */
85static int fec_mdio_read(struct ethernet_regs *eth, uint8_t phyaddr,
86 uint8_t regaddr)
Ilya Yanoke93a4a52009-07-21 19:32:21 +040087{
Ilya Yanoke93a4a52009-07-21 19:32:21 +040088 uint32_t reg; /* convenient holder for the PHY register */
89 uint32_t phy; /* convenient holder for the PHY */
90 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +000091 int val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +040092
93 /*
94 * reading from any PHY's register is done by properly
95 * programming the FEC's MII data register.
96 */
Marek Vasutbf2386b2011-09-11 18:05:34 +000097 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Tekic6cd8d52016-12-06 00:00:50 +010098 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
99 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400100
101 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000102 phy | reg, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400103
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100104 /* wait for the related interrupt */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000105 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000106 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400107 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
108 printf("Read MDIO failed...\n");
109 return -1;
110 }
111 }
112
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100113 /* clear mii interrupt bit */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000114 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400115
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100116 /* it's now safe to read the PHY's register */
Troy Kisky2000c662012-02-07 14:08:47 +0000117 val = (unsigned short)readl(&eth->mii_data);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100118 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
119 regaddr, val);
Troy Kisky2000c662012-02-07 14:08:47 +0000120 return val;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400121}
122
Troy Kisky5e762652012-10-22 16:40:41 +0000123static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic889f2e22010-02-01 14:51:30 +0100124{
125 /*
126 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
127 * and do not drop the Preamble.
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000128 *
129 * The i.MX28 and i.MX6 types have another field in the MSCR (aka
130 * MII_SPEED) register that defines the MDIO output hold time. Earlier
131 * versions are RAZ there, so just ignore the difference and write the
132 * register always.
133 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
134 * HOLDTIME + 1 is the number of clk cycles the fec is holding the
135 * output.
136 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
137 * Given that ceil(clkrate / 5000000) <= 64, the calculation for
138 * holdtime cannot result in a value greater than 3.
Stefano Babic889f2e22010-02-01 14:51:30 +0100139 */
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000140 u32 pclk = imx_get_fecclk();
141 u32 speed = DIV_ROUND_UP(pclk, 5000000);
142 u32 hold = DIV_ROUND_UP(pclk, 100000000) - 1;
Markus Niebel1af82742014-02-05 10:54:11 +0100143#ifdef FEC_QUIRK_ENET_MAC
144 speed--;
145#endif
Måns Rullgård4aeddb72015-12-08 15:38:45 +0000146 writel(speed << 1 | hold << 8, &eth->mii_speed);
Troy Kisky5e762652012-10-22 16:40:41 +0000147 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic889f2e22010-02-01 14:51:30 +0100148}
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400149
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100150static int fec_mdio_write(struct ethernet_regs *eth, uint8_t phyaddr,
151 uint8_t regaddr, uint16_t data)
Troy Kisky2000c662012-02-07 14:08:47 +0000152{
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400153 uint32_t reg; /* convenient holder for the PHY register */
154 uint32_t phy; /* convenient holder for the PHY */
155 uint32_t start;
156
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100157 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
158 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400159
160 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutbf2386b2011-09-11 18:05:34 +0000161 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400162
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100163 /* wait for the MII interrupt */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000164 start = get_timer(0);
Marek Vasutbf2386b2011-09-11 18:05:34 +0000165 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400166 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
167 printf("Write MDIO failed...\n");
168 return -1;
169 }
170 }
171
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100172 /* clear MII interrupt bit */
Marek Vasutbf2386b2011-09-11 18:05:34 +0000173 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100174 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
175 regaddr, data);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400176
177 return 0;
178}
179
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100180static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
181 int regaddr)
Troy Kisky2000c662012-02-07 14:08:47 +0000182{
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100183 return fec_mdio_read(bus->priv, phyaddr, regaddr);
Troy Kisky2000c662012-02-07 14:08:47 +0000184}
185
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100186static int fec_phy_write(struct mii_dev *bus, int phyaddr, int dev_addr,
187 int regaddr, u16 data)
Troy Kisky2000c662012-02-07 14:08:47 +0000188{
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100189 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
Troy Kisky2000c662012-02-07 14:08:47 +0000190}
191
192#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400193static int miiphy_restart_aneg(struct eth_device *dev)
194{
Stefano Babicd6228172012-02-22 00:24:35 +0000195 int ret = 0;
196#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasutedcd6c02011-09-16 01:13:47 +0200197 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000198 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200199
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400200 /*
201 * Wake up from sleep if necessary
202 * Reset PHY, then delay 300ns
203 */
John Rigbye650e492010-01-25 23:12:55 -0700204#ifdef CONFIG_MX27
Troy Kisky2000c662012-02-07 14:08:47 +0000205 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbye650e492010-01-25 23:12:55 -0700206#endif
Troy Kisky2000c662012-02-07 14:08:47 +0000207 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400208 udelay(1000);
209
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100210 /* Set the auto-negotiation advertisement register bits */
Troy Kisky2000c662012-02-07 14:08:47 +0000211 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100212 LPA_100FULL | LPA_100HALF | LPA_10FULL |
213 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky2000c662012-02-07 14:08:47 +0000214 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100215 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut539ecee2011-09-11 18:05:36 +0000216
217 if (fec->mii_postcall)
218 ret = fec->mii_postcall(fec->phy_id);
219
Stefano Babicd6228172012-02-22 00:24:35 +0000220#endif
Marek Vasut539ecee2011-09-11 18:05:36 +0000221 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400222}
223
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200224#ifndef CONFIG_FEC_FIXED_SPEED
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400225static int miiphy_wait_aneg(struct eth_device *dev)
226{
227 uint32_t start;
Troy Kisky2000c662012-02-07 14:08:47 +0000228 int status;
Marek Vasutedcd6c02011-09-16 01:13:47 +0200229 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky2000c662012-02-07 14:08:47 +0000230 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400231
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100232 /* Wait for AN completion */
Graeme Russf8b82ee2011-07-15 23:31:37 +0000233 start = get_timer(0);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400234 do {
235 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
236 printf("%s: Autonegotiation timeout\n", dev->name);
237 return -1;
238 }
239
Troy Kisky2000c662012-02-07 14:08:47 +0000240 status = fec_mdio_read(eth, fec->phy_id, MII_BMSR);
241 if (status < 0) {
242 printf("%s: Autonegotiation failed. status: %d\n",
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100243 dev->name, status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400244 return -1;
245 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500246 } while (!(status & BMSR_LSTATUS));
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400247
248 return 0;
249}
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200250#endif /* CONFIG_FEC_FIXED_SPEED */
Troy Kisky2000c662012-02-07 14:08:47 +0000251#endif
252
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400253static int fec_rx_task_enable(struct fec_priv *fec)
254{
Marek Vasutc1582c02012-08-29 03:49:51 +0000255 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400256 return 0;
257}
258
259static int fec_rx_task_disable(struct fec_priv *fec)
260{
261 return 0;
262}
263
264static int fec_tx_task_enable(struct fec_priv *fec)
265{
Marek Vasutc1582c02012-08-29 03:49:51 +0000266 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400267 return 0;
268}
269
270static int fec_tx_task_disable(struct fec_priv *fec)
271{
272 return 0;
273}
274
275/**
276 * Initialize receive task's buffer descriptors
277 * @param[in] fec all we know about the device yet
278 * @param[in] count receive buffer count to be allocated
Eric Nelson3d2f7272012-03-15 18:33:25 +0000279 * @param[in] dsize desired size of each receive buffer
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400280 * @return 0 on success
281 *
Marek Vasut03880452013-10-12 20:36:25 +0200282 * Init all RX descriptors to default values.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400283 */
Marek Vasut03880452013-10-12 20:36:25 +0200284static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400285{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000286 uint32_t size;
Ye Lie2670912018-01-10 13:20:44 +0800287 ulong data;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000288 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400289
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400290 /*
Marek Vasut03880452013-10-12 20:36:25 +0200291 * Reload the RX descriptors with default values and wipe
292 * the RX buffers.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400293 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000294 size = roundup(dsize, ARCH_DMA_MINALIGN);
295 for (i = 0; i < count; i++) {
Ye Lie2670912018-01-10 13:20:44 +0800296 data = fec->rbd_base[i].data_pointer;
297 memset((void *)data, 0, dsize);
298 flush_dcache_range(data, data + size);
Marek Vasut03880452013-10-12 20:36:25 +0200299
300 fec->rbd_base[i].status = FEC_RBD_EMPTY;
301 fec->rbd_base[i].data_length = 0;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000302 }
303
304 /* Mark the last RBD to close the ring. */
Marek Vasut03880452013-10-12 20:36:25 +0200305 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400306 fec->rbd_index = 0;
307
Ye Lie2670912018-01-10 13:20:44 +0800308 flush_dcache_range((ulong)fec->rbd_base,
309 (ulong)fec->rbd_base + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400310}
311
312/**
313 * Initialize transmit task's buffer descriptors
314 * @param[in] fec all we know about the device yet
315 *
316 * Transmit buffers are created externally. We only have to init the BDs here.\n
317 * Note: There is a race condition in the hardware. When only one BD is in
318 * use it must be marked with the WRAP bit to use it for every transmitt.
319 * This bit in combination with the READY bit results into double transmit
320 * of each data buffer. It seems the state machine checks READY earlier then
321 * resetting it after the first transfer.
322 * Using two BDs solves this issue.
323 */
324static void fec_tbd_init(struct fec_priv *fec)
325{
Ye Lie2670912018-01-10 13:20:44 +0800326 ulong addr = (ulong)fec->tbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000327 unsigned size = roundup(2 * sizeof(struct fec_bd),
328 ARCH_DMA_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +0200329
330 memset(fec->tbd_base, 0, size);
331 fec->tbd_base[0].status = 0;
332 fec->tbd_base[1].status = FEC_TBD_WRAP;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400333 fec->tbd_index = 0;
Marek Vasut03880452013-10-12 20:36:25 +0200334 flush_dcache_range(addr, addr + size);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400335}
336
337/**
338 * Mark the given read buffer descriptor as free
339 * @param[in] last 1 if this is the last buffer descriptor in the chain, else 0
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100340 * @param[in] prbd buffer descriptor to mark free again
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400341 */
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100342static void fec_rbd_clean(int last, struct fec_bd *prbd)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400343{
Eric Nelson3d2f7272012-03-15 18:33:25 +0000344 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400345 if (last)
Eric Nelson3d2f7272012-03-15 18:33:25 +0000346 flags |= FEC_RBD_WRAP;
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100347 writew(flags, &prbd->status);
348 writew(0, &prbd->data_length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400349}
350
Jagan Tekibc5fb462016-12-06 00:00:48 +0100351static int fec_get_hwaddr(int dev_id, unsigned char *mac)
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400352{
Fabio Estevam04fc1282011-12-20 05:46:31 +0000353 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500354 return !is_valid_ethaddr(mac);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400355}
356
Jagan Teki484f0212016-12-06 00:00:49 +0100357#ifdef CONFIG_DM_ETH
358static int fecmxc_set_hwaddr(struct udevice *dev)
359#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100360static int fec_set_hwaddr(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100361#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400362{
Jagan Teki484f0212016-12-06 00:00:49 +0100363#ifdef CONFIG_DM_ETH
364 struct fec_priv *fec = dev_get_priv(dev);
365 struct eth_pdata *pdata = dev_get_platdata(dev);
366 uchar *mac = pdata->enetaddr;
367#else
Stefano Babic889f2e22010-02-01 14:51:30 +0100368 uchar *mac = dev->enetaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400369 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100370#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400371
372 writel(0, &fec->eth->iaddr1);
373 writel(0, &fec->eth->iaddr2);
374 writel(0, &fec->eth->gaddr1);
375 writel(0, &fec->eth->gaddr2);
376
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100377 /* Set physical address */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400378 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100379 &fec->eth->paddr1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400380 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
381
382 return 0;
383}
384
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100385/* Do initial configuration of the FEC registers */
Marek Vasut335cbd22012-05-01 11:09:41 +0000386static void fec_reg_setup(struct fec_priv *fec)
387{
388 uint32_t rcntrl;
389
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100390 /* Set interrupt mask register */
Marek Vasut335cbd22012-05-01 11:09:41 +0000391 writel(0x00000000, &fec->eth->imask);
392
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100393 /* Clear FEC-Lite interrupt event register(IEVENT) */
Marek Vasut335cbd22012-05-01 11:09:41 +0000394 writel(0xffffffff, &fec->eth->ievent);
395
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100396 /* Set FEC-Lite receive control register(R_CNTRL): */
Marek Vasut335cbd22012-05-01 11:09:41 +0000397
398 /* Start with frame length = 1518, common for all modes. */
399 rcntrl = PKTSIZE << FEC_RCNTRL_MAX_FL_SHIFT;
benoit.thebaudeau@advansacc7a282012-07-19 02:12:46 +0000400 if (fec->xcv_type != SEVENWIRE) /* xMII modes */
401 rcntrl |= FEC_RCNTRL_FCE | FEC_RCNTRL_MII_MODE;
402 if (fec->xcv_type == RGMII)
Marek Vasut335cbd22012-05-01 11:09:41 +0000403 rcntrl |= FEC_RCNTRL_RGMII;
404 else if (fec->xcv_type == RMII)
405 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasut335cbd22012-05-01 11:09:41 +0000406
407 writel(rcntrl, &fec->eth->r_cntrl);
408}
409
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400410/**
411 * Start the FEC engine
412 * @param[in] dev Our device to handle
413 */
Jagan Teki484f0212016-12-06 00:00:49 +0100414#ifdef CONFIG_DM_ETH
415static int fec_open(struct udevice *dev)
416#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400417static int fec_open(struct eth_device *edev)
Jagan Teki484f0212016-12-06 00:00:49 +0100418#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400419{
Jagan Teki484f0212016-12-06 00:00:49 +0100420#ifdef CONFIG_DM_ETH
421 struct fec_priv *fec = dev_get_priv(dev);
422#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400423 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100424#endif
Troy Kisky01112132012-02-07 14:08:46 +0000425 int speed;
Ye Lie2670912018-01-10 13:20:44 +0800426 ulong addr, size;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000427 int i;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400428
429 debug("fec_open: fec_open(dev)\n");
430 /* full-duplex, heartbeat disabled */
431 writel(1 << 2, &fec->eth->x_cntrl);
432 fec->rbd_index = 0;
433
Eric Nelson3d2f7272012-03-15 18:33:25 +0000434 /* Invalidate all descriptors */
435 for (i = 0; i < FEC_RBD_NUM - 1; i++)
436 fec_rbd_clean(0, &fec->rbd_base[i]);
437 fec_rbd_clean(1, &fec->rbd_base[i]);
438
439 /* Flush the descriptors into RAM */
440 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd),
441 ARCH_DMA_MINALIGN);
Ye Lie2670912018-01-10 13:20:44 +0800442 addr = (ulong)fec->rbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000443 flush_dcache_range(addr, addr + size);
444
Troy Kisky01112132012-02-07 14:08:46 +0000445#ifdef FEC_QUIRK_ENET_MAC
Jason Liubbcef6c2011-12-16 05:17:07 +0000446 /* Enable ENET HW endian SWAP */
447 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100448 &fec->eth->ecntrl);
Jason Liubbcef6c2011-12-16 05:17:07 +0000449 /* Enable ENET store and forward mode */
450 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100451 &fec->eth->x_wmrk);
Jason Liubbcef6c2011-12-16 05:17:07 +0000452#endif
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100453 /* Enable FEC-Lite controller */
John Rigbye650e492010-01-25 23:12:55 -0700454 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100455 &fec->eth->ecntrl);
456
Fabio Estevam84c1f522013-09-13 00:36:27 -0300457#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby99d5fed2010-01-25 23:12:57 -0700458 udelay(100);
John Rigby99d5fed2010-01-25 23:12:57 -0700459
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100460 /* setup the MII gasket for RMII mode */
John Rigby99d5fed2010-01-25 23:12:57 -0700461 /* disable the gasket */
462 writew(0, &fec->eth->miigsk_enr);
463
464 /* wait for the gasket to be disabled */
465 while (readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY)
466 udelay(2);
467
468 /* configure gasket for RMII, 50 MHz, no loopback, and no echo */
469 writew(MIIGSK_CFGR_IF_MODE_RMII, &fec->eth->miigsk_cfgr);
470
471 /* re-enable the gasket */
472 writew(MIIGSK_ENR_EN, &fec->eth->miigsk_enr);
473
474 /* wait until MII gasket is ready */
475 int max_loops = 10;
476 while ((readw(&fec->eth->miigsk_enr) & MIIGSK_ENR_READY) == 0) {
477 if (--max_loops <= 0) {
478 printf("WAIT for MII Gasket ready timed out\n");
479 break;
480 }
481 }
482#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400483
Troy Kisky2000c662012-02-07 14:08:47 +0000484#ifdef CONFIG_PHYLIB
Troy Kisky2c42b3c2012-10-22 16:40:45 +0000485 {
Troy Kisky2000c662012-02-07 14:08:47 +0000486 /* Start up the PHY */
Timur Tabi42387462012-07-09 08:52:43 +0000487 int ret = phy_startup(fec->phydev);
488
489 if (ret) {
490 printf("Could not initialize PHY %s\n",
491 fec->phydev->dev->name);
492 return ret;
493 }
Troy Kisky2000c662012-02-07 14:08:47 +0000494 speed = fec->phydev->speed;
Troy Kisky2000c662012-02-07 14:08:47 +0000495 }
Hannes Schmelzer5a15c1a2016-06-22 12:07:14 +0200496#elif CONFIG_FEC_FIXED_SPEED
497 speed = CONFIG_FEC_FIXED_SPEED;
Troy Kisky2000c662012-02-07 14:08:47 +0000498#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400499 miiphy_wait_aneg(edev);
Troy Kisky01112132012-02-07 14:08:46 +0000500 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasutedcd6c02011-09-16 01:13:47 +0200501 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky2000c662012-02-07 14:08:47 +0000502#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400503
Troy Kisky01112132012-02-07 14:08:46 +0000504#ifdef FEC_QUIRK_ENET_MAC
505 {
506 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wang89d932a2013-05-27 22:55:43 +0000507 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky01112132012-02-07 14:08:46 +0000508 if (speed == _1000BASET)
509 ecr |= FEC_ECNTRL_SPEED;
510 else if (speed != _100BASET)
511 rcr |= FEC_RCNTRL_RMII_10T;
512 writel(ecr, &fec->eth->ecntrl);
513 writel(rcr, &fec->eth->r_cntrl);
514 }
515#endif
516 debug("%s:Speed=%i\n", __func__, speed);
517
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100518 /* Enable SmartDMA receive task */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400519 fec_rx_task_enable(fec);
520
521 udelay(100000);
522 return 0;
523}
524
Jagan Teki484f0212016-12-06 00:00:49 +0100525#ifdef CONFIG_DM_ETH
526static int fecmxc_init(struct udevice *dev)
527#else
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100528static int fec_init(struct eth_device *dev, bd_t *bd)
Jagan Teki484f0212016-12-06 00:00:49 +0100529#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400530{
Jagan Teki484f0212016-12-06 00:00:49 +0100531#ifdef CONFIG_DM_ETH
532 struct fec_priv *fec = dev_get_priv(dev);
533#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400534 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100535#endif
Ye Lie2670912018-01-10 13:20:44 +0800536 u8 *mib_ptr = (uint8_t *)&fec->eth->rmon_t_drop;
537 u8 *i;
538 ulong addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400539
John Rigbya4a30552010-10-13 14:31:08 -0600540 /* Initialize MAC address */
Jagan Teki484f0212016-12-06 00:00:49 +0100541#ifdef CONFIG_DM_ETH
542 fecmxc_set_hwaddr(dev);
543#else
John Rigbya4a30552010-10-13 14:31:08 -0600544 fec_set_hwaddr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100545#endif
John Rigbya4a30552010-10-13 14:31:08 -0600546
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100547 /* Setup transmit descriptors, there are two in total. */
Marek Vasut03880452013-10-12 20:36:25 +0200548 fec_tbd_init(fec);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400549
Marek Vasut03880452013-10-12 20:36:25 +0200550 /* Setup receive descriptors. */
551 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400552
Marek Vasut335cbd22012-05-01 11:09:41 +0000553 fec_reg_setup(fec);
Marek Vasutb8f88562011-09-11 18:05:31 +0000554
benoit.thebaudeau@advans551bb362012-07-19 02:12:58 +0000555 if (fec->xcv_type != SEVENWIRE)
Troy Kisky5e762652012-10-22 16:40:41 +0000556 fec_mii_setspeed(fec->bus->priv);
Marek Vasutb8f88562011-09-11 18:05:31 +0000557
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100558 /* Set Opcode/Pause Duration Register */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400559 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
560 writel(0x2, &fec->eth->x_wmrk);
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100561
562 /* Set multicast address filter */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400563 writel(0x00000000, &fec->eth->gaddr1);
564 writel(0x00000000, &fec->eth->gaddr2);
565
Peng Fanbf8e58b2018-01-10 13:20:43 +0800566 /* Do not access reserved register */
567 if (!is_mx6ul() && !is_mx6ull() && !is_mx8m()) {
Peng Fan13433fd2015-08-12 17:46:51 +0800568 /* clear MIB RAM */
569 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
570 writel(0, i);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400571
Peng Fan13433fd2015-08-12 17:46:51 +0800572 /* FIFO receive start register */
573 writel(0x520, &fec->eth->r_fstart);
574 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400575
576 /* size and address of each buffer */
577 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
Ye Lie2670912018-01-10 13:20:44 +0800578
579 addr = (ulong)fec->tbd_base;
580 writel((uint32_t)addr, &fec->eth->etdsr);
581
582 addr = (ulong)fec->rbd_base;
583 writel((uint32_t)addr, &fec->eth->erdsr);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400584
Troy Kisky2000c662012-02-07 14:08:47 +0000585#ifndef CONFIG_PHYLIB
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400586 if (fec->xcv_type != SEVENWIRE)
587 miiphy_restart_aneg(dev);
Troy Kisky2000c662012-02-07 14:08:47 +0000588#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400589 fec_open(dev);
590 return 0;
591}
592
593/**
594 * Halt the FEC engine
595 * @param[in] dev Our device to handle
596 */
Jagan Teki484f0212016-12-06 00:00:49 +0100597#ifdef CONFIG_DM_ETH
598static void fecmxc_halt(struct udevice *dev)
599#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400600static void fec_halt(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100601#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400602{
Jagan Teki484f0212016-12-06 00:00:49 +0100603#ifdef CONFIG_DM_ETH
604 struct fec_priv *fec = dev_get_priv(dev);
605#else
Marek Vasutedcd6c02011-09-16 01:13:47 +0200606 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100607#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400608 int counter = 0xffff;
609
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100610 /* issue graceful stop command to the FEC transmitter if necessary */
John Rigbye650e492010-01-25 23:12:55 -0700611 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100612 &fec->eth->x_cntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400613
614 debug("eth_halt: wait for stop regs\n");
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100615 /* wait for graceful stop to register */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400616 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbye650e492010-01-25 23:12:55 -0700617 udelay(1);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400618
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100619 /* Disable SmartDMA tasks */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400620 fec_tx_task_disable(fec);
621 fec_rx_task_disable(fec);
622
623 /*
624 * Disable the Ethernet Controller
625 * Note: this will also reset the BD index counter!
626 */
John Rigby99d5fed2010-01-25 23:12:57 -0700627 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100628 &fec->eth->ecntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400629 fec->rbd_index = 0;
630 fec->tbd_index = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400631 debug("eth_halt: done\n");
632}
633
634/**
635 * Transmit one frame
636 * @param[in] dev Our ethernet device to handle
637 * @param[in] packet Pointer to the data to be transmitted
638 * @param[in] length Data count in bytes
639 * @return 0 on success
640 */
Jagan Teki484f0212016-12-06 00:00:49 +0100641#ifdef CONFIG_DM_ETH
642static int fecmxc_send(struct udevice *dev, void *packet, int length)
643#else
Joe Hershberger7c31bd12012-05-21 14:45:27 +0000644static int fec_send(struct eth_device *dev, void *packet, int length)
Jagan Teki484f0212016-12-06 00:00:49 +0100645#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400646{
647 unsigned int status;
Ye Lie2670912018-01-10 13:20:44 +0800648 u32 size;
649 ulong addr, end;
Marek Vasut5f1631d2012-08-29 03:49:49 +0000650 int timeout = FEC_XFER_TIMEOUT;
651 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400652
653 /*
654 * This routine transmits one frame. This routine only accepts
655 * 6-byte Ethernet addresses.
656 */
Jagan Teki484f0212016-12-06 00:00:49 +0100657#ifdef CONFIG_DM_ETH
658 struct fec_priv *fec = dev_get_priv(dev);
659#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400660 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100661#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400662
663 /*
664 * Check for valid length of data.
665 */
666 if ((length > 1500) || (length <= 0)) {
Stefano Babic889f2e22010-02-01 14:51:30 +0100667 printf("Payload (%d) too large\n", length);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400668 return -1;
669 }
670
671 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000672 * Setup the transmit buffer. We are always using the first buffer for
673 * transmission, the second will be empty and only used to stop the DMA
674 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400675 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000676#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000677 swap_packet((uint32_t *)packet, length);
678#endif
Eric Nelson3d2f7272012-03-15 18:33:25 +0000679
Ye Lie2670912018-01-10 13:20:44 +0800680 addr = (ulong)packet;
Marek Vasut4325d242012-08-26 10:19:21 +0000681 end = roundup(addr + length, ARCH_DMA_MINALIGN);
682 addr &= ~(ARCH_DMA_MINALIGN - 1);
683 flush_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000684
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400685 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Ye Lie2670912018-01-10 13:20:44 +0800686 writel((uint32_t)addr, &fec->tbd_base[fec->tbd_index].data_pointer);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000687
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400688 /*
689 * update BD's status now
690 * This block:
691 * - is always the last in a chain (means no chain)
692 * - should transmitt the CRC
693 * - might be the last BD in the list, so the address counter should
694 * wrap (-> keep the WRAP flag)
695 */
696 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
697 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
698 writew(status, &fec->tbd_base[fec->tbd_index].status);
699
700 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000701 * Flush data cache. This code flushes both TX descriptors to RAM.
702 * After this code, the descriptors will be safely in RAM and we
703 * can start DMA.
704 */
705 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
Ye Lie2670912018-01-10 13:20:44 +0800706 addr = (ulong)fec->tbd_base;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000707 flush_dcache_range(addr, addr + size);
708
709 /*
Marek Vasutd521b3c2013-07-12 01:03:04 +0200710 * Below we read the DMA descriptor's last four bytes back from the
711 * DRAM. This is important in order to make sure that all WRITE
712 * operations on the bus that were triggered by previous cache FLUSH
713 * have completed.
714 *
715 * Otherwise, on MX28, it is possible to observe a corruption of the
716 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
717 * for the bus structure of MX28. The scenario is as follows:
718 *
719 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
720 * to DRAM due to flush_dcache_range()
721 * 2) ARM core writes the FEC registers via AHB_ARB2
722 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
723 *
724 * Note that 2) does sometimes finish before 1) due to reordering of
725 * WRITE accesses on the AHB bus, therefore triggering 3) before the
726 * DMA descriptor is fully written into DRAM. This results in occasional
727 * corruption of the DMA descriptor.
728 */
729 readl(addr + size - 4);
730
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100731 /* Enable SmartDMA transmit task */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400732 fec_tx_task_enable(fec);
733
734 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000735 * Wait until frame is sent. On each turn of the wait cycle, we must
736 * invalidate data cache to see what's really in RAM. Also, we need
737 * barrier here.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400738 */
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000739 while (--timeout) {
Marek Vasutc1582c02012-08-29 03:49:51 +0000740 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasut5f1631d2012-08-29 03:49:49 +0000741 break;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400742 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000743
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300744 if (!timeout) {
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000745 ret = -EINVAL;
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300746 goto out;
747 }
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000748
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300749 /*
750 * The TDAR bit is cleared when the descriptors are all out from TX
751 * but on mx6solox we noticed that the READY bit is still not cleared
752 * right after TDAR.
753 * These are two distinct signals, and in IC simulation, we found that
754 * TDAR always gets cleared prior than the READY bit of last BD becomes
755 * cleared.
756 * In mx6solox, we use a later version of FEC IP. It looks like that
757 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
758 * version.
759 *
760 * Fix this by polling the READY bit of BD after the TDAR polling,
761 * which covers the mx6solox case and does not harm the other SoCs.
762 */
763 timeout = FEC_XFER_TIMEOUT;
764 while (--timeout) {
765 invalidate_dcache_range(addr, addr + size);
766 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
767 FEC_TBD_READY))
768 break;
769 }
770
771 if (!timeout)
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000772 ret = -EINVAL;
773
Fabio Estevamc34e99f2014-08-25 13:34:17 -0300774out:
Marek Vasut9bf7bf02012-08-29 03:49:50 +0000775 debug("fec_send: status 0x%x index %d ret %i\n",
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100776 readw(&fec->tbd_base[fec->tbd_index].status),
777 fec->tbd_index, ret);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400778 /* for next transmission use the other buffer */
779 if (fec->tbd_index)
780 fec->tbd_index = 0;
781 else
782 fec->tbd_index = 1;
783
Marek Vasut5f1631d2012-08-29 03:49:49 +0000784 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400785}
786
787/**
788 * Pull one frame from the card
789 * @param[in] dev Our ethernet device to handle
790 * @return Length of packet read
791 */
Jagan Teki484f0212016-12-06 00:00:49 +0100792#ifdef CONFIG_DM_ETH
793static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
794#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400795static int fec_recv(struct eth_device *dev)
Jagan Teki484f0212016-12-06 00:00:49 +0100796#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400797{
Jagan Teki484f0212016-12-06 00:00:49 +0100798#ifdef CONFIG_DM_ETH
799 struct fec_priv *fec = dev_get_priv(dev);
800#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400801 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki484f0212016-12-06 00:00:49 +0100802#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400803 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
804 unsigned long ievent;
805 int frame_length, len = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400806 uint16_t bd_status;
Ye Lie2670912018-01-10 13:20:44 +0800807 ulong addr, size, end;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000808 int i;
Ye Libd7e5382018-03-28 20:54:11 +0800809
810#ifdef CONFIG_DM_ETH
811 *packetp = memalign(ARCH_DMA_MINALIGN, FEC_MAX_PKT_SIZE);
812 if (*packetp == 0) {
813 printf("%s: error allocating packetp\n", __func__);
814 return -ENOMEM;
815 }
816#else
Fabio Estevamcc956082013-09-17 23:13:10 -0300817 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ye Libd7e5382018-03-28 20:54:11 +0800818#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400819
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100820 /* Check if any critical events have happened */
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400821 ievent = readl(&fec->eth->ievent);
822 writel(ievent, &fec->eth->ievent);
Marek Vasut478e2d02011-10-24 23:40:03 +0000823 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400824 if (ievent & FEC_IEVENT_BABR) {
Jagan Teki484f0212016-12-06 00:00:49 +0100825#ifdef CONFIG_DM_ETH
826 fecmxc_halt(dev);
827 fecmxc_init(dev);
828#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400829 fec_halt(dev);
830 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100831#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400832 printf("some error: 0x%08lx\n", ievent);
833 return 0;
834 }
835 if (ievent & FEC_IEVENT_HBERR) {
836 /* Heartbeat error */
837 writel(0x00000001 | readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100838 &fec->eth->x_cntrl);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400839 }
840 if (ievent & FEC_IEVENT_GRA) {
841 /* Graceful stop complete */
842 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
Jagan Teki484f0212016-12-06 00:00:49 +0100843#ifdef CONFIG_DM_ETH
844 fecmxc_halt(dev);
845#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400846 fec_halt(dev);
Jagan Teki484f0212016-12-06 00:00:49 +0100847#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400848 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100849 &fec->eth->x_cntrl);
Jagan Teki484f0212016-12-06 00:00:49 +0100850#ifdef CONFIG_DM_ETH
851 fecmxc_init(dev);
852#else
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400853 fec_init(dev, fec->bd);
Jagan Teki484f0212016-12-06 00:00:49 +0100854#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400855 }
856 }
857
858 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000859 * Read the buffer status. Before the status can be read, the data cache
860 * must be invalidated, because the data in RAM might have been changed
861 * by DMA. The descriptors are properly aligned to cachelines so there's
862 * no need to worry they'd overlap.
863 *
864 * WARNING: By invalidating the descriptor here, we also invalidate
865 * the descriptors surrounding this one. Therefore we can NOT change the
866 * contents of this descriptor nor the surrounding ones. The problem is
867 * that in order to mark the descriptor as processed, we need to change
868 * the descriptor. The solution is to mark the whole cache line when all
869 * descriptors in the cache line are processed.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400870 */
Ye Lie2670912018-01-10 13:20:44 +0800871 addr = (ulong)rbd;
Eric Nelson3d2f7272012-03-15 18:33:25 +0000872 addr &= ~(ARCH_DMA_MINALIGN - 1);
873 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
874 invalidate_dcache_range(addr, addr + size);
875
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400876 bd_status = readw(&rbd->status);
877 debug("fec_recv: status 0x%x\n", bd_status);
878
879 if (!(bd_status & FEC_RBD_EMPTY)) {
880 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100881 ((readw(&rbd->data_length) - 4) > 14)) {
882 /* Get buffer address and size */
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200883 addr = readl(&rbd->data_pointer);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400884 frame_length = readw(&rbd->data_length) - 4;
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100885 /* Invalidate data cache over the buffer */
Marek Vasut4325d242012-08-26 10:19:21 +0000886 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
887 addr &= ~(ARCH_DMA_MINALIGN - 1);
888 invalidate_dcache_range(addr, end);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000889
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100890 /* Fill the buffer and pass it to upper layers */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000891#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200892 swap_packet((uint32_t *)addr, frame_length);
Marek Vasut6a5fd4c2011-11-08 23:18:10 +0000893#endif
Ye Libd7e5382018-03-28 20:54:11 +0800894
895#ifdef CONFIG_DM_ETH
896 memcpy(*packetp, (char *)addr, frame_length);
897#else
Albert ARIBAUD \(3ADEV\)13420302015-06-19 14:18:27 +0200898 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger9f09a362015-04-08 01:41:06 -0500899 net_process_received_packet(buff, frame_length);
Ye Libd7e5382018-03-28 20:54:11 +0800900#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400901 len = frame_length;
902 } else {
903 if (bd_status & FEC_RBD_ERR)
Ye Lie2670912018-01-10 13:20:44 +0800904 debug("error frame: 0x%08lx 0x%08x\n",
905 addr, bd_status);
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400906 }
Eric Nelson3d2f7272012-03-15 18:33:25 +0000907
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400908 /*
Eric Nelson3d2f7272012-03-15 18:33:25 +0000909 * Free the current buffer, restart the engine and move forward
910 * to the next buffer. Here we check if the whole cacheline of
911 * descriptors was already processed and if so, we mark it free
912 * as whole.
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400913 */
Eric Nelson3d2f7272012-03-15 18:33:25 +0000914 size = RXDESC_PER_CACHELINE - 1;
915 if ((fec->rbd_index & size) == size) {
916 i = fec->rbd_index - size;
Ye Lie2670912018-01-10 13:20:44 +0800917 addr = (ulong)&fec->rbd_base[i];
Eric Nelson3d2f7272012-03-15 18:33:25 +0000918 for (; i <= fec->rbd_index ; i++) {
919 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
920 &fec->rbd_base[i]);
921 }
922 flush_dcache_range(addr,
Jagan Tekic6cd8d52016-12-06 00:00:50 +0100923 addr + ARCH_DMA_MINALIGN);
Eric Nelson3d2f7272012-03-15 18:33:25 +0000924 }
925
Ilya Yanoke93a4a52009-07-21 19:32:21 +0400926 fec_rx_task_enable(fec);
927 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
928 }
929 debug("fec_recv: stop\n");
930
931 return len;
932}
933
Troy Kisky4c2ddec2012-10-22 16:40:44 +0000934static void fec_set_dev_name(char *dest, int dev_id)
935{
936 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
937}
938
Marek Vasut03880452013-10-12 20:36:25 +0200939static int fec_alloc_descs(struct fec_priv *fec)
940{
941 unsigned int size;
942 int i;
943 uint8_t *data;
Ye Lie2670912018-01-10 13:20:44 +0800944 ulong addr;
Marek Vasut03880452013-10-12 20:36:25 +0200945
946 /* Allocate TX descriptors. */
947 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
948 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
949 if (!fec->tbd_base)
950 goto err_tx;
951
952 /* Allocate RX descriptors. */
953 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
954 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
955 if (!fec->rbd_base)
956 goto err_rx;
957
958 memset(fec->rbd_base, 0, size);
959
960 /* Allocate RX buffers. */
961
962 /* Maximum RX buffer size. */
Fabio Estevam8b798b22014-08-25 13:34:16 -0300963 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut03880452013-10-12 20:36:25 +0200964 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevam8b798b22014-08-25 13:34:16 -0300965 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut03880452013-10-12 20:36:25 +0200966 if (!data) {
967 printf("%s: error allocating rxbuf %d\n", __func__, i);
968 goto err_ring;
969 }
970
971 memset(data, 0, size);
972
Ye Lie2670912018-01-10 13:20:44 +0800973 addr = (ulong)data;
974 fec->rbd_base[i].data_pointer = (uint32_t)addr;
Marek Vasut03880452013-10-12 20:36:25 +0200975 fec->rbd_base[i].status = FEC_RBD_EMPTY;
976 fec->rbd_base[i].data_length = 0;
977 /* Flush the buffer to memory. */
Ye Lie2670912018-01-10 13:20:44 +0800978 flush_dcache_range(addr, addr + size);
Marek Vasut03880452013-10-12 20:36:25 +0200979 }
980
981 /* Mark the last RBD to close the ring. */
982 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
983
984 fec->rbd_index = 0;
985 fec->tbd_index = 0;
986
987 return 0;
988
989err_ring:
Ye Lie2670912018-01-10 13:20:44 +0800990 for (; i >= 0; i--) {
991 addr = fec->rbd_base[i].data_pointer;
992 free((void *)addr);
993 }
Marek Vasut03880452013-10-12 20:36:25 +0200994 free(fec->rbd_base);
995err_rx:
996 free(fec->tbd_base);
997err_tx:
998 return -ENOMEM;
999}
1000
1001static void fec_free_descs(struct fec_priv *fec)
1002{
1003 int i;
Ye Lie2670912018-01-10 13:20:44 +08001004 ulong addr;
Marek Vasut03880452013-10-12 20:36:25 +02001005
Ye Lie2670912018-01-10 13:20:44 +08001006 for (i = 0; i < FEC_RBD_NUM; i++) {
1007 addr = fec->rbd_base[i].data_pointer;
1008 free((void *)addr);
1009 }
Marek Vasut03880452013-10-12 20:36:25 +02001010 free(fec->rbd_base);
1011 free(fec->tbd_base);
1012}
1013
Peng Fan0c59c4f2018-03-28 20:54:12 +08001014struct mii_dev *fec_get_miibus(ulong base_addr, int dev_id)
Jagan Teki484f0212016-12-06 00:00:49 +01001015{
Peng Fan0c59c4f2018-03-28 20:54:12 +08001016 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
Jagan Teki484f0212016-12-06 00:00:49 +01001017 struct mii_dev *bus;
1018 int ret;
1019
1020 bus = mdio_alloc();
1021 if (!bus) {
1022 printf("mdio_alloc failed\n");
1023 return NULL;
1024 }
1025 bus->read = fec_phy_read;
1026 bus->write = fec_phy_write;
1027 bus->priv = eth;
1028 fec_set_dev_name(bus->name, dev_id);
1029
1030 ret = mdio_register(bus);
1031 if (ret) {
1032 printf("mdio_register failed\n");
1033 free(bus);
1034 return NULL;
1035 }
1036 fec_mii_setspeed(eth);
1037 return bus;
1038}
1039
1040#ifndef CONFIG_DM_ETH
Troy Kiskydce4def2012-10-22 16:40:46 +00001041#ifdef CONFIG_PHYLIB
1042int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1043 struct mii_dev *bus, struct phy_device *phydev)
1044#else
1045static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1046 struct mii_dev *bus, int phy_id)
1047#endif
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001048{
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001049 struct eth_device *edev;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001050 struct fec_priv *fec;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001051 unsigned char ethaddr[6];
Andy Duan8f8e4582017-04-10 19:44:35 +08001052 char mac[16];
Marek Vasut43b10302011-09-11 18:05:37 +00001053 uint32_t start;
1054 int ret = 0;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001055
1056 /* create and fill edev struct */
1057 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1058 if (!edev) {
Marek Vasutedcd6c02011-09-16 01:13:47 +02001059 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001060 ret = -ENOMEM;
1061 goto err1;
Marek Vasutedcd6c02011-09-16 01:13:47 +02001062 }
1063
1064 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1065 if (!fec) {
1066 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasut43b10302011-09-11 18:05:37 +00001067 ret = -ENOMEM;
1068 goto err2;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001069 }
Marek Vasutedcd6c02011-09-16 01:13:47 +02001070
Nobuhiro Iwamatsu1843c5b2010-10-19 14:03:42 +09001071 memset(edev, 0, sizeof(*edev));
Marek Vasutedcd6c02011-09-16 01:13:47 +02001072 memset(fec, 0, sizeof(*fec));
1073
Marek Vasut03880452013-10-12 20:36:25 +02001074 ret = fec_alloc_descs(fec);
1075 if (ret)
1076 goto err3;
1077
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001078 edev->priv = fec;
1079 edev->init = fec_init;
1080 edev->send = fec_send;
1081 edev->recv = fec_recv;
1082 edev->halt = fec_halt;
Heiko Schocher9ada5e62010-04-27 07:43:52 +02001083 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001084
Ye Lie2670912018-01-10 13:20:44 +08001085 fec->eth = (struct ethernet_regs *)(ulong)base_addr;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001086 fec->bd = bd;
1087
Marek Vasutdbb4fce2011-09-11 18:05:33 +00001088 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001089
1090 /* Reset chip. */
John Rigbye650e492010-01-25 23:12:55 -07001091 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasut43b10302011-09-11 18:05:37 +00001092 start = get_timer(0);
1093 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1094 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -07001095 printf("FEC MXC: Timeout resetting chip\n");
Marek Vasut03880452013-10-12 20:36:25 +02001096 goto err4;
Marek Vasut43b10302011-09-11 18:05:37 +00001097 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001098 udelay(10);
Marek Vasut43b10302011-09-11 18:05:37 +00001099 }
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001100
Marek Vasut335cbd22012-05-01 11:09:41 +00001101 fec_reg_setup(fec);
Troy Kisky4c2ddec2012-10-22 16:40:44 +00001102 fec_set_dev_name(edev->name, dev_id);
1103 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001104 fec->bus = bus;
1105 fec_mii_setspeed(bus->priv);
1106#ifdef CONFIG_PHYLIB
1107 fec->phydev = phydev;
1108 phy_connect_dev(phydev, edev);
1109 /* Configure phy */
1110 phy_config(phydev);
1111#else
Marek Vasutedcd6c02011-09-16 01:13:47 +02001112 fec->phy_id = phy_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001113#endif
1114 eth_register(edev);
Andy Duan8f8e4582017-04-10 19:44:35 +08001115 /* only support one eth device, the index number pointed by dev_id */
1116 edev->index = fec->dev_id;
Troy Kiskydce4def2012-10-22 16:40:46 +00001117
Andy Duan0eaaf832017-04-10 19:44:34 +08001118 if (fec_get_hwaddr(fec->dev_id, ethaddr) == 0) {
1119 debug("got MAC%d address from fuse: %pM\n", fec->dev_id, ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001120 memcpy(edev->enetaddr, ethaddr, 6);
Andy Duan8f8e4582017-04-10 19:44:35 +08001121 if (fec->dev_id)
1122 sprintf(mac, "eth%daddr", fec->dev_id);
1123 else
1124 strcpy(mac, "ethaddr");
Simon Glass64b723f2017-08-03 12:22:12 -06001125 if (!env_get(mac))
Simon Glass8551d552017-08-03 12:22:11 -06001126 eth_env_set_enetaddr(mac, ethaddr);
Troy Kiskydce4def2012-10-22 16:40:46 +00001127 }
1128 return ret;
Marek Vasut03880452013-10-12 20:36:25 +02001129err4:
1130 fec_free_descs(fec);
Troy Kiskydce4def2012-10-22 16:40:46 +00001131err3:
1132 free(fec);
1133err2:
1134 free(edev);
1135err1:
1136 return ret;
1137}
1138
Troy Kiskydce4def2012-10-22 16:40:46 +00001139int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1140{
1141 uint32_t base_mii;
1142 struct mii_dev *bus = NULL;
1143#ifdef CONFIG_PHYLIB
1144 struct phy_device *phydev = NULL;
1145#endif
1146 int ret;
1147
Peng Fana65e0362018-03-28 20:54:14 +08001148#ifdef CONFIG_FEC_MXC_MDIO_BASE
Troy Kisky2000c662012-02-07 14:08:47 +00001149 /*
1150 * The i.MX28 has two ethernet interfaces, but they are not equal.
1151 * Only the first one can access the MDIO bus.
1152 */
Peng Fana65e0362018-03-28 20:54:14 +08001153 base_mii = CONFIG_FEC_MXC_MDIO_BASE;
Troy Kisky2000c662012-02-07 14:08:47 +00001154#else
Troy Kiskydce4def2012-10-22 16:40:46 +00001155 base_mii = addr;
Troy Kisky2000c662012-02-07 14:08:47 +00001156#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001157 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
1158 bus = fec_get_miibus(base_mii, dev_id);
1159 if (!bus)
1160 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001161#ifdef CONFIG_PHYLIB
Troy Kiskydce4def2012-10-22 16:40:46 +00001162 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001163 if (!phydev) {
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001164 mdio_unregister(bus);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001165 free(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001166 return -ENOMEM;
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001167 }
Troy Kiskydce4def2012-10-22 16:40:46 +00001168 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1169#else
1170 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
Troy Kisky2c42b3c2012-10-22 16:40:45 +00001171#endif
Troy Kiskydce4def2012-10-22 16:40:46 +00001172 if (ret) {
1173#ifdef CONFIG_PHYLIB
1174 free(phydev);
1175#endif
Måns Rullgårdc6e4a862015-12-08 15:38:46 +00001176 mdio_unregister(bus);
Troy Kiskydce4def2012-10-22 16:40:46 +00001177 free(bus);
1178 }
Marek Vasut43b10302011-09-11 18:05:37 +00001179 return ret;
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001180}
1181
Troy Kisky4e0eae62012-10-22 16:40:42 +00001182#ifdef CONFIG_FEC_MXC_PHYADDR
1183int fecmxc_initialize(bd_t *bd)
1184{
1185 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1186 IMX_FEC_BASE);
Ilya Yanoke93a4a52009-07-21 19:32:21 +04001187}
Troy Kisky4e0eae62012-10-22 16:40:42 +00001188#endif
Marek Vasut539ecee2011-09-11 18:05:36 +00001189
Troy Kisky2000c662012-02-07 14:08:47 +00001190#ifndef CONFIG_PHYLIB
Marek Vasut539ecee2011-09-11 18:05:36 +00001191int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1192{
1193 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1194 fec->mii_postcall = cb;
1195 return 0;
Jagan Teki484f0212016-12-06 00:00:49 +01001196}
1197#endif
1198
1199#else
1200
Jagan Teki87e7f352016-12-06 00:00:51 +01001201static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1202{
1203 struct fec_priv *priv = dev_get_priv(dev);
1204 struct eth_pdata *pdata = dev_get_platdata(dev);
1205
1206 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1207}
1208
Ye Libd7e5382018-03-28 20:54:11 +08001209static int fecmxc_free_pkt(struct udevice *dev, uchar *packet, int length)
1210{
1211 if (packet)
1212 free(packet);
1213
1214 return 0;
1215}
1216
Jagan Teki484f0212016-12-06 00:00:49 +01001217static const struct eth_ops fecmxc_ops = {
1218 .start = fecmxc_init,
1219 .send = fecmxc_send,
1220 .recv = fecmxc_recv,
Ye Libd7e5382018-03-28 20:54:11 +08001221 .free_pkt = fecmxc_free_pkt,
Jagan Teki484f0212016-12-06 00:00:49 +01001222 .stop = fecmxc_halt,
1223 .write_hwaddr = fecmxc_set_hwaddr,
Jagan Teki87e7f352016-12-06 00:00:51 +01001224 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
Jagan Teki484f0212016-12-06 00:00:49 +01001225};
1226
1227static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1228{
1229 struct phy_device *phydev;
1230 int mask = 0xffffffff;
1231
Lukasz Majewski07b75a32018-04-15 21:45:54 +02001232#ifdef CONFIG_FEC_MXC_PHYADDR
Jagan Teki484f0212016-12-06 00:00:49 +01001233 mask = 1 << CONFIG_FEC_MXC_PHYADDR;
1234#endif
1235
1236 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
1237 if (!phydev)
1238 return -ENODEV;
1239
1240 phy_connect_dev(phydev, dev);
1241
1242 priv->phydev = phydev;
1243 phy_config(phydev);
1244
1245 return 0;
1246}
1247
1248static int fecmxc_probe(struct udevice *dev)
1249{
1250 struct eth_pdata *pdata = dev_get_platdata(dev);
1251 struct fec_priv *priv = dev_get_priv(dev);
1252 struct mii_dev *bus = NULL;
Jagan Teki484f0212016-12-06 00:00:49 +01001253 uint32_t start;
1254 int ret;
1255
1256 ret = fec_alloc_descs(priv);
1257 if (ret)
1258 return ret;
1259
Jagan Teki484f0212016-12-06 00:00:49 +01001260 /* Reset chip. */
Jagan Tekic6cd8d52016-12-06 00:00:50 +01001261 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1262 &priv->eth->ecntrl);
Jagan Teki484f0212016-12-06 00:00:49 +01001263 start = get_timer(0);
1264 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1265 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1266 printf("FEC MXC: Timeout reseting chip\n");
1267 goto err_timeout;
1268 }
1269 udelay(10);
1270 }
1271
1272 fec_reg_setup(priv);
Jagan Teki484f0212016-12-06 00:00:49 +01001273
Peng Fanbd3e8cb2018-03-28 20:54:13 +08001274 priv->dev_id = dev->seq;
Peng Fana65e0362018-03-28 20:54:14 +08001275#ifdef CONFIG_FEC_MXC_MDIO_BASE
1276 bus = fec_get_miibus((ulong)CONFIG_FEC_MXC_MDIO_BASE, dev->seq);
1277#else
Peng Fanbd3e8cb2018-03-28 20:54:13 +08001278 bus = fec_get_miibus((ulong)priv->eth, dev->seq);
Peng Fana65e0362018-03-28 20:54:14 +08001279#endif
Lothar Waßmannd33e9ee2017-06-27 15:23:16 +02001280 if (!bus) {
1281 ret = -ENOMEM;
1282 goto err_mii;
1283 }
1284
1285 priv->bus = bus;
1286 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1287 priv->interface = pdata->phy_interface;
1288 ret = fec_phy_init(priv, dev);
1289 if (ret)
1290 goto err_phy;
1291
Jagan Teki484f0212016-12-06 00:00:49 +01001292 return 0;
1293
Jagan Teki484f0212016-12-06 00:00:49 +01001294err_phy:
1295 mdio_unregister(bus);
1296 free(bus);
1297err_mii:
Ye Li5fa556c2018-03-28 20:54:16 +08001298err_timeout:
Jagan Teki484f0212016-12-06 00:00:49 +01001299 fec_free_descs(priv);
1300 return ret;
Marek Vasut539ecee2011-09-11 18:05:36 +00001301}
Jagan Teki484f0212016-12-06 00:00:49 +01001302
1303static int fecmxc_remove(struct udevice *dev)
1304{
1305 struct fec_priv *priv = dev_get_priv(dev);
1306
1307 free(priv->phydev);
1308 fec_free_descs(priv);
1309 mdio_unregister(priv->bus);
1310 mdio_free(priv->bus);
1311
1312 return 0;
1313}
1314
1315static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1316{
1317 struct eth_pdata *pdata = dev_get_platdata(dev);
1318 struct fec_priv *priv = dev_get_priv(dev);
1319 const char *phy_mode;
1320
Simon Glassba1dea42017-05-17 17:18:05 -06001321 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
Jagan Teki484f0212016-12-06 00:00:49 +01001322 priv->eth = (struct ethernet_regs *)pdata->iobase;
1323
1324 pdata->phy_interface = -1;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001325 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1326 NULL);
Jagan Teki484f0212016-12-06 00:00:49 +01001327 if (phy_mode)
1328 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1329 if (pdata->phy_interface == -1) {
1330 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1331 return -EINVAL;
1332 }
1333
1334 /* TODO
1335 * Need to get the reset-gpio and related properties from DT
1336 * and implemet the enet reset code on .probe call
1337 */
1338
1339 return 0;
1340}
1341
1342static const struct udevice_id fecmxc_ids[] = {
1343 { .compatible = "fsl,imx6q-fec" },
Peng Fan56406302018-03-28 20:54:15 +08001344 { .compatible = "fsl,imx6sl-fec" },
1345 { .compatible = "fsl,imx6sx-fec" },
1346 { .compatible = "fsl,imx6ul-fec" },
Lukasz Majewski47311222018-04-15 21:54:22 +02001347 { .compatible = "fsl,imx53-fec" },
Jagan Teki484f0212016-12-06 00:00:49 +01001348 { }
1349};
1350
1351U_BOOT_DRIVER(fecmxc_gem) = {
1352 .name = "fecmxc",
1353 .id = UCLASS_ETH,
1354 .of_match = fecmxc_ids,
1355 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1356 .probe = fecmxc_probe,
1357 .remove = fecmxc_remove,
1358 .ops = &fecmxc_ops,
1359 .priv_auto_alloc_size = sizeof(struct fec_priv),
1360 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1361};
Troy Kisky2000c662012-02-07 14:08:47 +00001362#endif