blob: 45954cecb6b6a9fa0d76f59815a3a710d6495f51 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
TsiChungLiew95e87872008-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 Durgehellof0a0c882019-11-15 23:54:19 +01008 *
9 * Conversion to DM
10 * (C) 2019 Angelo Dureghello <angelo.dureghello@timesys.com>
TsiChungLiew95e87872008-01-15 14:00:25 -060011 */
12
13#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060014#include <env.h>
Simon Glassf11478f2019-12-28 10:45:07 -070015#include <hang.h>
TsiChungLiew95e87872008-01-15 14:00:25 -060016#include <malloc.h>
17#include <command.h>
18#include <config.h>
19#include <net.h>
20#include <miiphy.h>
Simon Glasscaefa322019-11-14 12:57:31 -070021#include <linux/mii.h>
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010022#include <asm/immap.h>
23#include <asm/fsl_mcdmafec.h>
24
25#include "MCD_dma.h"
TsiChungLiew95e87872008-01-15 14:00:25 -060026
TsiChungLiew95e87872008-01-15 14:00:25 -060027#undef ET_DEBUG
28#undef MII_DEBUG
29
30/* Ethernet Transmit and Receive Buffers */
31#define DBUF_LENGTH 1520
32#define PKT_MAXBUF_SIZE 1518
TsiChungLiew95e87872008-01-15 14:00:25 -060033#define FIFO_ERRSTAT (FIFO_STAT_RXW | FIFO_STAT_UF | FIFO_STAT_OF)
34
35/* RxBD bits definitions */
36#define BD_ENET_RX_ERR (BD_ENET_RX_LG | BD_ENET_RX_NO | BD_ENET_RX_CR | \
37 BD_ENET_RX_OV | BD_ENET_RX_TR)
38
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010039DECLARE_GLOBAL_DATA_PTR;
TsiChungLiew95e87872008-01-15 14:00:25 -060040
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010041static void init_eth_info(struct fec_info_dma *info)
42{
43 /* setup Receive and Transmit buffer descriptor */
44#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
45 static u32 tmp;
TsiChungLiew95e87872008-01-15 14:00:25 -060046
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010047 if (info->index == 0)
48 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
49 else
50 info->rxbd = (cbd_t *)DBUF_LENGTH;
51
52 info->rxbd = (cbd_t *)((u32)info->rxbd + tmp);
53 tmp = (u32)info->rxbd;
54 info->txbd =
55 (cbd_t *)((u32)info->txbd + tmp +
56 (PKTBUFSRX * sizeof(cbd_t)));
57 tmp = (u32)info->txbd;
58 info->txbuf =
59 (char *)((u32)info->txbuf + tmp +
60 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
61 tmp = (u32)info->txbuf;
TsiChung Liew1844b8f2008-04-30 12:11:19 -050062#else
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010063 info->rxbd =
64 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
65 (PKTBUFSRX * sizeof(cbd_t)));
66 info->txbd =
67 (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE,
68 (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t)));
69 info->txbuf =
70 (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH);
TsiChung Liew1844b8f2008-04-30 12:11:19 -050071#endif
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010072
73#ifdef ET_DEBUG
74 printf("rxbd %x txbd %x\n", (int)info->rxbd, (int)info->txbd);
TsiChungLiew95e87872008-01-15 14:00:25 -060075#endif
Angelo Durgehellof0a0c882019-11-15 23:54:19 +010076 info->phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32);
77}
78
79static void fec_halt(struct udevice *dev)
80{
81 struct fec_info_dma *info = dev->priv;
82 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
83 int counter = 0xffff;
84
85 /* issue graceful stop command to the FEC transmitter if necessary */
86 fecp->tcr |= FEC_TCR_GTS;
87
88 /* wait for graceful stop to register */
89 while ((counter--) && (!(fecp->eir & FEC_EIR_GRA)))
90 ;
91
92 /* Disable DMA tasks */
93 MCD_killDma(info->tx_task);
94 MCD_killDma(info->rx_task);
95
96 /* Disable the Ethernet Controller */
97 fecp->ecr &= ~FEC_ECR_ETHER_EN;
98
99 /* Clear FIFO status registers */
100 fecp->rfsr &= FIFO_ERRSTAT;
101 fecp->tfsr &= FIFO_ERRSTAT;
102
103 fecp->frst = 0x01000000;
TsiChungLiew95e87872008-01-15 14:00:25 -0600104
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100105 /* Issue a reset command to the FEC chip */
106 fecp->ecr |= FEC_ECR_RESET;
107
108 /* wait at least 20 clock cycles */
109 mdelay(10);
110
111#ifdef ET_DEBUG
112 printf("Ethernet task stopped\n");
113#endif
114}
TsiChungLiew95e87872008-01-15 14:00:25 -0600115
116#ifdef ET_DEBUG
117static void dbg_fec_regs(struct eth_device *dev)
118{
119 struct fec_info_dma *info = dev->priv;
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100120 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
TsiChungLiew95e87872008-01-15 14:00:25 -0600121
122 printf("=====\n");
123 printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir);
124 printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr);
125 printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr);
126 printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr);
127 printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr);
128 printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc);
129 printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr);
130 printf("r hash %x - %x\n", (int)&fecp->rhr, fecp->rhr);
131 printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr);
132 printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr);
133 printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur);
134 printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd);
135 printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur);
136 printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr);
137 printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur);
138 printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr);
139 printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr);
140 printf("r_fdata %x - %x\n", (int)&fecp->rfdr, fecp->rfdr);
141 printf("r_fstat %x - %x\n", (int)&fecp->rfsr, fecp->rfsr);
142 printf("r_fctrl %x - %x\n", (int)&fecp->rfcr, fecp->rfcr);
143 printf("r_flrfp %x - %x\n", (int)&fecp->rlrfp, fecp->rlrfp);
144 printf("r_flwfp %x - %x\n", (int)&fecp->rlwfp, fecp->rlwfp);
145 printf("r_frfar %x - %x\n", (int)&fecp->rfar, fecp->rfar);
146 printf("r_frfrp %x - %x\n", (int)&fecp->rfrp, fecp->rfrp);
147 printf("r_frfwp %x - %x\n", (int)&fecp->rfwp, fecp->rfwp);
148 printf("t_fdata %x - %x\n", (int)&fecp->tfdr, fecp->tfdr);
149 printf("t_fstat %x - %x\n", (int)&fecp->tfsr, fecp->tfsr);
150 printf("t_fctrl %x - %x\n", (int)&fecp->tfcr, fecp->tfcr);
151 printf("t_flrfp %x - %x\n", (int)&fecp->tlrfp, fecp->tlrfp);
152 printf("t_flwfp %x - %x\n", (int)&fecp->tlwfp, fecp->tlwfp);
153 printf("t_ftfar %x - %x\n", (int)&fecp->tfar, fecp->tfar);
154 printf("t_ftfrp %x - %x\n", (int)&fecp->tfrp, fecp->tfrp);
155 printf("t_ftfwp %x - %x\n", (int)&fecp->tfwp, fecp->tfwp);
156 printf("frst %x - %x\n", (int)&fecp->frst, fecp->frst);
157 printf("ctcwr %x - %x\n", (int)&fecp->ctcwr, fecp->ctcwr);
158}
159#endif
160
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100161static void set_fec_duplex_speed(volatile fecdma_t *fecp, int dup_spd)
TsiChungLiew95e87872008-01-15 14:00:25 -0600162{
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100163 bd_t *bd = gd->bd;
164
TsiChungLiew95e87872008-01-15 14:00:25 -0600165 if ((dup_spd >> 16) == FULL) {
166 /* Set maximum frame length */
167 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE |
168 FEC_RCR_PROM | 0x100;
169 fecp->tcr = FEC_TCR_FDEN;
170 } else {
171 /* Half duplex mode */
172 fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) |
173 FEC_RCR_MII_MODE | FEC_RCR_DRT;
174 fecp->tcr &= ~FEC_TCR_FDEN;
175 }
176
177 if ((dup_spd & 0xFFFF) == _100BASET) {
178#ifdef MII_DEBUG
179 printf("100Mbps\n");
180#endif
181 bd->bi_ethspeed = 100;
182 } else {
183#ifdef MII_DEBUG
184 printf("10Mbps\n");
185#endif
186 bd->bi_ethspeed = 10;
187 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600188}
189
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100190static void fec_set_hwaddr(volatile fecdma_t *fecp, u8 *mac)
TsiChungLiew95e87872008-01-15 14:00:25 -0600191{
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100192 u8 curr_byte; /* byte for which to compute the CRC */
TsiChungLiew95e87872008-01-15 14:00:25 -0600193 int byte; /* loop - counter */
194 int bit; /* loop - counter */
195 u32 crc = 0xffffffff; /* initial value */
196
197 for (byte = 0; byte < 6; byte++) {
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100198 curr_byte = mac[byte];
TsiChungLiew95e87872008-01-15 14:00:25 -0600199 for (bit = 0; bit < 8; bit++) {
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100200 if ((curr_byte & 0x01) ^ (crc & 0x01)) {
TsiChungLiew95e87872008-01-15 14:00:25 -0600201 crc >>= 1;
202 crc = crc ^ 0xedb88320;
203 } else {
204 crc >>= 1;
205 }
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100206 curr_byte >>= 1;
TsiChungLiew95e87872008-01-15 14:00:25 -0600207 }
208 }
209
210 crc = crc >> 26;
211
212 /* Set individual hash table register */
213 if (crc >= 32) {
214 fecp->ialr = (1 << (crc - 32));
215 fecp->iaur = 0;
216 } else {
217 fecp->ialr = 0;
218 fecp->iaur = (1 << crc);
219 }
220
221 /* Set physical address */
222 fecp->palr = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
223 fecp->paur = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
224
225 /* Clear multicast address hash table */
226 fecp->gaur = 0;
227 fecp->galr = 0;
228}
229
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100230static int fec_init(struct udevice *dev)
TsiChungLiew95e87872008-01-15 14:00:25 -0600231{
232 struct fec_info_dma *info = dev->priv;
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100233 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
234 int rval, i;
Mike Frysingerb2039652009-02-11 19:01:26 -0500235 uchar enetaddr[6];
TsiChungLiew95e87872008-01-15 14:00:25 -0600236
237#ifdef ET_DEBUG
238 printf("fec_init: iobase 0x%08x ...\n", info->iobase);
239#endif
240
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100241 fecpin_setclear(info, 1);
TsiChungLiew95e87872008-01-15 14:00:25 -0600242 fec_halt(dev);
243
244#if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200245 defined (CONFIG_SYS_DISCOVER_PHY)
TsiChungLiew95e87872008-01-15 14:00:25 -0600246
247 mii_init();
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100248 set_fec_duplex_speed(fecp, info->dup_spd);
TsiChungLiew95e87872008-01-15 14:00:25 -0600249#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200250#ifndef CONFIG_SYS_DISCOVER_PHY
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100251 set_fec_duplex_speed(fecp, (FECDUPLEX << 16) | FECSPEED);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200252#endif /* ifndef CONFIG_SYS_DISCOVER_PHY */
TsiChungLiew95e87872008-01-15 14:00:25 -0600253#endif /* CONFIG_CMD_MII || CONFIG_MII */
254
255 /* We use strictly polling mode only */
256 fecp->eimr = 0;
257
258 /* Clear any pending interrupt */
259 fecp->eir = 0xffffffff;
260
261 /* Set station address */
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100262 if (info->index == 0)
263 rval = eth_env_get_enetaddr("ethaddr", enetaddr);
Mike Frysingerb2039652009-02-11 19:01:26 -0500264 else
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100265 rval = eth_env_get_enetaddr("eth1addr", enetaddr);
266
267 if (!rval) {
268 puts("Please set a valid MAC address\n");
269 return -EINVAL;
270 }
271
Mike Frysingerb2039652009-02-11 19:01:26 -0500272 fec_set_hwaddr(fecp, enetaddr);
TsiChungLiew95e87872008-01-15 14:00:25 -0600273
274 /* Set Opcode/Pause Duration Register */
275 fecp->opd = 0x00010020;
276
Heinrich Schuchardt42376962017-08-29 18:44:37 +0200277 /* Setup Buffers and Buffer Descriptors */
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100278 info->rx_idx = 0;
279 info->tx_idx = 0;
TsiChungLiew95e87872008-01-15 14:00:25 -0600280
281 /* Setup Receiver Buffer Descriptors (13.14.24.18)
282 * Settings: Empty, Wrap */
283 for (i = 0; i < PKTBUFSRX; i++) {
284 info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
285 info->rxbd[i].cbd_datlen = PKTSIZE_ALIGN;
Joe Hershberger9f09a362015-04-08 01:41:06 -0500286 info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i];
TsiChungLiew95e87872008-01-15 14:00:25 -0600287 }
288 info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
289
290 /* Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
291 * Settings: Last, Tx CRC */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200292 for (i = 0; i < CONFIG_SYS_TX_ETH_BUFFER; i++) {
TsiChungLiew95e87872008-01-15 14:00:25 -0600293 info->txbd[i].cbd_sc = 0;
294 info->txbd[i].cbd_datlen = 0;
295 info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]);
296 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200297 info->txbd[CONFIG_SYS_TX_ETH_BUFFER - 1].cbd_sc |= BD_ENET_TX_WRAP;
TsiChungLiew95e87872008-01-15 14:00:25 -0600298
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100299 info->used_tbd_idx = 0;
300 info->clean_tbd_num = CONFIG_SYS_TX_ETH_BUFFER;
TsiChungLiew95e87872008-01-15 14:00:25 -0600301
302 /* Set Rx FIFO alarm and granularity value */
303 fecp->rfcr = 0x0c000000;
304 fecp->rfar = 0x0000030c;
305
306 /* Set Tx FIFO granularity value */
307 fecp->tfcr = FIFO_CTRL_FRAME | FIFO_CTRL_GR(6) | 0x00040000;
308 fecp->tfar = 0x00000080;
309
310 fecp->tfwr = 0x2;
311 fecp->ctcwr = 0x03000000;
312
313 /* Enable DMA receive task */
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100314 MCD_startDma(info->rx_task,
315 (s8 *)info->rxbd,
316 0,
317 (s8 *)&fecp->rfdr,
318 4,
319 0,
320 4,
321 info->rx_init,
322 info->rx_pri,
323 (MCD_FECRX_DMA | MCD_TT_FLAGS_DEF),
324 (MCD_NO_CSUM | MCD_NO_BYTE_SWAP)
TsiChungLiew95e87872008-01-15 14:00:25 -0600325 );
326
327 /* Enable DMA tx task with no ready buffer descriptors */
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100328 MCD_startDma(info->tx_task,
329 (s8 *)info->txbd,
330 0,
331 (s8 *)&fecp->tfdr,
332 4,
333 0,
334 4,
335 info->tx_init,
336 info->tx_pri,
337 (MCD_FECTX_DMA | MCD_TT_FLAGS_DEF),
338 (MCD_NO_CSUM | MCD_NO_BYTE_SWAP)
TsiChungLiew95e87872008-01-15 14:00:25 -0600339 );
340
341 /* Now enable the transmit and receive processing */
342 fecp->ecr |= FEC_ECR_ETHER_EN;
343
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100344 return 0;
345}
346
347static int mcdmafec_init(struct udevice *dev)
348{
349 return fec_init(dev);
TsiChungLiew95e87872008-01-15 14:00:25 -0600350}
351
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100352static int mcdmafec_send(struct udevice *dev, void *packet, int length)
TsiChungLiew95e87872008-01-15 14:00:25 -0600353{
354 struct fec_info_dma *info = dev->priv;
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100355 cbd_t *p_tbd, *p_used_tbd;
356 u16 phy_status;
TsiChungLiew95e87872008-01-15 14:00:25 -0600357
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100358 miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phy_status);
TsiChungLiew95e87872008-01-15 14:00:25 -0600359
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100360 /* process all the consumed TBDs */
361 while (info->clean_tbd_num < CONFIG_SYS_TX_ETH_BUFFER) {
362 p_used_tbd = &info->txbd[info->used_tbd_idx];
363 if (p_used_tbd->cbd_sc & BD_ENET_TX_READY) {
364#ifdef ET_DEBUG
365 printf("Cannot clean TBD %d, in use\n",
366 info->clean_tbd_num);
367#endif
368 return 0;
369 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600370
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100371 /* clean this buffer descriptor */
372 if (info->used_tbd_idx == (CONFIG_SYS_TX_ETH_BUFFER - 1))
373 p_used_tbd->cbd_sc = BD_ENET_TX_WRAP;
374 else
375 p_used_tbd->cbd_sc = 0;
TsiChungLiew95e87872008-01-15 14:00:25 -0600376
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100377 /* update some indeces for a correct handling of TBD ring */
378 info->clean_tbd_num++;
379 info->used_tbd_idx = (info->used_tbd_idx + 1)
380 % CONFIG_SYS_TX_ETH_BUFFER;
381 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600382
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100383 /* Check for valid length of data. */
384 if (length > 1500 || length <= 0)
385 return -1;
TsiChungLiew95e87872008-01-15 14:00:25 -0600386
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100387 /* Check the number of vacant TxBDs. */
388 if (info->clean_tbd_num < 1) {
389 printf("No available TxBDs ...\n");
390 return -1;
391 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600392
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100393 /* Get the first TxBD to send the mac header */
394 p_tbd = &info->txbd[info->tx_idx];
395 p_tbd->cbd_datlen = length;
396 p_tbd->cbd_bufaddr = (u32)packet;
397 p_tbd->cbd_sc |= BD_ENET_TX_LAST | BD_ENET_TX_TC | BD_ENET_TX_READY;
398 info->tx_idx = (info->tx_idx + 1) % CONFIG_SYS_TX_ETH_BUFFER;
TsiChungLiew95e87872008-01-15 14:00:25 -0600399
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100400 /* Enable DMA transmit task */
401 MCD_continDma(info->tx_task);
TsiChungLiew95e87872008-01-15 14:00:25 -0600402
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100403 info->clean_tbd_num -= 1;
404
405 /* wait until frame is sent . */
406 while (p_tbd->cbd_sc & BD_ENET_TX_READY)
407 udelay(10);
408
409 return (int)(info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_STATS);
TsiChungLiew95e87872008-01-15 14:00:25 -0600410}
411
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100412static int mcdmafec_recv(struct udevice *dev, int flags, uchar **packetp)
TsiChungLiew95e87872008-01-15 14:00:25 -0600413{
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100414 struct fec_info_dma *info = dev->priv;
415 volatile fecdma_t *fecp = (fecdma_t *)info->iobase;
TsiChungLiew95e87872008-01-15 14:00:25 -0600416
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100417 cbd_t *prbd = &info->rxbd[info->rx_idx];
418 u32 ievent;
419 int frame_length, len = 0;
TsiChungLiew95e87872008-01-15 14:00:25 -0600420
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100421 /* Check if any critical events have happened */
422 ievent = fecp->eir;
423 if (ievent != 0) {
424 fecp->eir = ievent;
TsiChungLiew95e87872008-01-15 14:00:25 -0600425
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100426 if (ievent & (FEC_EIR_BABT | FEC_EIR_TXERR | FEC_EIR_RXERR)) {
427 printf("fec_recv: error\n");
428 fec_halt(dev);
429 fec_init(dev);
430 return 0;
431 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600432
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100433 if (ievent & FEC_EIR_HBERR) {
434 /* Heartbeat error */
435 fecp->tcr |= FEC_TCR_GTS;
436 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600437
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100438 if (ievent & FEC_EIR_GRA) {
439 /* Graceful stop complete */
440 if (fecp->tcr & FEC_TCR_GTS) {
441 printf("fec_recv: tcr_gts\n");
442 fec_halt(dev);
443 fecp->tcr &= ~FEC_TCR_GTS;
444 fec_init(dev);
445 }
446 }
447 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600448
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100449 if (!(prbd->cbd_sc & BD_ENET_RX_EMPTY)) {
450 if ((prbd->cbd_sc & BD_ENET_RX_LAST) &&
451 !(prbd->cbd_sc & BD_ENET_RX_ERR) &&
452 ((prbd->cbd_datlen - 4) > 14)) {
453 /* Get buffer address and size */
454 frame_length = prbd->cbd_datlen - 4;
TsiChungLiew95e87872008-01-15 14:00:25 -0600455
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100456 /* Fill the buffer and pass it to upper layers */
457 net_process_received_packet((uchar *)prbd->cbd_bufaddr,
458 frame_length);
459 len = frame_length;
460 }
TsiChungLiew95e87872008-01-15 14:00:25 -0600461
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100462 /* Reset buffer descriptor as empty */
463 if (info->rx_idx == (PKTBUFSRX - 1))
464 prbd->cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
465 else
466 prbd->cbd_sc = BD_ENET_RX_EMPTY;
467
468 prbd->cbd_datlen = PKTSIZE_ALIGN;
TsiChungLiew95e87872008-01-15 14:00:25 -0600469
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100470 /* Now, we have an empty RxBD, restart the DMA receive task */
471 MCD_continDma(info->rx_task);
472
473 /* Increment BD count */
474 info->rx_idx = (info->rx_idx + 1) % PKTBUFSRX;
475 }
476
477 return len;
478}
479
480static void mcdmafec_halt(struct udevice *dev)
481{
482 fec_halt(dev);
483}
484
485static const struct eth_ops mcdmafec_ops = {
486 .start = mcdmafec_init,
487 .send = mcdmafec_send,
488 .recv = mcdmafec_recv,
489 .stop = mcdmafec_halt,
490};
491
492/*
493 * Boot sequence, called just after mcffec_ofdata_to_platdata,
494 * as DM way, it replaces old mcffec_initialize.
495 */
496static int mcdmafec_probe(struct udevice *dev)
497{
498 struct fec_info_dma *info = dev->priv;
499 struct eth_pdata *pdata = dev_get_platdata(dev);
500 int node = dev_of_offset(dev);
501 int retval;
502 const u32 *val;
503
504 info->index = dev->seq;
505 info->iobase = pdata->iobase;
506 info->miibase = pdata->iobase;
507 info->phy_addr = -1;
508
509 val = fdt_getprop(gd->fdt_blob, node, "rx-task", NULL);
510 if (val)
511 info->rx_task = fdt32_to_cpu(*val);
512
513 val = fdt_getprop(gd->fdt_blob, node, "tx-task", NULL);
514 if (val)
515 info->tx_task = fdt32_to_cpu(*val);
516
517 val = fdt_getprop(gd->fdt_blob, node, "rx-prioprity", NULL);
518 if (val)
519 info->rx_pri = fdt32_to_cpu(*val);
520
521 val = fdt_getprop(gd->fdt_blob, node, "tx-prioprity", NULL);
522 if (val)
523 info->tx_pri = fdt32_to_cpu(*val);
524
525 val = fdt_getprop(gd->fdt_blob, node, "rx-init", NULL);
526 if (val)
527 info->rx_init = fdt32_to_cpu(*val);
528
529 val = fdt_getprop(gd->fdt_blob, node, "tx-init", NULL);
530 if (val)
531 info->tx_init = fdt32_to_cpu(*val);
532
533#ifdef CONFIG_SYS_FEC_BUF_USE_SRAM
534 u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000;
535#endif
536 init_eth_info(info);
TsiChungLiew95e87872008-01-15 14:00:25 -0600537
538#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100539 info->bus = mdio_alloc();
540 if (!info->bus)
541 return -ENOMEM;
542 strncpy(info->bus->name, dev->name, MDIO_NAME_LEN);
543 info->bus->read = mcffec_miiphy_read;
544 info->bus->write = mcffec_miiphy_write;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500545
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100546 retval = mdio_register(info->bus);
547 if (retval < 0)
548 return retval;
TsiChungLiew95e87872008-01-15 14:00:25 -0600549#endif
550
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100551 return 0;
552}
TsiChungLiew95e87872008-01-15 14:00:25 -0600553
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100554static int mcdmafec_remove(struct udevice *dev)
555{
556 struct fec_info_dma *priv = dev_get_priv(dev);
557
558 mdio_unregister(priv->bus);
559 mdio_free(priv->bus);
TsiChungLiew95e87872008-01-15 14:00:25 -0600560
Ben Warrene7edd4e2008-08-26 22:12:36 -0700561 return 0;
TsiChungLiew95e87872008-01-15 14:00:25 -0600562}
Angelo Durgehellof0a0c882019-11-15 23:54:19 +0100563
564/*
565 * Boot sequence, called 1st
566 */
567static int mcdmafec_ofdata_to_platdata(struct udevice *dev)
568{
569 struct eth_pdata *pdata = dev_get_platdata(dev);
570 const u32 *val;
571
572 pdata->iobase = (phys_addr_t)devfdt_get_addr(dev);
573 /* Default to 10Mbit/s */
574 pdata->max_speed = 10;
575
576 val = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
577 if (val)
578 pdata->max_speed = fdt32_to_cpu(*val);
579
580 return 0;
581}
582
583static const struct udevice_id mcdmafec_ids[] = {
584 { .compatible = "fsl,mcf-dma-fec" },
585 { }
586};
587
588U_BOOT_DRIVER(mcffec) = {
589 .name = "mcdmafec",
590 .id = UCLASS_ETH,
591 .of_match = mcdmafec_ids,
592 .ofdata_to_platdata = mcdmafec_ofdata_to_platdata,
593 .probe = mcdmafec_probe,
594 .remove = mcdmafec_remove,
595 .ops = &mcdmafec_ops,
596 .priv_auto_alloc_size = sizeof(struct fec_info_dma),
597 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
598};