blob: 7838fb5c10a75df103fbc9bf10a5ee509f2d702b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChungLiew777d1ab2008-01-15 14:00:25 -06002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2007 Freescale Semiconductor, Inc.
7 * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
Angelo Durgehello05ffdc82019-11-15 23:54:19 +01008 *
9 * Conversion to DM
10 * (C) 2019 Angelo Dureghello <angelo.dureghello@timesys.com>
TsiChungLiew777d1ab2008-01-15 14:00:25 -060011 */
12
13#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060014#include <env.h>
TsiChungLiew777d1ab2008-01-15 14:00:25 -060015#include <malloc.h>
16#include <command.h>
17#include <config.h>
18#include <net.h>
19#include <miiphy.h>
Simon Glass68a6aa82019-11-14 12:57:31 -070020#include <linux/mii.h>
Angelo Durgehello05ffdc82019-11-15 23:54:19 +010021#include <asm/immap.h>
22#include <asm/fsl_mcdmafec.h>
23
24#include "MCD_dma.h"
TsiChungLiew777d1ab2008-01-15 14:00:25 -060025
TsiChungLiew777d1ab2008-01-15 14:00:25 -060026#undef ET_DEBUG
27#undef MII_DEBUG
28
29/* Ethernet Transmit and Receive Buffers */
30#define DBUF_LENGTH 1520
31#define PKT_MAXBUF_SIZE 1518
TsiChungLiew777d1ab2008-01-15 14:00:25 -060032#define FIFO_ERRSTAT (FIFO_STAT_RXW | FIFO_STAT_UF | FIFO_STAT_OF)
33
34/* RxBD bits definitions */
35#define BD_ENET_RX_ERR (BD_ENET_RX_LG | BD_ENET_RX_NO | BD_ENET_RX_CR | \
36 BD_ENET_RX_OV | BD_ENET_RX_TR)
37
Angelo Durgehello05ffdc82019-11-15 23:54:19 +010038DECLARE_GLOBAL_DATA_PTR;
TsiChungLiew777d1ab2008-01-15 14:00:25 -060039
Angelo Durgehello05ffdc82019-11-15 23:54:19 +010040static void init_eth_info(struct fec_info_dma *info)
41{
42 /* setup Receive and Transmit buffer descriptor */
43#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
44 static u32 tmp;
TsiChungLiew777d1ab2008-01-15 14:00:25 -060045
Angelo Durgehello05ffdc82019-11-15 23:54:19 +010046 if (info->index == 0)
47 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
48 else
49 info->rxbd = (cbd_t *)DBUF_LENGTH;
50
51 info->rxbd = (cbd_t *)((u32)info->rxbd + tmp);
52 tmp = (u32)info->rxbd;
53 info->txbd =
54 (cbd_t *)((u32)info->txbd + tmp +
55 (PKTBUFSRX * sizeof(cbd_t)));
56 tmp = (u32)info->txbd;
57 info->txbuf =
58 (char *)((u32)info->txbuf + tmp +
59 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
60 tmp = (u32)info->txbuf;
TsiChung Liewf32f7fe2008-04-30 12:11:19 -050061#else
Angelo Durgehello05ffdc82019-11-15 23:54:19 +010062 info->rxbd =
63 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
64 (PKTBUFSRX * sizeof(cbd_t)));
65 info->txbd =
66 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
67 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
68 info->txbuf =
69 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liewf32f7fe2008-04-30 12:11:19 -050070#endif
TsiChungLiew777d1ab2008-01-15 14:00:25 -060071
Angelo Durgehello05ffdc82019-11-15 23:54:19 +010072#ifdef ET_DEBUG
73 printf("rxbd %x txbd %x\n", (int)info->rxbd, (int)info->txbd);
74#endif
75 info->phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
76}
77
78static void fec_halt(struct udevice *dev)
79{
80 struct fec_info_dma *info = dev->priv;
81 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
82 int counter = 0xffff;
83
84 /* issue graceful stop command to the FEC transmitter if necessary */
85 fecp->tcr |= FEC_TCR_GTS;
86
87 /* wait for graceful stop to register */
88 while ((counter--) && (!(fecp->eir & FEC_EIR_GRA)))
89 ;
90
91 /* Disable DMA tasks */
92 MCD_killDma(info->tx_task);
93 MCD_killDma(info->rx_task);
94
95 /* Disable the Ethernet Controller */
96 fecp->ecr &= ~FEC_ECR_ETHER_EN;
97
98 /* Clear FIFO status registers */
99 fecp->rfsr &= FIFO_ERRSTAT;
100 fecp->tfsr &= FIFO_ERRSTAT;
101
102 fecp->frst = 0x01000000;
103
104 /* Issue a reset command to the FEC chip */
105 fecp->ecr |= FEC_ECR_RESET;
106
107 /* wait at least 20 clock cycles */
108 mdelay(10);
109
110#ifdef ET_DEBUG
111 printf("Ethernet task stopped\n");
112#endif
113}
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600114
115#ifdef ET_DEBUG
116static void dbg_fec_regs(struct eth_device *dev)
117{
118 struct fec_info_dma *info = dev->priv;
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100119 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600120
121 printf("=====\n");
122 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
123 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
124 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
125 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
126 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
127 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
128 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
129 printf("r hash %x - %x\n", (int)&fecp->rhr, fecp->rhr);
130 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
131 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
132 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
133 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
134 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
135 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
136 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
137 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
138 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
139 printf("r_fdata %x - %x\n", (int)&fecp->rfdr, fecp->rfdr);
140 printf("r_fstat %x - %x\n", (int)&fecp->rfsr, fecp->rfsr);
141 printf("r_fctrl %x - %x\n", (int)&fecp->rfcr, fecp->rfcr);
142 printf("r_flrfp %x - %x\n", (int)&fecp->rlrfp, fecp->rlrfp);
143 printf("r_flwfp %x - %x\n", (int)&fecp->rlwfp, fecp->rlwfp);
144 printf("r_frfar %x - %x\n", (int)&fecp->rfar, fecp->rfar);
145 printf("r_frfrp %x - %x\n", (int)&fecp->rfrp, fecp->rfrp);
146 printf("r_frfwp %x - %x\n", (int)&fecp->rfwp, fecp->rfwp);
147 printf("t_fdata %x - %x\n", (int)&fecp->tfdr, fecp->tfdr);
148 printf("t_fstat %x - %x\n", (int)&fecp->tfsr, fecp->tfsr);
149 printf("t_fctrl %x - %x\n", (int)&fecp->tfcr, fecp->tfcr);
150 printf("t_flrfp %x - %x\n", (int)&fecp->tlrfp, fecp->tlrfp);
151 printf("t_flwfp %x - %x\n", (int)&fecp->tlwfp, fecp->tlwfp);
152 printf("t_ftfar %x - %x\n", (int)&fecp->tfar, fecp->tfar);
153 printf("t_ftfrp %x - %x\n", (int)&fecp->tfrp, fecp->tfrp);
154 printf("t_ftfwp %x - %x\n", (int)&fecp->tfwp, fecp->tfwp);
155 printf("frst %x - %x\n", (int)&fecp->frst, fecp->frst);
156 printf("ctcwr %x - %x\n", (int)&fecp->ctcwr, fecp->ctcwr);
157}
158#endif
159
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100160static void set_fec_duplex_speed(volatile fecdma_t *fecp, int dup_spd)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600161{
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100162 bd_t *bd = gd->bd;
163
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600164 if ((dup_spd >> 16) == FULL) {
165 /* Set maximum frame length */
166 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
167 FEC_RCR_PROM | 0x100;
168 fecp->tcr = FEC_TCR_FDEN;
169 } else {
170 /* Half duplex mode */
171 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
172 FEC_RCR_MII_MODE | FEC_RCR_DRT;
173 fecp->tcr &= ~FEC_TCR_FDEN;
174 }
175
176 if ((dup_spd & 0xFFFF) == _100BASET) {
177#ifdef MII_DEBUG
178 printf("100Mbps\n");
179#endif
180 bd->bi_ethspeed = 100;
181 } else {
182#ifdef MII_DEBUG
183 printf("10Mbps\n");
184#endif
185 bd->bi_ethspeed = 10;
186 }
187}
188
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100189static void fec_set_hwaddr(volatile fecdma_t *fecp, u8 *mac)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600190{
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100191 u8 curr_byte; /* byte for which to compute the CRC */
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600192 int byte; /* loop - counter */
193 int bit; /* loop - counter */
194 u32 crc = 0xffffffff; /* initial value */
195
196 for (byte = 0; byte < 6; byte++) {
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100197 curr_byte = mac[byte];
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600198 for (bit = 0; bit < 8; bit++) {
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100199 if ((curr_byte & 0x01) ^ (crc & 0x01)) {
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600200 crc >>= 1;
201 crc = crc ^ 0xedb88320;
202 } else {
203 crc >>= 1;
204 }
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100205 curr_byte >>= 1;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600206 }
207 }
208
209 crc = crc >> 26;
210
211 /* Set individual hash table register */
212 if (crc >= 32) {
213 fecp->ialr = (1 << (crc - 32));
214 fecp->iaur = 0;
215 } else {
216 fecp->ialr = 0;
217 fecp->iaur = (1 << crc);
218 }
219
220 /* Set physical address */
221 fecp->palr = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
222 fecp->paur = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
223
224 /* Clear multicast address hash table */
225 fecp->gaur = 0;
226 fecp->galr = 0;
227}
228
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100229static int fec_init(struct udevice *dev)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600230{
231 struct fec_info_dma *info = dev->priv;
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100232 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
233 int rval, i;
Mike Frysingerd3f87142009-02-11 19:01:26 -0500234 uchar enetaddr[6];
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600235
236#ifdef ET_DEBUG
237 printf("fec_init: iobase 0x%08x ...\n", info->iobase);
238#endif
239
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100240 fecpin_setclear(info, 1);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600241 fec_halt(dev);
242
243#if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200244 defined (CONFIG_SYS_DISCOVER_PHY)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600245
246 mii_init();
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100247 set_fec_duplex_speed(fecp, info->dup_spd);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600248#else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200249#ifndef CONFIG_SYS_DISCOVER_PHY
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100250 set_fec_duplex_speed(fecp, (FECDUPLEX << 16) | FECSPEED);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200251#endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600252#endif /* CONFIG_CMD_MII || CONFIG_MII */
253
254 /* We use strictly polling mode only */
255 fecp->eimr = 0;
256
257 /* Clear any pending interrupt */
258 fecp->eir = 0xffffffff;
259
260 /* Set station address */
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100261 if (info->index == 0)
262 rval = eth_env_get_enetaddr("ethaddr", enetaddr);
Mike Frysingerd3f87142009-02-11 19:01:26 -0500263 else
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100264 rval = eth_env_get_enetaddr("eth1addr", enetaddr);
265
266 if (!rval) {
267 puts("Please set a valid MAC address\n");
268 return -EINVAL;
269 }
270
Mike Frysingerd3f87142009-02-11 19:01:26 -0500271 fec_set_hwaddr(fecp, enetaddr);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600272
273 /* Set Opcode/Pause Duration Register */
274 fecp->opd = 0x00010020;
275
Heinrich Schuchardte4691562017-08-29 18:44:37 +0200276 /* Setup Buffers and Buffer Descriptors */
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100277 info->rx_idx = 0;
278 info->tx_idx = 0;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600279
280 /* Setup Receiver Buffer Descriptors (13.14.24.18)
281 * Settings: Empty, Wrap */
282 for (i = 0; i < PKTBUFSRX; i++) {
283 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
284 info->rxbd[i].cbd_datlen = PKTSIZE_ALIGN;
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500285 info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600286 }
287 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
288
289 /* Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
290 * Settings: Last, Tx CRC */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200291 for (i = 0; i < CONFIG_SYS_TX_ETH_BUFFER; i++) {
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600292 info->txbd[i].cbd_sc = 0;
293 info->txbd[i].cbd_datlen = 0;
294 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
295 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200296 info->txbd[CONFIG_SYS_TX_ETH_BUFFER - 1].cbd_sc |= BD_ENET_TX_WRAP;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600297
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100298 info->used_tbd_idx = 0;
299 info->clean_tbd_num = CONFIG_SYS_TX_ETH_BUFFER;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600300
301 /* Set Rx FIFO alarm and granularity value */
302 fecp->rfcr = 0x0c000000;
303 fecp->rfar = 0x0000030c;
304
305 /* Set Tx FIFO granularity value */
306 fecp->tfcr = FIFO_CTRL_FRAME | FIFO_CTRL_GR(6) | 0x00040000;
307 fecp->tfar = 0x00000080;
308
309 fecp->tfwr = 0x2;
310 fecp->ctcwr = 0x03000000;
311
312 /* Enable DMA receive task */
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100313 MCD_startDma(info->rx_task,
314 (s8 *)info->rxbd,
315 0,
316 (s8 *)&fecp->rfdr,
317 4,
318 0,
319 4,
320 info->rx_init,
321 info->rx_pri,
322 (MCD_FECRX_DMA | MCD_TT_FLAGS_DEF),
323 (MCD_NO_CSUM | MCD_NO_BYTE_SWAP)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600324 );
325
326 /* Enable DMA tx task with no ready buffer descriptors */
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100327 MCD_startDma(info->tx_task,
328 (s8 *)info->txbd,
329 0,
330 (s8 *)&fecp->tfdr,
331 4,
332 0,
333 4,
334 info->tx_init,
335 info->tx_pri,
336 (MCD_FECTX_DMA | MCD_TT_FLAGS_DEF),
337 (MCD_NO_CSUM | MCD_NO_BYTE_SWAP)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600338 );
339
340 /* Now enable the transmit and receive processing */
341 fecp->ecr |= FEC_ECR_ETHER_EN;
342
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100343 return 0;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600344}
345
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100346static int mcdmafec_init(struct udevice *dev)
347{
348 return fec_init(dev);
349}
350
351static int mcdmafec_send(struct udevice *dev, void *packet, int length)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600352{
353 struct fec_info_dma *info = dev->priv;
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100354 cbd_t *p_tbd, *p_used_tbd;
355 u16 phy_status;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600356
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100357 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phy_status);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600358
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100359 /* process all the consumed TBDs */
360 while (info->clean_tbd_num < CONFIG_SYS_TX_ETH_BUFFER) {
361 p_used_tbd = &info->txbd[info->used_tbd_idx];
362 if (p_used_tbd->cbd_sc & BD_ENET_TX_READY) {
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600363#ifdef ET_DEBUG
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100364 printf("Cannot clean TBD %d, in use\n",
365 info->clean_tbd_num);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600366#endif
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100367 return 0;
368 }
369
370 /* clean this buffer descriptor */
371 if (info->used_tbd_idx == (CONFIG_SYS_TX_ETH_BUFFER - 1))
372 p_used_tbd->cbd_sc = BD_ENET_TX_WRAP;
373 else
374 p_used_tbd->cbd_sc = 0;
375
376 /* update some indeces for a correct handling of TBD ring */
377 info->clean_tbd_num++;
378 info->used_tbd_idx = (info->used_tbd_idx + 1)
379 % CONFIG_SYS_TX_ETH_BUFFER;
380 }
381
382 /* Check for valid length of data. */
383 if (length > 1500 || length <= 0)
384 return -1;
385
386 /* Check the number of vacant TxBDs. */
387 if (info->clean_tbd_num < 1) {
388 printf("No available TxBDs ...\n");
389 return -1;
390 }
391
392 /* Get the first TxBD to send the mac header */
393 p_tbd = &info->txbd[info->tx_idx];
394 p_tbd->cbd_datlen = length;
395 p_tbd->cbd_bufaddr = (u32)packet;
396 p_tbd->cbd_sc |= BD_ENET_TX_LAST | BD_ENET_TX_TC | BD_ENET_TX_READY;
397 info->tx_idx = (info->tx_idx + 1) % CONFIG_SYS_TX_ETH_BUFFER;
398
399 /* Enable DMA transmit task */
400 MCD_continDma(info->tx_task);
401
402 info->clean_tbd_num -= 1;
403
404 /* wait until frame is sent . */
405 while (p_tbd->cbd_sc & BD_ENET_TX_READY)
406 udelay(10);
407
408 return (int)(info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_STATS);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600409}
410
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100411static int mcdmafec_recv(struct udevice *dev, int flags, uchar **packetp)
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600412{
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100413 struct fec_info_dma *info = dev->priv;
414 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
415
416 cbd_t *prbd = &info->rxbd[info->rx_idx];
417 u32 ievent;
418 int frame_length, len = 0;
419
420 /* Check if any critical events have happened */
421 ievent = fecp->eir;
422 if (ievent != 0) {
423 fecp->eir = ievent;
424
425 if (ievent & (FEC_EIR_BABT | FEC_EIR_TXERR | FEC_EIR_RXERR)) {
426 printf("fec_recv: error\n");
427 fec_halt(dev);
428 fec_init(dev);
429 return 0;
430 }
431
432 if (ievent & FEC_EIR_HBERR) {
433 /* Heartbeat error */
434 fecp->tcr |= FEC_TCR_GTS;
435 }
436
437 if (ievent & FEC_EIR_GRA) {
438 /* Graceful stop complete */
439 if (fecp->tcr & FEC_TCR_GTS) {
440 printf("fec_recv: tcr_gts\n");
441 fec_halt(dev);
442 fecp->tcr &= ~FEC_TCR_GTS;
443 fec_init(dev);
444 }
445 }
446 }
447
448 if (!(prbd->cbd_sc & BD_ENET_RX_EMPTY)) {
449 if ((prbd->cbd_sc & BD_ENET_RX_LAST) &&
450 !(prbd->cbd_sc & BD_ENET_RX_ERR) &&
451 ((prbd->cbd_datlen - 4) > 14)) {
452 /* Get buffer address and size */
453 frame_length = prbd->cbd_datlen - 4;
454
455 /* Fill the buffer and pass it to upper layers */
456 net_process_received_packet((uchar *)prbd->cbd_bufaddr,
457 frame_length);
458 len = frame_length;
459 }
460
461 /* Reset buffer descriptor as empty */
462 if (info->rx_idx == (PKTBUFSRX - 1))
463 prbd->cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
464 else
465 prbd->cbd_sc = BD_ENET_RX_EMPTY;
466
467 prbd->cbd_datlen = PKTSIZE_ALIGN;
468
469 /* Now, we have an empty RxBD, restart the DMA receive task */
470 MCD_continDma(info->rx_task);
471
472 /* Increment BD count */
473 info->rx_idx = (info->rx_idx + 1) % PKTBUFSRX;
474 }
475
476 return len;
477}
478
479static void mcdmafec_halt(struct udevice *dev)
480{
481 fec_halt(dev);
482}
483
484static const struct eth_ops mcdmafec_ops = {
485 .start = mcdmafec_init,
486 .send = mcdmafec_send,
487 .recv = mcdmafec_recv,
488 .stop = mcdmafec_halt,
489};
490
491/*
492 * Boot sequence, called just after mcffec_ofdata_to_platdata,
493 * as DM way, it replaces old mcffec_initialize.
494 */
495static int mcdmafec_probe(struct udevice *dev)
496{
497 struct fec_info_dma *info = dev->priv;
498 struct eth_pdata *pdata = dev_get_platdata(dev);
499 int node = dev_of_offset(dev);
500 int retval;
501 const u32 *val;
502
503 info->index = dev->seq;
504 info->iobase = pdata->iobase;
505 info->miibase = pdata->iobase;
506 info->phy_addr = -1;
507
508 val = fdt_getprop(gd->fdt_blob, node, "rx-task", NULL);
509 if (val)
510 info->rx_task = fdt32_to_cpu(*val);
511
512 val = fdt_getprop(gd->fdt_blob, node, "tx-task", NULL);
513 if (val)
514 info->tx_task = fdt32_to_cpu(*val);
515
516 val = fdt_getprop(gd->fdt_blob, node, "rx-prioprity", NULL);
517 if (val)
518 info->rx_pri = fdt32_to_cpu(*val);
519
520 val = fdt_getprop(gd->fdt_blob, node, "tx-prioprity", NULL);
521 if (val)
522 info->tx_pri = fdt32_to_cpu(*val);
523
524 val = fdt_getprop(gd->fdt_blob, node, "rx-init", NULL);
525 if (val)
526 info->rx_init = fdt32_to_cpu(*val);
527
528 val = fdt_getprop(gd->fdt_blob, node, "tx-init", NULL);
529 if (val)
530 info->tx_init = fdt32_to_cpu(*val);
531
532#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
533 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
TsiChung Liewf32f7fe2008-04-30 12:11:19 -0500534#endif
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100535 init_eth_info(info);
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600536
537#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100538 info->bus = mdio_alloc();
539 if (!info->bus)
540 return -ENOMEM;
541 strncpy(info->bus->name, dev->name, MDIO_NAME_LEN);
542 info->bus->read = mcffec_miiphy_read;
543 info->bus->write = mcffec_miiphy_write;
Joe Hershberger5a49f172016-08-08 11:28:38 -0500544
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100545 retval = mdio_register(info->bus);
546 if (retval < 0)
547 return retval;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600548#endif
549
Ben Warrenb31da882008-08-26 22:12:36 -0700550 return 0;
TsiChungLiew777d1ab2008-01-15 14:00:25 -0600551}
Angelo Durgehello05ffdc82019-11-15 23:54:19 +0100552
553static int mcdmafec_remove(struct udevice *dev)
554{
555 struct fec_info_dma *priv = dev_get_priv(dev);
556
557 mdio_unregister(priv->bus);
558 mdio_free(priv->bus);
559
560 return 0;
561}
562
563/*
564 * Boot sequence, called 1st
565 */
566static int mcdmafec_ofdata_to_platdata(struct udevice *dev)
567{
568 struct eth_pdata *pdata = dev_get_platdata(dev);
569 const u32 *val;
570
571 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
572 /* Default to 10Mbit/s */
573 pdata->max_speed = 10;
574
575 val = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
576 if (val)
577 pdata->max_speed = fdt32_to_cpu(*val);
578
579 return 0;
580}
581
582static const struct udevice_id mcdmafec_ids[] = {
583 { .compatible = "fsl,mcf-dma-fec" },
584 { }
585};
586
587U_BOOT_DRIVER(mcffec) = {
588 .name = "mcdmafec",
589 .id = UCLASS_ETH,
590 .of_match = mcdmafec_ids,
591 .ofdata_to_platdata = mcdmafec_ofdata_to_platdata,
592 .probe = mcdmafec_probe,
593 .remove = mcdmafec_remove,
594 .ops = &mcdmafec_ops,
595 .priv_auto_alloc_size = sizeof(struct fec_info_dma),
596 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
597};