blob: 910879ba3e4c08cd8df43842958e00411c632164 [file] [log] [blame]
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001/*
2 * (C) Copyright 2009 Ilya Yanok, Emcraft Systems Ltd <yanok@emcraft.com>
3 * (C) Copyright 2008,2009 Eric Jarrige <eric.jarrige@armadeus.org>
4 * (C) Copyright 2008 Armadeus Systems nc
5 * (C) Copyright 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6 * (C) Copyright 2007 Pengutronix, Juergen Beisert <j.beisert@pengutronix.de>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Ilya Yanok0b23fb32009-07-21 19:32:21 +04009 */
10
11#include <common.h>
Jagan Teki60752ca2016-12-06 00:00:49 +010012#include <dm.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040013#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060014#include <memalign.h>
Jagan Teki567173a2016-12-06 00:00:50 +010015#include <miiphy.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040016#include <net.h>
Jeroen Hofstee84f64c82014-10-08 22:57:40 +020017#include <netdev.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040018#include "fec_mxc.h"
19
Jagan Teki567173a2016-12-06 00:00:50 +010020#include <asm/io.h>
21#include <linux/errno.h>
22#include <linux/compiler.h>
23
Ilya Yanok0b23fb32009-07-21 19:32:21 +040024#include <asm/arch/clock.h>
25#include <asm/arch/imx-regs.h>
Peng Fanfbecbaa2015-08-12 17:46:51 +080026#include <asm/imx-common/sys_proto.h>
Ilya Yanok0b23fb32009-07-21 19:32:21 +040027
28DECLARE_GLOBAL_DATA_PTR;
29
Marek Vasutbc1ce152012-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 Estevamdb5b7f52014-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 Yanok0b23fb32009-07-21 19:32:21 +040044#ifndef CONFIG_MII
45#error "CONFIG_MII has to be defined!"
46#endif
47
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000048#ifndef CONFIG_FEC_XCV_TYPE
49#define CONFIG_FEC_XCV_TYPE MII100
Marek Vasut392b8502011-09-11 18:05:33 +000050#endif
51
Marek Vasutbe7e87e2011-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 Nelson5c1ad3e2012-03-15 18:33:25 +000056#ifdef CONFIG_MX28
57#define CONFIG_FEC_MXC_SWAP_PACKET
58#endif
59
60#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!"
Marek Vasutbe7e87e2011-11-08 23:18:10 +000070#endif
71
Ilya Yanok0b23fb32009-07-21 19:32:21 +040072#undef DEBUG
73
Eric Nelson5c1ad3e2012-03-15 18:33:25 +000074#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-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 Teki567173a2016-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 Yanok0b23fb32009-07-21 19:32:21 +040087{
Ilya Yanok0b23fb32009-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 Kisky13947f42012-02-07 14:08:47 +000091 int val;
Ilya Yanok0b23fb32009-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 Vasutd133b882011-09-11 18:05:34 +000097 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Teki567173a2016-12-06 00:00:50 +010098 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
99 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400100
101 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA |
Marek Vasutd133b882011-09-11 18:05:34 +0000102 phy | reg, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400103
Jagan Teki567173a2016-12-06 00:00:50 +0100104 /* wait for the related interrupt */
Graeme Russa60d1e52011-07-15 23:31:37 +0000105 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000106 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-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 Teki567173a2016-12-06 00:00:50 +0100113 /* clear mii interrupt bit */
Marek Vasutd133b882011-09-11 18:05:34 +0000114 writel(FEC_IEVENT_MII, &eth->ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400115
Jagan Teki567173a2016-12-06 00:00:50 +0100116 /* it's now safe to read the PHY's register */
Troy Kisky13947f42012-02-07 14:08:47 +0000117 val = (unsigned short)readl(&eth->mii_data);
Jagan Teki567173a2016-12-06 00:00:50 +0100118 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
119 regaddr, val);
Troy Kisky13947f42012-02-07 14:08:47 +0000120 return val;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400121}
122
Troy Kisky575c5cc2012-10-22 16:40:41 +0000123static void fec_mii_setspeed(struct ethernet_regs *eth)
Stefano Babic4294b242010-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ård843a3e52015-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 Babic4294b242010-02-01 14:51:30 +0100139 */
Måns Rullgård843a3e52015-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 Niebel6ba45cc2014-02-05 10:54:11 +0100143#ifdef FEC_QUIRK_ENET_MAC
144 speed--;
145#endif
Måns Rullgård843a3e52015-12-08 15:38:45 +0000146 writel(speed << 1 | hold << 8, &eth->mii_speed);
Troy Kisky575c5cc2012-10-22 16:40:41 +0000147 debug("%s: mii_speed %08x\n", __func__, readl(&eth->mii_speed));
Stefano Babic4294b242010-02-01 14:51:30 +0100148}
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400149
Jagan Teki567173a2016-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 Kisky13947f42012-02-07 14:08:47 +0000152{
Ilya Yanok0b23fb32009-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 Teki567173a2016-12-06 00:00:50 +0100157 reg = regaddr << FEC_MII_DATA_RA_SHIFT;
158 phy = phyaddr << FEC_MII_DATA_PA_SHIFT;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400159
160 writel(FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
Marek Vasutd133b882011-09-11 18:05:34 +0000161 FEC_MII_DATA_TA | phy | reg | data, &eth->mii_data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400162
Jagan Teki567173a2016-12-06 00:00:50 +0100163 /* wait for the MII interrupt */
Graeme Russa60d1e52011-07-15 23:31:37 +0000164 start = get_timer(0);
Marek Vasutd133b882011-09-11 18:05:34 +0000165 while (!(readl(&eth->ievent) & FEC_IEVENT_MII)) {
Ilya Yanok0b23fb32009-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 Teki567173a2016-12-06 00:00:50 +0100172 /* clear MII interrupt bit */
Marek Vasutd133b882011-09-11 18:05:34 +0000173 writel(FEC_IEVENT_MII, &eth->ievent);
Jagan Teki567173a2016-12-06 00:00:50 +0100174 debug("%s: phy: %02x reg:%02x val:%#x\n", __func__, phyaddr,
175 regaddr, data);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400176
177 return 0;
178}
179
Jagan Teki567173a2016-12-06 00:00:50 +0100180static int fec_phy_read(struct mii_dev *bus, int phyaddr, int dev_addr,
181 int regaddr)
Troy Kisky13947f42012-02-07 14:08:47 +0000182{
Jagan Teki567173a2016-12-06 00:00:50 +0100183 return fec_mdio_read(bus->priv, phyaddr, regaddr);
Troy Kisky13947f42012-02-07 14:08:47 +0000184}
185
Jagan Teki567173a2016-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 Kisky13947f42012-02-07 14:08:47 +0000188{
Jagan Teki567173a2016-12-06 00:00:50 +0100189 return fec_mdio_write(bus->priv, phyaddr, regaddr, data);
Troy Kisky13947f42012-02-07 14:08:47 +0000190}
191
192#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400193static int miiphy_restart_aneg(struct eth_device *dev)
194{
Stefano Babicb774fe92012-02-22 00:24:35 +0000195 int ret = 0;
196#if !defined(CONFIG_FEC_MXC_NO_ANEG)
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200197 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000198 struct ethernet_regs *eth = fec->bus->priv;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200199
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400200 /*
201 * Wake up from sleep if necessary
202 * Reset PHY, then delay 300ns
203 */
John Rigbycb17b922010-01-25 23:12:55 -0700204#ifdef CONFIG_MX27
Troy Kisky13947f42012-02-07 14:08:47 +0000205 fec_mdio_write(eth, fec->phy_id, MII_DCOUNTER, 0x00FF);
John Rigbycb17b922010-01-25 23:12:55 -0700206#endif
Troy Kisky13947f42012-02-07 14:08:47 +0000207 fec_mdio_write(eth, fec->phy_id, MII_BMCR, BMCR_RESET);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400208 udelay(1000);
209
Jagan Teki567173a2016-12-06 00:00:50 +0100210 /* Set the auto-negotiation advertisement register bits */
Troy Kisky13947f42012-02-07 14:08:47 +0000211 fec_mdio_write(eth, fec->phy_id, MII_ADVERTISE,
Jagan Teki567173a2016-12-06 00:00:50 +0100212 LPA_100FULL | LPA_100HALF | LPA_10FULL |
213 LPA_10HALF | PHY_ANLPAR_PSB_802_3);
Troy Kisky13947f42012-02-07 14:08:47 +0000214 fec_mdio_write(eth, fec->phy_id, MII_BMCR,
Jagan Teki567173a2016-12-06 00:00:50 +0100215 BMCR_ANENABLE | BMCR_ANRESTART);
Marek Vasut2e5f4422011-09-11 18:05:36 +0000216
217 if (fec->mii_postcall)
218 ret = fec->mii_postcall(fec->phy_id);
219
Stefano Babicb774fe92012-02-22 00:24:35 +0000220#endif
Marek Vasut2e5f4422011-09-11 18:05:36 +0000221 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400222}
223
Hannes Schmelzer07507012016-06-22 12:07:14 +0200224#ifndef CONFIG_FEC_FIXED_SPEED
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400225static int miiphy_wait_aneg(struct eth_device *dev)
226{
227 uint32_t start;
Troy Kisky13947f42012-02-07 14:08:47 +0000228 int status;
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200229 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Troy Kisky13947f42012-02-07 14:08:47 +0000230 struct ethernet_regs *eth = fec->bus->priv;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400231
Jagan Teki567173a2016-12-06 00:00:50 +0100232 /* Wait for AN completion */
Graeme Russa60d1e52011-07-15 23:31:37 +0000233 start = get_timer(0);
Ilya Yanok0b23fb32009-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 Kisky13947f42012-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 Teki567173a2016-12-06 00:00:50 +0100243 dev->name, status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400244 return -1;
245 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500246 } while (!(status & BMSR_LSTATUS));
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400247
248 return 0;
249}
Hannes Schmelzer07507012016-06-22 12:07:14 +0200250#endif /* CONFIG_FEC_FIXED_SPEED */
Troy Kisky13947f42012-02-07 14:08:47 +0000251#endif
252
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400253static int fec_rx_task_enable(struct fec_priv *fec)
254{
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000255 writel(FEC_R_DES_ACTIVE_RDAR, &fec->eth->r_des_active);
Ilya Yanok0b23fb32009-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 Vasutc0b5a3b2012-08-29 03:49:51 +0000266 writel(FEC_X_DES_ACTIVE_TDAR, &fec->eth->x_des_active);
Ilya Yanok0b23fb32009-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 Nelson5c1ad3e2012-03-15 18:33:25 +0000279 * @param[in] dsize desired size of each receive buffer
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400280 * @return 0 on success
281 *
Marek Vasut79e5f272013-10-12 20:36:25 +0200282 * Init all RX descriptors to default values.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400283 */
Marek Vasut79e5f272013-10-12 20:36:25 +0200284static void fec_rbd_init(struct fec_priv *fec, int count, int dsize)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400285{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000286 uint32_t size;
Marek Vasut79e5f272013-10-12 20:36:25 +0200287 uint8_t *data;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000288 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400289
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400290 /*
Marek Vasut79e5f272013-10-12 20:36:25 +0200291 * Reload the RX descriptors with default values and wipe
292 * the RX buffers.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400293 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000294 size = roundup(dsize, ARCH_DMA_MINALIGN);
295 for (i = 0; i < count; i++) {
Marek Vasut79e5f272013-10-12 20:36:25 +0200296 data = (uint8_t *)fec->rbd_base[i].data_pointer;
297 memset(data, 0, dsize);
298 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
299
300 fec->rbd_base[i].status = FEC_RBD_EMPTY;
301 fec->rbd_base[i].data_length = 0;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000302 }
303
304 /* Mark the last RBD to close the ring. */
Marek Vasut79e5f272013-10-12 20:36:25 +0200305 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400306 fec->rbd_index = 0;
307
Marek Vasut79e5f272013-10-12 20:36:25 +0200308 flush_dcache_range((unsigned)fec->rbd_base,
309 (unsigned)fec->rbd_base + size);
Ilya Yanok0b23fb32009-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{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000326 unsigned addr = (unsigned)fec->tbd_base;
327 unsigned size = roundup(2 * sizeof(struct fec_bd),
328 ARCH_DMA_MINALIGN);
Marek Vasut79e5f272013-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 Yanok0b23fb32009-07-21 19:32:21 +0400333 fec->tbd_index = 0;
Marek Vasut79e5f272013-10-12 20:36:25 +0200334 flush_dcache_range(addr, addr + size);
Ilya Yanok0b23fb32009-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 Teki567173a2016-12-06 00:00:50 +0100340 * @param[in] prbd buffer descriptor to mark free again
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400341 */
Jagan Teki567173a2016-12-06 00:00:50 +0100342static void fec_rbd_clean(int last, struct fec_bd *prbd)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400343{
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000344 unsigned short flags = FEC_RBD_EMPTY;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400345 if (last)
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000346 flags |= FEC_RBD_WRAP;
Jagan Teki567173a2016-12-06 00:00:50 +0100347 writew(flags, &prbd->status);
348 writew(0, &prbd->data_length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400349}
350
Jagan Tekif54183e2016-12-06 00:00:48 +0100351static int fec_get_hwaddr(int dev_id, unsigned char *mac)
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400352{
Fabio Estevambe252b62011-12-20 05:46:31 +0000353 imx_get_mac_from_fuse(dev_id, mac);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500354 return !is_valid_ethaddr(mac);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400355}
356
Jagan Teki60752ca2016-12-06 00:00:49 +0100357#ifdef CONFIG_DM_ETH
358static int fecmxc_set_hwaddr(struct udevice *dev)
359#else
Stefano Babic4294b242010-02-01 14:51:30 +0100360static int fec_set_hwaddr(struct eth_device *dev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100361#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400362{
Jagan Teki60752ca2016-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 Babic4294b242010-02-01 14:51:30 +0100368 uchar *mac = dev->enetaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400369 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100370#endif
Ilya Yanok0b23fb32009-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 Teki567173a2016-12-06 00:00:50 +0100377 /* Set physical address */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400378 writel((mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3],
Jagan Teki567173a2016-12-06 00:00:50 +0100379 &fec->eth->paddr1);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400380 writel((mac[4] << 24) + (mac[5] << 16) + 0x8808, &fec->eth->paddr2);
381
382 return 0;
383}
384
Jagan Teki567173a2016-12-06 00:00:50 +0100385/* Do initial configuration of the FEC registers */
Marek Vasuta5990b22012-05-01 11:09:41 +0000386static void fec_reg_setup(struct fec_priv *fec)
387{
388 uint32_t rcntrl;
389
Jagan Teki567173a2016-12-06 00:00:50 +0100390 /* Set interrupt mask register */
Marek Vasuta5990b22012-05-01 11:09:41 +0000391 writel(0x00000000, &fec->eth->imask);
392
Jagan Teki567173a2016-12-06 00:00:50 +0100393 /* Clear FEC-Lite interrupt event register(IEVENT) */
Marek Vasuta5990b22012-05-01 11:09:41 +0000394 writel(0xffffffff, &fec->eth->ievent);
395
Jagan Teki567173a2016-12-06 00:00:50 +0100396 /* Set FEC-Lite receive control register(R_CNTRL): */
Marek Vasuta5990b22012-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@advans9d2d9242012-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 Vasuta5990b22012-05-01 11:09:41 +0000403 rcntrl |= FEC_RCNTRL_RGMII;
404 else if (fec->xcv_type == RMII)
405 rcntrl |= FEC_RCNTRL_RMII;
Marek Vasuta5990b22012-05-01 11:09:41 +0000406
407 writel(rcntrl, &fec->eth->r_cntrl);
408}
409
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400410/**
411 * Start the FEC engine
412 * @param[in] dev Our device to handle
413 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100414#ifdef CONFIG_DM_ETH
415static int fec_open(struct udevice *dev)
416#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400417static int fec_open(struct eth_device *edev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100418#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400419{
Jagan Teki60752ca2016-12-06 00:00:49 +0100420#ifdef CONFIG_DM_ETH
421 struct fec_priv *fec = dev_get_priv(dev);
422#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400423 struct fec_priv *fec = (struct fec_priv *)edev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100424#endif
Troy Kisky28774cb2012-02-07 14:08:46 +0000425 int speed;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000426 uint32_t addr, size;
427 int i;
Ilya Yanok0b23fb32009-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 Nelson5c1ad3e2012-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);
442 addr = (uint32_t)fec->rbd_base;
443 flush_dcache_range(addr, addr + size);
444
Troy Kisky28774cb2012-02-07 14:08:46 +0000445#ifdef FEC_QUIRK_ENET_MAC
Jason Liu2ef2b952011-12-16 05:17:07 +0000446 /* Enable ENET HW endian SWAP */
447 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_DBSWAP,
Jagan Teki567173a2016-12-06 00:00:50 +0100448 &fec->eth->ecntrl);
Jason Liu2ef2b952011-12-16 05:17:07 +0000449 /* Enable ENET store and forward mode */
450 writel(readl(&fec->eth->x_wmrk) | FEC_X_WMRK_STRFWD,
Jagan Teki567173a2016-12-06 00:00:50 +0100451 &fec->eth->x_wmrk);
Jason Liu2ef2b952011-12-16 05:17:07 +0000452#endif
Jagan Teki567173a2016-12-06 00:00:50 +0100453 /* Enable FEC-Lite controller */
John Rigbycb17b922010-01-25 23:12:55 -0700454 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_ETHER_EN,
Jagan Teki567173a2016-12-06 00:00:50 +0100455 &fec->eth->ecntrl);
456
Fabio Estevam7df51fd2013-09-13 00:36:27 -0300457#if defined(CONFIG_MX25) || defined(CONFIG_MX53) || defined(CONFIG_MX6SL)
John Rigby740d6ae2010-01-25 23:12:57 -0700458 udelay(100);
John Rigby740d6ae2010-01-25 23:12:57 -0700459
Jagan Teki567173a2016-12-06 00:00:50 +0100460 /* setup the MII gasket for RMII mode */
John Rigby740d6ae2010-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 Yanok0b23fb32009-07-21 19:32:21 +0400483
Troy Kisky13947f42012-02-07 14:08:47 +0000484#ifdef CONFIG_PHYLIB
Troy Kisky4dc27ee2012-10-22 16:40:45 +0000485 {
Troy Kisky13947f42012-02-07 14:08:47 +0000486 /* Start up the PHY */
Timur Tabi11af8d62012-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 Kisky13947f42012-02-07 14:08:47 +0000494 speed = fec->phydev->speed;
Troy Kisky13947f42012-02-07 14:08:47 +0000495 }
Hannes Schmelzer07507012016-06-22 12:07:14 +0200496#elif CONFIG_FEC_FIXED_SPEED
497 speed = CONFIG_FEC_FIXED_SPEED;
Troy Kisky13947f42012-02-07 14:08:47 +0000498#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400499 miiphy_wait_aneg(edev);
Troy Kisky28774cb2012-02-07 14:08:46 +0000500 speed = miiphy_speed(edev->name, fec->phy_id);
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200501 miiphy_duplex(edev->name, fec->phy_id);
Troy Kisky13947f42012-02-07 14:08:47 +0000502#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400503
Troy Kisky28774cb2012-02-07 14:08:46 +0000504#ifdef FEC_QUIRK_ENET_MAC
505 {
506 u32 ecr = readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_SPEED;
Alison Wangbcb6e902013-05-27 22:55:43 +0000507 u32 rcr = readl(&fec->eth->r_cntrl) & ~FEC_RCNTRL_RMII_10T;
Troy Kisky28774cb2012-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 Teki567173a2016-12-06 00:00:50 +0100518 /* Enable SmartDMA receive task */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400519 fec_rx_task_enable(fec);
520
521 udelay(100000);
522 return 0;
523}
524
Jagan Teki60752ca2016-12-06 00:00:49 +0100525#ifdef CONFIG_DM_ETH
526static int fecmxc_init(struct udevice *dev)
527#else
Jagan Teki567173a2016-12-06 00:00:50 +0100528static int fec_init(struct eth_device *dev, bd_t *bd)
Jagan Teki60752ca2016-12-06 00:00:49 +0100529#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400530{
Jagan Teki60752ca2016-12-06 00:00:49 +0100531#ifdef CONFIG_DM_ETH
532 struct fec_priv *fec = dev_get_priv(dev);
533#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400534 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100535#endif
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200536 uint32_t mib_ptr = (uint32_t)&fec->eth->rmon_t_drop;
Marek Vasut79e5f272013-10-12 20:36:25 +0200537 int i;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400538
John Rigbye9319f12010-10-13 14:31:08 -0600539 /* Initialize MAC address */
Jagan Teki60752ca2016-12-06 00:00:49 +0100540#ifdef CONFIG_DM_ETH
541 fecmxc_set_hwaddr(dev);
542#else
John Rigbye9319f12010-10-13 14:31:08 -0600543 fec_set_hwaddr(dev);
Jagan Teki60752ca2016-12-06 00:00:49 +0100544#endif
John Rigbye9319f12010-10-13 14:31:08 -0600545
Jagan Teki567173a2016-12-06 00:00:50 +0100546 /* Setup transmit descriptors, there are two in total. */
Marek Vasut79e5f272013-10-12 20:36:25 +0200547 fec_tbd_init(fec);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400548
Marek Vasut79e5f272013-10-12 20:36:25 +0200549 /* Setup receive descriptors. */
550 fec_rbd_init(fec, FEC_RBD_NUM, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400551
Marek Vasuta5990b22012-05-01 11:09:41 +0000552 fec_reg_setup(fec);
Marek Vasut9eb37702011-09-11 18:05:31 +0000553
benoit.thebaudeau@advansf41471e2012-07-19 02:12:58 +0000554 if (fec->xcv_type != SEVENWIRE)
Troy Kisky575c5cc2012-10-22 16:40:41 +0000555 fec_mii_setspeed(fec->bus->priv);
Marek Vasut9eb37702011-09-11 18:05:31 +0000556
Jagan Teki567173a2016-12-06 00:00:50 +0100557 /* Set Opcode/Pause Duration Register */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400558 writel(0x00010020, &fec->eth->op_pause); /* FIXME 0xffff0020; */
559 writel(0x2, &fec->eth->x_wmrk);
Jagan Teki567173a2016-12-06 00:00:50 +0100560
561 /* Set multicast address filter */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400562 writel(0x00000000, &fec->eth->gaddr1);
563 writel(0x00000000, &fec->eth->gaddr2);
564
Peng Fanfbecbaa2015-08-12 17:46:51 +0800565 /* Do not access reserved register for i.MX6UL */
Peng Fan87f99892016-05-23 18:36:04 +0800566 if (!is_mx6ul()) {
Peng Fanfbecbaa2015-08-12 17:46:51 +0800567 /* clear MIB RAM */
568 for (i = mib_ptr; i <= mib_ptr + 0xfc; i += 4)
569 writel(0, i);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400570
Peng Fanfbecbaa2015-08-12 17:46:51 +0800571 /* FIFO receive start register */
572 writel(0x520, &fec->eth->r_fstart);
573 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400574
575 /* size and address of each buffer */
576 writel(FEC_MAX_PKT_SIZE, &fec->eth->emrbr);
577 writel((uint32_t)fec->tbd_base, &fec->eth->etdsr);
578 writel((uint32_t)fec->rbd_base, &fec->eth->erdsr);
579
Troy Kisky13947f42012-02-07 14:08:47 +0000580#ifndef CONFIG_PHYLIB
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400581 if (fec->xcv_type != SEVENWIRE)
582 miiphy_restart_aneg(dev);
Troy Kisky13947f42012-02-07 14:08:47 +0000583#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400584 fec_open(dev);
585 return 0;
586}
587
588/**
589 * Halt the FEC engine
590 * @param[in] dev Our device to handle
591 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100592#ifdef CONFIG_DM_ETH
593static void fecmxc_halt(struct udevice *dev)
594#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400595static void fec_halt(struct eth_device *dev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100596#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400597{
Jagan Teki60752ca2016-12-06 00:00:49 +0100598#ifdef CONFIG_DM_ETH
599 struct fec_priv *fec = dev_get_priv(dev);
600#else
Marek Vasut9e27e9d2011-09-16 01:13:47 +0200601 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100602#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400603 int counter = 0xffff;
604
Jagan Teki567173a2016-12-06 00:00:50 +0100605 /* issue graceful stop command to the FEC transmitter if necessary */
John Rigbycb17b922010-01-25 23:12:55 -0700606 writel(FEC_TCNTRL_GTS | readl(&fec->eth->x_cntrl),
Jagan Teki567173a2016-12-06 00:00:50 +0100607 &fec->eth->x_cntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400608
609 debug("eth_halt: wait for stop regs\n");
Jagan Teki567173a2016-12-06 00:00:50 +0100610 /* wait for graceful stop to register */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400611 while ((counter--) && (!(readl(&fec->eth->ievent) & FEC_IEVENT_GRA)))
John Rigbycb17b922010-01-25 23:12:55 -0700612 udelay(1);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400613
Jagan Teki567173a2016-12-06 00:00:50 +0100614 /* Disable SmartDMA tasks */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400615 fec_tx_task_disable(fec);
616 fec_rx_task_disable(fec);
617
618 /*
619 * Disable the Ethernet Controller
620 * Note: this will also reset the BD index counter!
621 */
John Rigby740d6ae2010-01-25 23:12:57 -0700622 writel(readl(&fec->eth->ecntrl) & ~FEC_ECNTRL_ETHER_EN,
Jagan Teki567173a2016-12-06 00:00:50 +0100623 &fec->eth->ecntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400624 fec->rbd_index = 0;
625 fec->tbd_index = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400626 debug("eth_halt: done\n");
627}
628
629/**
630 * Transmit one frame
631 * @param[in] dev Our ethernet device to handle
632 * @param[in] packet Pointer to the data to be transmitted
633 * @param[in] length Data count in bytes
634 * @return 0 on success
635 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100636#ifdef CONFIG_DM_ETH
637static int fecmxc_send(struct udevice *dev, void *packet, int length)
638#else
Joe Hershberger442dac42012-05-21 14:45:27 +0000639static int fec_send(struct eth_device *dev, void *packet, int length)
Jagan Teki60752ca2016-12-06 00:00:49 +0100640#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400641{
642 unsigned int status;
Marek Vasutefe24d22012-08-26 10:19:21 +0000643 uint32_t size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000644 uint32_t addr;
Marek Vasutbc1ce152012-08-29 03:49:49 +0000645 int timeout = FEC_XFER_TIMEOUT;
646 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400647
648 /*
649 * This routine transmits one frame. This routine only accepts
650 * 6-byte Ethernet addresses.
651 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100652#ifdef CONFIG_DM_ETH
653 struct fec_priv *fec = dev_get_priv(dev);
654#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400655 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100656#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400657
658 /*
659 * Check for valid length of data.
660 */
661 if ((length > 1500) || (length <= 0)) {
Stefano Babic4294b242010-02-01 14:51:30 +0100662 printf("Payload (%d) too large\n", length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400663 return -1;
664 }
665
666 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000667 * Setup the transmit buffer. We are always using the first buffer for
668 * transmission, the second will be empty and only used to stop the DMA
669 * engine. We also flush the packet to RAM here to avoid cache trouble.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400670 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000671#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000672 swap_packet((uint32_t *)packet, length);
673#endif
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000674
675 addr = (uint32_t)packet;
Marek Vasutefe24d22012-08-26 10:19:21 +0000676 end = roundup(addr + length, ARCH_DMA_MINALIGN);
677 addr &= ~(ARCH_DMA_MINALIGN - 1);
678 flush_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000679
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400680 writew(length, &fec->tbd_base[fec->tbd_index].data_length);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000681 writel(addr, &fec->tbd_base[fec->tbd_index].data_pointer);
682
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400683 /*
684 * update BD's status now
685 * This block:
686 * - is always the last in a chain (means no chain)
687 * - should transmitt the CRC
688 * - might be the last BD in the list, so the address counter should
689 * wrap (-> keep the WRAP flag)
690 */
691 status = readw(&fec->tbd_base[fec->tbd_index].status) & FEC_TBD_WRAP;
692 status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
693 writew(status, &fec->tbd_base[fec->tbd_index].status);
694
695 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000696 * Flush data cache. This code flushes both TX descriptors to RAM.
697 * After this code, the descriptors will be safely in RAM and we
698 * can start DMA.
699 */
700 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
701 addr = (uint32_t)fec->tbd_base;
702 flush_dcache_range(addr, addr + size);
703
704 /*
Marek Vasutab94cd42013-07-12 01:03:04 +0200705 * Below we read the DMA descriptor's last four bytes back from the
706 * DRAM. This is important in order to make sure that all WRITE
707 * operations on the bus that were triggered by previous cache FLUSH
708 * have completed.
709 *
710 * Otherwise, on MX28, it is possible to observe a corruption of the
711 * DMA descriptors. Please refer to schematic "Figure 1-2" in MX28RM
712 * for the bus structure of MX28. The scenario is as follows:
713 *
714 * 1) ARM core triggers a series of WRITEs on the AHB_ARB2 bus going
715 * to DRAM due to flush_dcache_range()
716 * 2) ARM core writes the FEC registers via AHB_ARB2
717 * 3) FEC DMA starts reading/writing from/to DRAM via AHB_ARB3
718 *
719 * Note that 2) does sometimes finish before 1) due to reordering of
720 * WRITE accesses on the AHB bus, therefore triggering 3) before the
721 * DMA descriptor is fully written into DRAM. This results in occasional
722 * corruption of the DMA descriptor.
723 */
724 readl(addr + size - 4);
725
Jagan Teki567173a2016-12-06 00:00:50 +0100726 /* Enable SmartDMA transmit task */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400727 fec_tx_task_enable(fec);
728
729 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000730 * Wait until frame is sent. On each turn of the wait cycle, we must
731 * invalidate data cache to see what's really in RAM. Also, we need
732 * barrier here.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400733 */
Marek Vasut67449092012-08-29 03:49:50 +0000734 while (--timeout) {
Marek Vasutc0b5a3b2012-08-29 03:49:51 +0000735 if (!(readl(&fec->eth->x_des_active) & FEC_X_DES_ACTIVE_TDAR))
Marek Vasutbc1ce152012-08-29 03:49:49 +0000736 break;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400737 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000738
Fabio Estevamf5992882014-08-25 13:34:17 -0300739 if (!timeout) {
740 ret = -EINVAL;
741 goto out;
742 }
743
744 /*
745 * The TDAR bit is cleared when the descriptors are all out from TX
746 * but on mx6solox we noticed that the READY bit is still not cleared
747 * right after TDAR.
748 * These are two distinct signals, and in IC simulation, we found that
749 * TDAR always gets cleared prior than the READY bit of last BD becomes
750 * cleared.
751 * In mx6solox, we use a later version of FEC IP. It looks like that
752 * this intrinsic behaviour of TDAR bit has changed in this newer FEC
753 * version.
754 *
755 * Fix this by polling the READY bit of BD after the TDAR polling,
756 * which covers the mx6solox case and does not harm the other SoCs.
757 */
758 timeout = FEC_XFER_TIMEOUT;
759 while (--timeout) {
760 invalidate_dcache_range(addr, addr + size);
761 if (!(readw(&fec->tbd_base[fec->tbd_index].status) &
762 FEC_TBD_READY))
763 break;
764 }
765
Marek Vasut67449092012-08-29 03:49:50 +0000766 if (!timeout)
767 ret = -EINVAL;
768
Fabio Estevamf5992882014-08-25 13:34:17 -0300769out:
Marek Vasut67449092012-08-29 03:49:50 +0000770 debug("fec_send: status 0x%x index %d ret %i\n",
Jagan Teki567173a2016-12-06 00:00:50 +0100771 readw(&fec->tbd_base[fec->tbd_index].status),
772 fec->tbd_index, ret);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400773 /* for next transmission use the other buffer */
774 if (fec->tbd_index)
775 fec->tbd_index = 0;
776 else
777 fec->tbd_index = 1;
778
Marek Vasutbc1ce152012-08-29 03:49:49 +0000779 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400780}
781
782/**
783 * Pull one frame from the card
784 * @param[in] dev Our ethernet device to handle
785 * @return Length of packet read
786 */
Jagan Teki60752ca2016-12-06 00:00:49 +0100787#ifdef CONFIG_DM_ETH
788static int fecmxc_recv(struct udevice *dev, int flags, uchar **packetp)
789#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400790static int fec_recv(struct eth_device *dev)
Jagan Teki60752ca2016-12-06 00:00:49 +0100791#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400792{
Jagan Teki60752ca2016-12-06 00:00:49 +0100793#ifdef CONFIG_DM_ETH
794 struct fec_priv *fec = dev_get_priv(dev);
795#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400796 struct fec_priv *fec = (struct fec_priv *)dev->priv;
Jagan Teki60752ca2016-12-06 00:00:49 +0100797#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400798 struct fec_bd *rbd = &fec->rbd_base[fec->rbd_index];
799 unsigned long ievent;
800 int frame_length, len = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400801 uint16_t bd_status;
Marek Vasutefe24d22012-08-26 10:19:21 +0000802 uint32_t addr, size, end;
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000803 int i;
Fabio Estevamfd37f192013-09-17 23:13:10 -0300804 ALLOC_CACHE_ALIGN_BUFFER(uchar, buff, FEC_MAX_PKT_SIZE);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400805
Jagan Teki567173a2016-12-06 00:00:50 +0100806 /* Check if any critical events have happened */
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400807 ievent = readl(&fec->eth->ievent);
808 writel(ievent, &fec->eth->ievent);
Marek Vasuteda959f2011-10-24 23:40:03 +0000809 debug("fec_recv: ievent 0x%lx\n", ievent);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400810 if (ievent & FEC_IEVENT_BABR) {
Jagan Teki60752ca2016-12-06 00:00:49 +0100811#ifdef CONFIG_DM_ETH
812 fecmxc_halt(dev);
813 fecmxc_init(dev);
814#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400815 fec_halt(dev);
816 fec_init(dev, fec->bd);
Jagan Teki60752ca2016-12-06 00:00:49 +0100817#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400818 printf("some error: 0x%08lx\n", ievent);
819 return 0;
820 }
821 if (ievent & FEC_IEVENT_HBERR) {
822 /* Heartbeat error */
823 writel(0x00000001 | readl(&fec->eth->x_cntrl),
Jagan Teki567173a2016-12-06 00:00:50 +0100824 &fec->eth->x_cntrl);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400825 }
826 if (ievent & FEC_IEVENT_GRA) {
827 /* Graceful stop complete */
828 if (readl(&fec->eth->x_cntrl) & 0x00000001) {
Jagan Teki60752ca2016-12-06 00:00:49 +0100829#ifdef CONFIG_DM_ETH
830 fecmxc_halt(dev);
831#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400832 fec_halt(dev);
Jagan Teki60752ca2016-12-06 00:00:49 +0100833#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400834 writel(~0x00000001 & readl(&fec->eth->x_cntrl),
Jagan Teki567173a2016-12-06 00:00:50 +0100835 &fec->eth->x_cntrl);
Jagan Teki60752ca2016-12-06 00:00:49 +0100836#ifdef CONFIG_DM_ETH
837 fecmxc_init(dev);
838#else
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400839 fec_init(dev, fec->bd);
Jagan Teki60752ca2016-12-06 00:00:49 +0100840#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400841 }
842 }
843
844 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000845 * Read the buffer status. Before the status can be read, the data cache
846 * must be invalidated, because the data in RAM might have been changed
847 * by DMA. The descriptors are properly aligned to cachelines so there's
848 * no need to worry they'd overlap.
849 *
850 * WARNING: By invalidating the descriptor here, we also invalidate
851 * the descriptors surrounding this one. Therefore we can NOT change the
852 * contents of this descriptor nor the surrounding ones. The problem is
853 * that in order to mark the descriptor as processed, we need to change
854 * the descriptor. The solution is to mark the whole cache line when all
855 * descriptors in the cache line are processed.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400856 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000857 addr = (uint32_t)rbd;
858 addr &= ~(ARCH_DMA_MINALIGN - 1);
859 size = roundup(sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
860 invalidate_dcache_range(addr, addr + size);
861
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400862 bd_status = readw(&rbd->status);
863 debug("fec_recv: status 0x%x\n", bd_status);
864
865 if (!(bd_status & FEC_RBD_EMPTY)) {
866 if ((bd_status & FEC_RBD_LAST) && !(bd_status & FEC_RBD_ERR) &&
Jagan Teki567173a2016-12-06 00:00:50 +0100867 ((readw(&rbd->data_length) - 4) > 14)) {
868 /* Get buffer address and size */
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200869 addr = readl(&rbd->data_pointer);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400870 frame_length = readw(&rbd->data_length) - 4;
Jagan Teki567173a2016-12-06 00:00:50 +0100871 /* Invalidate data cache over the buffer */
Marek Vasutefe24d22012-08-26 10:19:21 +0000872 end = roundup(addr + frame_length, ARCH_DMA_MINALIGN);
873 addr &= ~(ARCH_DMA_MINALIGN - 1);
874 invalidate_dcache_range(addr, end);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000875
Jagan Teki567173a2016-12-06 00:00:50 +0100876 /* Fill the buffer and pass it to upper layers */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000877#ifdef CONFIG_FEC_MXC_SWAP_PACKET
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200878 swap_packet((uint32_t *)addr, frame_length);
Marek Vasutbe7e87e2011-11-08 23:18:10 +0000879#endif
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200880 memcpy(buff, (char *)addr, frame_length);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500881 net_process_received_packet(buff, frame_length);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400882 len = frame_length;
883 } else {
884 if (bd_status & FEC_RBD_ERR)
Albert ARIBAUD \(3ADEV\)b1895842015-06-19 14:18:27 +0200885 printf("error frame: 0x%08x 0x%08x\n",
886 addr, bd_status);
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400887 }
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000888
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400889 /*
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000890 * Free the current buffer, restart the engine and move forward
891 * to the next buffer. Here we check if the whole cacheline of
892 * descriptors was already processed and if so, we mark it free
893 * as whole.
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400894 */
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000895 size = RXDESC_PER_CACHELINE - 1;
896 if ((fec->rbd_index & size) == size) {
897 i = fec->rbd_index - size;
898 addr = (uint32_t)&fec->rbd_base[i];
899 for (; i <= fec->rbd_index ; i++) {
900 fec_rbd_clean(i == (FEC_RBD_NUM - 1),
901 &fec->rbd_base[i]);
902 }
903 flush_dcache_range(addr,
Jagan Teki567173a2016-12-06 00:00:50 +0100904 addr + ARCH_DMA_MINALIGN);
Eric Nelson5c1ad3e2012-03-15 18:33:25 +0000905 }
906
Ilya Yanok0b23fb32009-07-21 19:32:21 +0400907 fec_rx_task_enable(fec);
908 fec->rbd_index = (fec->rbd_index + 1) % FEC_RBD_NUM;
909 }
910 debug("fec_recv: stop\n");
911
912 return len;
913}
914
Troy Kiskyef8e3a32012-10-22 16:40:44 +0000915static void fec_set_dev_name(char *dest, int dev_id)
916{
917 sprintf(dest, (dev_id == -1) ? "FEC" : "FEC%i", dev_id);
918}
919
Marek Vasut79e5f272013-10-12 20:36:25 +0200920static int fec_alloc_descs(struct fec_priv *fec)
921{
922 unsigned int size;
923 int i;
924 uint8_t *data;
925
926 /* Allocate TX descriptors. */
927 size = roundup(2 * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
928 fec->tbd_base = memalign(ARCH_DMA_MINALIGN, size);
929 if (!fec->tbd_base)
930 goto err_tx;
931
932 /* Allocate RX descriptors. */
933 size = roundup(FEC_RBD_NUM * sizeof(struct fec_bd), ARCH_DMA_MINALIGN);
934 fec->rbd_base = memalign(ARCH_DMA_MINALIGN, size);
935 if (!fec->rbd_base)
936 goto err_rx;
937
938 memset(fec->rbd_base, 0, size);
939
940 /* Allocate RX buffers. */
941
942 /* Maximum RX buffer size. */
Fabio Estevamdb5b7f52014-08-25 13:34:16 -0300943 size = roundup(FEC_MAX_PKT_SIZE, FEC_DMA_RX_MINALIGN);
Marek Vasut79e5f272013-10-12 20:36:25 +0200944 for (i = 0; i < FEC_RBD_NUM; i++) {
Fabio Estevamdb5b7f52014-08-25 13:34:16 -0300945 data = memalign(FEC_DMA_RX_MINALIGN, size);
Marek Vasut79e5f272013-10-12 20:36:25 +0200946 if (!data) {
947 printf("%s: error allocating rxbuf %d\n", __func__, i);
948 goto err_ring;
949 }
950
951 memset(data, 0, size);
952
953 fec->rbd_base[i].data_pointer = (uint32_t)data;
954 fec->rbd_base[i].status = FEC_RBD_EMPTY;
955 fec->rbd_base[i].data_length = 0;
956 /* Flush the buffer to memory. */
957 flush_dcache_range((uint32_t)data, (uint32_t)data + size);
958 }
959
960 /* Mark the last RBD to close the ring. */
961 fec->rbd_base[i - 1].status = FEC_RBD_WRAP | FEC_RBD_EMPTY;
962
963 fec->rbd_index = 0;
964 fec->tbd_index = 0;
965
966 return 0;
967
968err_ring:
969 for (; i >= 0; i--)
970 free((void *)fec->rbd_base[i].data_pointer);
971 free(fec->rbd_base);
972err_rx:
973 free(fec->tbd_base);
974err_tx:
975 return -ENOMEM;
976}
977
978static void fec_free_descs(struct fec_priv *fec)
979{
980 int i;
981
982 for (i = 0; i < FEC_RBD_NUM; i++)
983 free((void *)fec->rbd_base[i].data_pointer);
984 free(fec->rbd_base);
985 free(fec->tbd_base);
986}
987
Jagan Teki60752ca2016-12-06 00:00:49 +0100988struct mii_dev *fec_get_miibus(uint32_t base_addr, int dev_id)
989{
990 struct ethernet_regs *eth = (struct ethernet_regs *)base_addr;
991 struct mii_dev *bus;
992 int ret;
993
994 bus = mdio_alloc();
995 if (!bus) {
996 printf("mdio_alloc failed\n");
997 return NULL;
998 }
999 bus->read = fec_phy_read;
1000 bus->write = fec_phy_write;
1001 bus->priv = eth;
1002 fec_set_dev_name(bus->name, dev_id);
1003
1004 ret = mdio_register(bus);
1005 if (ret) {
1006 printf("mdio_register failed\n");
1007 free(bus);
1008 return NULL;
1009 }
1010 fec_mii_setspeed(eth);
1011 return bus;
1012}
1013
1014#ifndef CONFIG_DM_ETH
Troy Kiskyfe428b92012-10-22 16:40:46 +00001015#ifdef CONFIG_PHYLIB
1016int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1017 struct mii_dev *bus, struct phy_device *phydev)
1018#else
1019static int fec_probe(bd_t *bd, int dev_id, uint32_t base_addr,
1020 struct mii_dev *bus, int phy_id)
1021#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001022{
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001023 struct eth_device *edev;
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001024 struct fec_priv *fec;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001025 unsigned char ethaddr[6];
Marek Vasute382fb42011-09-11 18:05:37 +00001026 uint32_t start;
1027 int ret = 0;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001028
1029 /* create and fill edev struct */
1030 edev = (struct eth_device *)malloc(sizeof(struct eth_device));
1031 if (!edev) {
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001032 puts("fec_mxc: not enough malloc memory for eth_device\n");
Marek Vasute382fb42011-09-11 18:05:37 +00001033 ret = -ENOMEM;
1034 goto err1;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001035 }
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001036
1037 fec = (struct fec_priv *)malloc(sizeof(struct fec_priv));
1038 if (!fec) {
1039 puts("fec_mxc: not enough malloc memory for fec_priv\n");
Marek Vasute382fb42011-09-11 18:05:37 +00001040 ret = -ENOMEM;
1041 goto err2;
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001042 }
1043
Nobuhiro Iwamatsude0b9572010-10-19 14:03:42 +09001044 memset(edev, 0, sizeof(*edev));
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001045 memset(fec, 0, sizeof(*fec));
1046
Marek Vasut79e5f272013-10-12 20:36:25 +02001047 ret = fec_alloc_descs(fec);
1048 if (ret)
1049 goto err3;
1050
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001051 edev->priv = fec;
1052 edev->init = fec_init;
1053 edev->send = fec_send;
1054 edev->recv = fec_recv;
1055 edev->halt = fec_halt;
Heiko Schocherfb57ec92010-04-27 07:43:52 +02001056 edev->write_hwaddr = fec_set_hwaddr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001057
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001058 fec->eth = (struct ethernet_regs *)base_addr;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001059 fec->bd = bd;
1060
Marek Vasut392b8502011-09-11 18:05:33 +00001061 fec->xcv_type = CONFIG_FEC_XCV_TYPE;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001062
1063 /* Reset chip. */
John Rigbycb17b922010-01-25 23:12:55 -07001064 writel(readl(&fec->eth->ecntrl) | FEC_ECNTRL_RESET, &fec->eth->ecntrl);
Marek Vasute382fb42011-09-11 18:05:37 +00001065 start = get_timer(0);
1066 while (readl(&fec->eth->ecntrl) & FEC_ECNTRL_RESET) {
1067 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
Vagrant Cascadian3450a852016-10-23 20:45:19 -07001068 printf("FEC MXC: Timeout resetting chip\n");
Marek Vasut79e5f272013-10-12 20:36:25 +02001069 goto err4;
Marek Vasute382fb42011-09-11 18:05:37 +00001070 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001071 udelay(10);
Marek Vasute382fb42011-09-11 18:05:37 +00001072 }
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001073
Marek Vasuta5990b22012-05-01 11:09:41 +00001074 fec_reg_setup(fec);
Troy Kiskyef8e3a32012-10-22 16:40:44 +00001075 fec_set_dev_name(edev->name, dev_id);
1076 fec->dev_id = (dev_id == -1) ? 0 : dev_id;
Troy Kisky13947f42012-02-07 14:08:47 +00001077 fec->bus = bus;
Troy Kiskyfe428b92012-10-22 16:40:46 +00001078 fec_mii_setspeed(bus->priv);
1079#ifdef CONFIG_PHYLIB
1080 fec->phydev = phydev;
1081 phy_connect_dev(phydev, edev);
1082 /* Configure phy */
1083 phy_config(phydev);
1084#else
1085 fec->phy_id = phy_id;
1086#endif
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001087 eth_register(edev);
1088
Jagan Tekif54183e2016-12-06 00:00:48 +01001089 if (fec_get_hwaddr(dev_id, ethaddr) == 0) {
Fabio Estevambe252b62011-12-20 05:46:31 +00001090 debug("got MAC%d address from fuse: %pM\n", dev_id, ethaddr);
Stefano Babic4294b242010-02-01 14:51:30 +01001091 memcpy(edev->enetaddr, ethaddr, 6);
Eric Nelsonddb636b2013-08-02 10:37:00 -07001092 if (!getenv("ethaddr"))
1093 eth_setenv_enetaddr("ethaddr", ethaddr);
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001094 }
Marek Vasute382fb42011-09-11 18:05:37 +00001095 return ret;
Marek Vasut79e5f272013-10-12 20:36:25 +02001096err4:
1097 fec_free_descs(fec);
Marek Vasute382fb42011-09-11 18:05:37 +00001098err3:
1099 free(fec);
1100err2:
1101 free(edev);
1102err1:
1103 return ret;
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001104}
1105
Troy Kiskyeef24482012-10-22 16:40:42 +00001106int fecmxc_initialize_multi(bd_t *bd, int dev_id, int phy_id, uint32_t addr)
1107{
Troy Kiskyfe428b92012-10-22 16:40:46 +00001108 uint32_t base_mii;
1109 struct mii_dev *bus = NULL;
1110#ifdef CONFIG_PHYLIB
1111 struct phy_device *phydev = NULL;
1112#endif
1113 int ret;
1114
1115#ifdef CONFIG_MX28
1116 /*
1117 * The i.MX28 has two ethernet interfaces, but they are not equal.
1118 * Only the first one can access the MDIO bus.
1119 */
1120 base_mii = MXS_ENET0_BASE;
1121#else
1122 base_mii = addr;
1123#endif
Troy Kiskyeef24482012-10-22 16:40:42 +00001124 debug("eth_init: fec_probe(bd, %i, %i) @ %08x\n", dev_id, phy_id, addr);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001125 bus = fec_get_miibus(base_mii, dev_id);
1126 if (!bus)
1127 return -ENOMEM;
1128#ifdef CONFIG_PHYLIB
1129 phydev = phy_find_by_mask(bus, 1 << phy_id, PHY_INTERFACE_MODE_RGMII);
1130 if (!phydev) {
Måns Rullgård845a57b2015-12-08 15:38:46 +00001131 mdio_unregister(bus);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001132 free(bus);
1133 return -ENOMEM;
1134 }
1135 ret = fec_probe(bd, dev_id, addr, bus, phydev);
1136#else
1137 ret = fec_probe(bd, dev_id, addr, bus, phy_id);
1138#endif
1139 if (ret) {
1140#ifdef CONFIG_PHYLIB
1141 free(phydev);
1142#endif
Måns Rullgård845a57b2015-12-08 15:38:46 +00001143 mdio_unregister(bus);
Troy Kiskyfe428b92012-10-22 16:40:46 +00001144 free(bus);
1145 }
1146 return ret;
Troy Kiskyeef24482012-10-22 16:40:42 +00001147}
1148
Troy Kisky09439c32012-10-22 16:40:40 +00001149#ifdef CONFIG_FEC_MXC_PHYADDR
Ilya Yanok0b23fb32009-07-21 19:32:21 +04001150int fecmxc_initialize(bd_t *bd)
1151{
Troy Kiskyeef24482012-10-22 16:40:42 +00001152 return fecmxc_initialize_multi(bd, -1, CONFIG_FEC_MXC_PHYADDR,
1153 IMX_FEC_BASE);
Marek Vasut9e27e9d2011-09-16 01:13:47 +02001154}
1155#endif
1156
Troy Kisky13947f42012-02-07 14:08:47 +00001157#ifndef CONFIG_PHYLIB
Marek Vasut2e5f4422011-09-11 18:05:36 +00001158int fecmxc_register_mii_postcall(struct eth_device *dev, int (*cb)(int))
1159{
1160 struct fec_priv *fec = (struct fec_priv *)dev->priv;
1161 fec->mii_postcall = cb;
1162 return 0;
1163}
Troy Kisky13947f42012-02-07 14:08:47 +00001164#endif
Jagan Teki60752ca2016-12-06 00:00:49 +01001165
1166#else
1167
Jagan Teki1ed25702016-12-06 00:00:51 +01001168static int fecmxc_read_rom_hwaddr(struct udevice *dev)
1169{
1170 struct fec_priv *priv = dev_get_priv(dev);
1171 struct eth_pdata *pdata = dev_get_platdata(dev);
1172
1173 return fec_get_hwaddr(priv->dev_id, pdata->enetaddr);
1174}
1175
Jagan Teki60752ca2016-12-06 00:00:49 +01001176static const struct eth_ops fecmxc_ops = {
1177 .start = fecmxc_init,
1178 .send = fecmxc_send,
1179 .recv = fecmxc_recv,
1180 .stop = fecmxc_halt,
1181 .write_hwaddr = fecmxc_set_hwaddr,
Jagan Teki1ed25702016-12-06 00:00:51 +01001182 .read_rom_hwaddr = fecmxc_read_rom_hwaddr,
Jagan Teki60752ca2016-12-06 00:00:49 +01001183};
1184
1185static int fec_phy_init(struct fec_priv *priv, struct udevice *dev)
1186{
1187 struct phy_device *phydev;
1188 int mask = 0xffffffff;
1189
1190#ifdef CONFIG_PHYLIB
1191 mask = 1 << CONFIG_FEC_MXC_PHYADDR;
1192#endif
1193
1194 phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
1195 if (!phydev)
1196 return -ENODEV;
1197
1198 phy_connect_dev(phydev, dev);
1199
1200 priv->phydev = phydev;
1201 phy_config(phydev);
1202
1203 return 0;
1204}
1205
1206static int fecmxc_probe(struct udevice *dev)
1207{
1208 struct eth_pdata *pdata = dev_get_platdata(dev);
1209 struct fec_priv *priv = dev_get_priv(dev);
1210 struct mii_dev *bus = NULL;
1211 int dev_id = -1;
Jagan Teki60752ca2016-12-06 00:00:49 +01001212 uint32_t start;
1213 int ret;
1214
1215 ret = fec_alloc_descs(priv);
1216 if (ret)
1217 return ret;
1218
1219 bus = fec_get_miibus((uint32_t)priv->eth, dev_id);
1220 if (!bus)
1221 goto err_mii;
1222
1223 priv->bus = bus;
1224 priv->xcv_type = CONFIG_FEC_XCV_TYPE;
1225 priv->interface = pdata->phy_interface;
1226 ret = fec_phy_init(priv, dev);
1227 if (ret)
1228 goto err_phy;
1229
1230 /* Reset chip. */
Jagan Teki567173a2016-12-06 00:00:50 +01001231 writel(readl(&priv->eth->ecntrl) | FEC_ECNTRL_RESET,
1232 &priv->eth->ecntrl);
Jagan Teki60752ca2016-12-06 00:00:49 +01001233 start = get_timer(0);
1234 while (readl(&priv->eth->ecntrl) & FEC_ECNTRL_RESET) {
1235 if (get_timer(start) > (CONFIG_SYS_HZ * 5)) {
1236 printf("FEC MXC: Timeout reseting chip\n");
1237 goto err_timeout;
1238 }
1239 udelay(10);
1240 }
1241
1242 fec_reg_setup(priv);
Jagan Teki60752ca2016-12-06 00:00:49 +01001243 priv->dev_id = (dev_id == -1) ? 0 : dev_id;
1244
Jagan Teki60752ca2016-12-06 00:00:49 +01001245 return 0;
1246
1247err_timeout:
1248 free(priv->phydev);
1249err_phy:
1250 mdio_unregister(bus);
1251 free(bus);
1252err_mii:
1253 fec_free_descs(priv);
1254 return ret;
1255}
1256
1257static int fecmxc_remove(struct udevice *dev)
1258{
1259 struct fec_priv *priv = dev_get_priv(dev);
1260
1261 free(priv->phydev);
1262 fec_free_descs(priv);
1263 mdio_unregister(priv->bus);
1264 mdio_free(priv->bus);
1265
1266 return 0;
1267}
1268
1269static int fecmxc_ofdata_to_platdata(struct udevice *dev)
1270{
1271 struct eth_pdata *pdata = dev_get_platdata(dev);
1272 struct fec_priv *priv = dev_get_priv(dev);
1273 const char *phy_mode;
1274
1275 pdata->iobase = (phys_addr_t)dev_get_addr(dev);
1276 priv->eth = (struct ethernet_regs *)pdata->iobase;
1277
1278 pdata->phy_interface = -1;
Simon Glasse160f7d2017-01-17 16:52:55 -07001279 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1280 NULL);
Jagan Teki60752ca2016-12-06 00:00:49 +01001281 if (phy_mode)
1282 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1283 if (pdata->phy_interface == -1) {
1284 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1285 return -EINVAL;
1286 }
1287
1288 /* TODO
1289 * Need to get the reset-gpio and related properties from DT
1290 * and implemet the enet reset code on .probe call
1291 */
1292
1293 return 0;
1294}
1295
1296static const struct udevice_id fecmxc_ids[] = {
1297 { .compatible = "fsl,imx6q-fec" },
1298 { }
1299};
1300
1301U_BOOT_DRIVER(fecmxc_gem) = {
1302 .name = "fecmxc",
1303 .id = UCLASS_ETH,
1304 .of_match = fecmxc_ids,
1305 .ofdata_to_platdata = fecmxc_ofdata_to_platdata,
1306 .probe = fecmxc_probe,
1307 .remove = fecmxc_remove,
1308 .ops = &fecmxc_ops,
1309 .priv_auto_alloc_size = sizeof(struct fec_priv),
1310 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1311};
1312#endif