blob: 6340585e8509b4383a5b9babfb0d2da460da2d6e [file] [log] [blame]
wdenk5da7f2f2004-01-03 00:43:19 +00001/*
2 * (C) Copyright 2003
3 * Ingo Assmus <ingo.assmus@keymile.com>
4 *
5 * based on - Driver for MV64360X ethernet ports
6 * Copyright (C) 2002 rabeeh@galileo.co.il
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27/*
28 * mv_eth.c - header file for the polled mode GT ethernet driver
29 */
30#include <common.h>
31#include <net.h>
32#include <malloc.h>
33
34#include "mv_eth.h"
35
36/* enable Debug outputs */
37
38#undef DEBUG_MV_ETH
39
40#ifdef DEBUG_MV_ETH
41#define DEBUG
42#define DP(x) x
43#else
44#define DP(x)
45#endif
46
47#undef MV64360_CHECKSUM_OFFLOAD
48/*************************************************************************
49**************************************************************************
50**************************************************************************
51* The first part is the high level driver of the gigE ethernet ports. *
52**************************************************************************
53**************************************************************************
54*************************************************************************/
55
56/* Definition for configuring driver */
57/* #define UPDATE_STATS_BY_SOFTWARE */
58#undef MV64360_RX_QUEUE_FILL_ON_TASK
59
60
61/* Constants */
62#define MAGIC_ETH_RUNNING 8031971
63#define MV64360_INTERNAL_SRAM_SIZE _256K
64#define EXTRA_BYTES 32
65#define WRAP ETH_HLEN + 2 + 4 + 16
66#define BUFFER_MTU dev->mtu + WRAP
67#define INT_CAUSE_UNMASK_ALL 0x0007ffff
68#define INT_CAUSE_UNMASK_ALL_EXT 0x0011ffff
69#ifdef MV64360_RX_FILL_ON_TASK
70#define INT_CAUSE_MASK_ALL 0x00000000
71#define INT_CAUSE_CHECK_BITS INT_CAUSE_UNMASK_ALL
72#define INT_CAUSE_CHECK_BITS_EXT INT_CAUSE_UNMASK_ALL_EXT
73#endif
74
75/* Read/Write to/from MV64360 internal registers */
76#define MV_REG_READ(offset) my_le32_to_cpu(* (volatile unsigned int *) (INTERNAL_REG_BASE_ADDR + offset))
77#define MV_REG_WRITE(offset,data) *(volatile unsigned int *) (INTERNAL_REG_BASE_ADDR + offset) = my_cpu_to_le32 (data)
78#define MV_SET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) |= ((unsigned int)my_cpu_to_le32(bits)))
79#define MV_RESET_REG_BITS(regOffset,bits) ((*((volatile unsigned int*)((INTERNAL_REG_BASE_ADDR) + (regOffset)))) &= ~((unsigned int)my_cpu_to_le32(bits)))
80
81/* Static function declarations */
82static int mv64360_eth_real_open (struct eth_device *eth);
83static int mv64360_eth_real_stop (struct eth_device *eth);
84static struct net_device_stats *mv64360_eth_get_stats (struct eth_device
85 *dev);
86static void eth_port_init_mac_tables (ETH_PORT eth_port_num);
87static void mv64360_eth_update_stat (struct eth_device *dev);
88bool db64360_eth_start (struct eth_device *eth);
89unsigned int eth_read_mib_counter (ETH_PORT eth_port_num,
90 unsigned int mib_offset);
91int mv64360_eth_receive (struct eth_device *dev);
92
93int mv64360_eth_xmit (struct eth_device *, volatile void *packet, int length);
94
95#ifndef UPDATE_STATS_BY_SOFTWARE
96static void mv64360_eth_print_stat (struct eth_device *dev);
97#endif
98/* Processes a received packet */
99extern void NetReceive (volatile uchar *, int);
100
101extern unsigned int INTERNAL_REG_BASE_ADDR;
102
103/*************************************************
104 *Helper functions - used inside the driver only *
105 *************************************************/
106#ifdef DEBUG_MV_ETH
107void print_globals (struct eth_device *dev)
108{
109 printf ("Ethernet PRINT_Globals-Debug function\n");
110 printf ("Base Address for ETH_PORT_INFO: %08x\n",
111 (unsigned int) dev->priv);
112 printf ("Base Address for mv64360_eth_priv: %08x\n",
113 (unsigned int) &(((ETH_PORT_INFO *) dev->priv)->
114 port_private));
115
116 printf ("GT Internal Base Address: %08x\n",
117 INTERNAL_REG_BASE_ADDR);
118 printf ("Base Address for TX-DESCs: %08x Number of allocated Buffers %d\n", (unsigned int) ((ETH_PORT_INFO *) dev->priv)->p_tx_desc_area_base[0], MV64360_TX_QUEUE_SIZE);
119 printf ("Base Address for RX-DESCs: %08x Number of allocated Buffers %d\n", (unsigned int) ((ETH_PORT_INFO *) dev->priv)->p_rx_desc_area_base[0], MV64360_RX_QUEUE_SIZE);
120 printf ("Base Address for RX-Buffer: %08x allocated Bytes %d\n",
121 (unsigned int) ((ETH_PORT_INFO *) dev->priv)->
122 p_rx_buffer_base[0],
123 (MV64360_RX_QUEUE_SIZE * MV64360_RX_BUFFER_SIZE) + 32);
124 printf ("Base Address for TX-Buffer: %08x allocated Bytes %d\n",
125 (unsigned int) ((ETH_PORT_INFO *) dev->priv)->
126 p_tx_buffer_base[0],
127 (MV64360_TX_QUEUE_SIZE * MV64360_TX_BUFFER_SIZE) + 32);
128}
129#endif
130
131#define my_cpu_to_le32(x) my_le32_to_cpu((x))
132
133unsigned long my_le32_to_cpu (unsigned long x)
134{
135 return (((x & 0x000000ffU) << 24) |
136 ((x & 0x0000ff00U) << 8) |
137 ((x & 0x00ff0000U) >> 8) | ((x & 0xff000000U) >> 24));
138}
139
140
141/**********************************************************************
142 * mv64360_eth_print_phy_status
143 *
144 * Prints gigabit ethenret phy status
145 *
146 * Input : pointer to ethernet interface network device structure
147 * Output : N/A
148 **********************************************************************/
149
150static void mv64360_eth_print_phy_status (struct eth_device *dev)
151{
152 struct mv64360_eth_priv *port_private;
153 unsigned int port_num;
154 ETH_PORT_INFO *ethernet_private = (ETH_PORT_INFO *) dev->priv;
155 unsigned int port_status, phy_reg_data;
156
157 port_private =
158 (struct mv64360_eth_priv *) ethernet_private->port_private;
159 port_num = port_private->port_num;
160
161 /* Check Link status on phy */
162 eth_port_read_smi_reg (port_num, 1, &phy_reg_data);
163 if (!(phy_reg_data & 0x20)) {
164 printf ("Ethernet port changed link status to DOWN\n");
165 } else {
166 port_status =
167 MV_REG_READ (MV64360_ETH_PORT_STATUS_REG (port_num));
168 printf ("Ethernet status port %d: Link up", port_num);
169 printf (", %s",
170 (port_status & BIT2) ? "Full Duplex" : "Half Duplex");
171 if (port_status & BIT4)
172 printf (", Speed 1 Gbps");
173 else
174 printf (", %s",
175 (port_status & BIT5) ? "Speed 100 Mbps" :
176 "Speed 10 Mbps");
177 printf ("\n");
178 }
179}
180
181/**********************************************************************
182 * u-boot entry functions for mv64360_eth
183 *
184 **********************************************************************/
185int db64360_eth_probe (struct eth_device *dev)
186{
187 return ((int) db64360_eth_start (dev));
188}
189
190int db64360_eth_poll (struct eth_device *dev)
191{
192 return mv64360_eth_receive (dev);
193}
194
195int db64360_eth_transmit (struct eth_device *dev, volatile void *packet,
196 int length)
197{
198 mv64360_eth_xmit (dev, packet, length);
199 return 0;
200}
201
202void db64360_eth_disable (struct eth_device *dev)
203{
204 mv64360_eth_stop (dev);
205}
206
207
208void mv6436x_eth_initialize (bd_t * bis)
209{
210 struct eth_device *dev;
211 ETH_PORT_INFO *ethernet_private;
212 struct mv64360_eth_priv *port_private;
213 int devnum, x, temp;
214 char *s, *e, buf[64];
215
216 for (devnum = 0; devnum < MV_ETH_DEVS; devnum++) {
217 dev = calloc (sizeof (*dev), 1);
218 if (!dev) {
219 printf ("%s: mv_enet%d allocation failure, %s\n",
220 __FUNCTION__, devnum, "eth_device structure");
221 return;
222 }
223
224 /* must be less than NAMESIZE (16) */
225 sprintf (dev->name, "mv_enet%d", devnum);
226
227#ifdef DEBUG
228 printf ("Initializing %s\n", dev->name);
229#endif
230
231 /* Extract the MAC address from the environment */
232 switch (devnum) {
233 case 0:
234 s = "ethaddr";
235 break;
236
237 case 1:
238 s = "eth1addr";
239 break;
240
241 case 2:
242 s = "eth2addr";
243 break;
244
245 default: /* this should never happen */
246 printf ("%s: Invalid device number %d\n",
247 __FUNCTION__, devnum);
248 return;
249 }
250
Wolfgang Denk76af2782010-07-24 21:55:43 +0200251 temp = getenv_f(s, buf, sizeof (buf));
wdenk5da7f2f2004-01-03 00:43:19 +0000252 s = (temp > 0) ? buf : NULL;
253
254#ifdef DEBUG
255 printf ("Setting MAC %d to %s\n", devnum, s);
256#endif
257 for (x = 0; x < 6; ++x) {
258 dev->enetaddr[x] = s ? simple_strtoul (s, &e, 16) : 0;
259 if (s)
260 s = (*e) ? e + 1 : e;
261 }
262 /* ronen - set the MAC addr in the HW */
263 eth_port_uc_addr_set (devnum, dev->enetaddr, 0);
264
265 dev->init = (void *) db64360_eth_probe;
266 dev->halt = (void *) ethernet_phy_reset;
267 dev->send = (void *) db64360_eth_transmit;
268 dev->recv = (void *) db64360_eth_poll;
269
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200270 ethernet_private = calloc (sizeof (*ethernet_private), 1);
271 dev->priv = (void *) ethernet_private;
272
wdenk5da7f2f2004-01-03 00:43:19 +0000273 if (!ethernet_private) {
274 printf ("%s: %s allocation failure, %s\n",
275 __FUNCTION__, dev->name,
276 "Private Device Structure");
277 free (dev);
278 return;
279 }
280 /* start with an zeroed ETH_PORT_INFO */
281 memset (ethernet_private, 0, sizeof (ETH_PORT_INFO));
282 memcpy (ethernet_private->port_mac_addr, dev->enetaddr, 6);
283
284 /* set pointer to memory for stats data structure etc... */
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200285 port_private = calloc (sizeof (*ethernet_private), 1);
Wolfgang Denkf6a692b2005-12-04 00:40:34 +0100286 ethernet_private->port_private = (void *)port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000287 if (!port_private) {
288 printf ("%s: %s allocation failure, %s\n",
289 __FUNCTION__, dev->name,
290 "Port Private Device Structure");
291
292 free (ethernet_private);
293 free (dev);
294 return;
295 }
296
297 port_private->stats =
298 calloc (sizeof (struct net_device_stats), 1);
299 if (!port_private->stats) {
300 printf ("%s: %s allocation failure, %s\n",
301 __FUNCTION__, dev->name,
302 "Net stat Structure");
303
304 free (port_private);
305 free (ethernet_private);
306 free (dev);
307 return;
308 }
309 memset (ethernet_private->port_private, 0,
310 sizeof (struct mv64360_eth_priv));
311 switch (devnum) {
312 case 0:
313 ethernet_private->port_num = ETH_0;
314 break;
315 case 1:
316 ethernet_private->port_num = ETH_1;
317 break;
318 case 2:
319 ethernet_private->port_num = ETH_2;
320 break;
321 default:
322 printf ("Invalid device number %d\n", devnum);
323 break;
324 };
325
326 port_private->port_num = devnum;
327 /*
328 * Read MIB counter on the GT in order to reset them,
329 * then zero all the stats fields in memory
330 */
331 mv64360_eth_update_stat (dev);
332 memset (port_private->stats, 0,
333 sizeof (struct net_device_stats));
334 /* Extract the MAC address from the environment */
335 switch (devnum) {
336 case 0:
337 s = "ethaddr";
338 break;
339
340 case 1:
341 s = "eth1addr";
342 break;
343
344 case 2:
345 s = "eth2addr";
346 break;
347
348 default: /* this should never happen */
349 printf ("%s: Invalid device number %d\n",
350 __FUNCTION__, devnum);
351 return;
352 }
353
Wolfgang Denk76af2782010-07-24 21:55:43 +0200354 temp = getenv_f(s, buf, sizeof (buf));
wdenk5da7f2f2004-01-03 00:43:19 +0000355 s = (temp > 0) ? buf : NULL;
356
357#ifdef DEBUG
358 printf ("Setting MAC %d to %s\n", devnum, s);
359#endif
360 for (x = 0; x < 6; ++x) {
361 dev->enetaddr[x] = s ? simple_strtoul (s, &e, 16) : 0;
362 if (s)
363 s = (*e) ? e + 1 : e;
364 }
365
366 DP (printf ("Allocating descriptor and buffer rings\n"));
367
368 ethernet_private->p_rx_desc_area_base[0] =
369 (ETH_RX_DESC *) memalign (16,
370 RX_DESC_ALIGNED_SIZE *
371 MV64360_RX_QUEUE_SIZE + 1);
372 ethernet_private->p_tx_desc_area_base[0] =
373 (ETH_TX_DESC *) memalign (16,
374 TX_DESC_ALIGNED_SIZE *
375 MV64360_TX_QUEUE_SIZE + 1);
376
377 ethernet_private->p_rx_buffer_base[0] =
378 (char *) memalign (16,
379 MV64360_RX_QUEUE_SIZE *
380 MV64360_TX_BUFFER_SIZE + 1);
381 ethernet_private->p_tx_buffer_base[0] =
382 (char *) memalign (16,
383 MV64360_RX_QUEUE_SIZE *
384 MV64360_TX_BUFFER_SIZE + 1);
385
386#ifdef DEBUG_MV_ETH
387 /* DEBUG OUTPUT prints adresses of globals */
388 print_globals (dev);
389#endif
390 eth_register (dev);
391
392 }
393 DP (printf ("%s: exit\n", __FUNCTION__));
394
395}
396
397/**********************************************************************
398 * mv64360_eth_open
399 *
400 * This function is called when openning the network device. The function
401 * should initialize all the hardware, initialize cyclic Rx/Tx
402 * descriptors chain and buffers and allocate an IRQ to the network
403 * device.
404 *
405 * Input : a pointer to the network device structure
406 * / / ronen - changed the output to match net/eth.c needs
407 * Output : nonzero of success , zero if fails.
408 * under construction
409 **********************************************************************/
410
411int mv64360_eth_open (struct eth_device *dev)
412{
413 return (mv64360_eth_real_open (dev));
414}
415
416/* Helper function for mv64360_eth_open */
417static int mv64360_eth_real_open (struct eth_device *dev)
418{
419
420 unsigned int queue;
421 ETH_PORT_INFO *ethernet_private;
422 struct mv64360_eth_priv *port_private;
423 unsigned int port_num;
Wolfgang Denk17725802011-11-29 22:17:48 +0000424 u32 phy_reg_data;
wdenk5da7f2f2004-01-03 00:43:19 +0000425
426 ethernet_private = (ETH_PORT_INFO *) dev->priv;
427 /* ronen - when we update the MAC env params we only update dev->enetaddr
428 see ./net/eth.c eth_set_enetaddr() */
429 memcpy (ethernet_private->port_mac_addr, dev->enetaddr, 6);
430
431 port_private =
432 (struct mv64360_eth_priv *) ethernet_private->port_private;
433 port_num = port_private->port_num;
434
435 /* Stop RX Queues */
436 MV_REG_WRITE (MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG (port_num),
437 0x0000ff00);
438
439 /* Clear the ethernet port interrupts */
440 MV_REG_WRITE (MV64360_ETH_INTERRUPT_CAUSE_REG (port_num), 0);
441 MV_REG_WRITE (MV64360_ETH_INTERRUPT_CAUSE_EXTEND_REG (port_num), 0);
442
443 /* Unmask RX buffer and TX end interrupt */
444 MV_REG_WRITE (MV64360_ETH_INTERRUPT_MASK_REG (port_num),
445 INT_CAUSE_UNMASK_ALL);
446
447 /* Unmask phy and link status changes interrupts */
448 MV_REG_WRITE (MV64360_ETH_INTERRUPT_EXTEND_MASK_REG (port_num),
449 INT_CAUSE_UNMASK_ALL_EXT);
450
451 /* Set phy address of the port */
452 ethernet_private->port_phy_addr = 0x8 + port_num;
453
454 /* Activate the DMA channels etc */
455 eth_port_init (ethernet_private);
456
457
458 /* "Allocate" setup TX rings */
459
460 for (queue = 0; queue < MV64360_TX_QUEUE_NUM; queue++) {
461 unsigned int size;
462
463 port_private->tx_ring_size[queue] = MV64360_TX_QUEUE_SIZE;
464 size = (port_private->tx_ring_size[queue] * TX_DESC_ALIGNED_SIZE); /*size = no of DESCs times DESC-size */
465 ethernet_private->tx_desc_area_size[queue] = size;
466
467 /* first clear desc area completely */
468 memset ((void *) ethernet_private->p_tx_desc_area_base[queue],
469 0, ethernet_private->tx_desc_area_size[queue]);
470
471 /* initialize tx desc ring with low level driver */
472 if (ether_init_tx_desc_ring
473 (ethernet_private, ETH_Q0,
474 port_private->tx_ring_size[queue],
475 MV64360_TX_BUFFER_SIZE /* Each Buffer is 1600 Byte */ ,
476 (unsigned int) ethernet_private->
477 p_tx_desc_area_base[queue],
478 (unsigned int) ethernet_private->
479 p_tx_buffer_base[queue]) == false)
480 printf ("### Error initializing TX Ring\n");
481 }
482
483 /* "Allocate" setup RX rings */
484 for (queue = 0; queue < MV64360_RX_QUEUE_NUM; queue++) {
485 unsigned int size;
486
487 /* Meantime RX Ring are fixed - but must be configurable by user */
488 port_private->rx_ring_size[queue] = MV64360_RX_QUEUE_SIZE;
489 size = (port_private->rx_ring_size[queue] *
490 RX_DESC_ALIGNED_SIZE);
491 ethernet_private->rx_desc_area_size[queue] = size;
492
493 /* first clear desc area completely */
494 memset ((void *) ethernet_private->p_rx_desc_area_base[queue],
495 0, ethernet_private->rx_desc_area_size[queue]);
496 if ((ether_init_rx_desc_ring
497 (ethernet_private, ETH_Q0,
498 port_private->rx_ring_size[queue],
499 MV64360_RX_BUFFER_SIZE /* Each Buffer is 1600 Byte */ ,
500 (unsigned int) ethernet_private->
501 p_rx_desc_area_base[queue],
502 (unsigned int) ethernet_private->
503 p_rx_buffer_base[queue])) == false)
504 printf ("### Error initializing RX Ring\n");
505 }
506
507 eth_port_start (ethernet_private);
508
509 /* Set maximum receive buffer to 9700 bytes */
510 MV_REG_WRITE (MV64360_ETH_PORT_SERIAL_CONTROL_REG (port_num),
511 (0x5 << 17) |
512 (MV_REG_READ
513 (MV64360_ETH_PORT_SERIAL_CONTROL_REG (port_num))
514 & 0xfff1ffff));
515
516 /*
517 * Set ethernet MTU for leaky bucket mechanism to 0 - this will
518 * disable the leaky bucket mechanism .
519 */
520
521 MV_REG_WRITE (MV64360_ETH_MAXIMUM_TRANSMIT_UNIT (port_num), 0);
Wolfgang Denk17725802011-11-29 22:17:48 +0000522 MV_REG_READ (MV64360_ETH_PORT_STATUS_REG (port_num));
wdenk5da7f2f2004-01-03 00:43:19 +0000523
524 /* Check Link status on phy */
525 eth_port_read_smi_reg (port_num, 1, &phy_reg_data);
526 if (!(phy_reg_data & 0x20)) {
527 /* Reset PHY */
528 if ((ethernet_phy_reset (port_num)) != true) {
529 printf ("$$ Warnning: No link on port %d \n",
530 port_num);
531 return 0;
532 } else {
533 eth_port_read_smi_reg (port_num, 1, &phy_reg_data);
534 if (!(phy_reg_data & 0x20)) {
535 printf ("### Error: Phy is not active\n");
536 return 0;
537 }
538 }
539 } else {
540 mv64360_eth_print_phy_status (dev);
541 }
542 port_private->eth_running = MAGIC_ETH_RUNNING;
543 return 1;
544}
545
546
547static int mv64360_eth_free_tx_rings (struct eth_device *dev)
548{
549 unsigned int queue;
550 ETH_PORT_INFO *ethernet_private;
551 struct mv64360_eth_priv *port_private;
552 unsigned int port_num;
553 volatile ETH_TX_DESC *p_tx_curr_desc;
554
555 ethernet_private = (ETH_PORT_INFO *) dev->priv;
556 port_private =
557 (struct mv64360_eth_priv *) ethernet_private->port_private;
558 port_num = port_private->port_num;
559
560 /* Stop Tx Queues */
561 MV_REG_WRITE (MV64360_ETH_TRANSMIT_QUEUE_COMMAND_REG (port_num),
562 0x0000ff00);
563
564 /* Free TX rings */
565 DP (printf ("Clearing previously allocated TX queues... "));
566 for (queue = 0; queue < MV64360_TX_QUEUE_NUM; queue++) {
567 /* Free on TX rings */
568 for (p_tx_curr_desc =
569 ethernet_private->p_tx_desc_area_base[queue];
570 ((unsigned int) p_tx_curr_desc <= (unsigned int)
571 ethernet_private->p_tx_desc_area_base[queue] +
572 ethernet_private->tx_desc_area_size[queue]);
573 p_tx_curr_desc =
574 (ETH_TX_DESC *) ((unsigned int) p_tx_curr_desc +
575 TX_DESC_ALIGNED_SIZE)) {
576 /* this is inside for loop */
577 if (p_tx_curr_desc->return_info != 0) {
578 p_tx_curr_desc->return_info = 0;
579 DP (printf ("freed\n"));
580 }
581 }
582 DP (printf ("Done\n"));
583 }
584 return 0;
585}
586
587static int mv64360_eth_free_rx_rings (struct eth_device *dev)
588{
589 unsigned int queue;
590 ETH_PORT_INFO *ethernet_private;
591 struct mv64360_eth_priv *port_private;
592 unsigned int port_num;
593 volatile ETH_RX_DESC *p_rx_curr_desc;
594
595 ethernet_private = (ETH_PORT_INFO *) dev->priv;
596 port_private =
597 (struct mv64360_eth_priv *) ethernet_private->port_private;
598 port_num = port_private->port_num;
599
600
601 /* Stop RX Queues */
602 MV_REG_WRITE (MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG (port_num),
603 0x0000ff00);
604
605 /* Free RX rings */
606 DP (printf ("Clearing previously allocated RX queues... "));
607 for (queue = 0; queue < MV64360_RX_QUEUE_NUM; queue++) {
608 /* Free preallocated skb's on RX rings */
609 for (p_rx_curr_desc =
610 ethernet_private->p_rx_desc_area_base[queue];
611 (((unsigned int) p_rx_curr_desc <
612 ((unsigned int) ethernet_private->
613 p_rx_desc_area_base[queue] +
614 ethernet_private->rx_desc_area_size[queue])));
615 p_rx_curr_desc =
616 (ETH_RX_DESC *) ((unsigned int) p_rx_curr_desc +
617 RX_DESC_ALIGNED_SIZE)) {
618 if (p_rx_curr_desc->return_info != 0) {
619 p_rx_curr_desc->return_info = 0;
620 DP (printf ("freed\n"));
621 }
622 }
623 DP (printf ("Done\n"));
624 }
625 return 0;
626}
627
628/**********************************************************************
629 * mv64360_eth_stop
630 *
631 * This function is used when closing the network device.
632 * It updates the hardware,
633 * release all memory that holds buffers and descriptors and release the IRQ.
634 * Input : a pointer to the device structure
635 * Output : zero if success , nonzero if fails
636 *********************************************************************/
637
638int mv64360_eth_stop (struct eth_device *dev)
639{
wdenk5da7f2f2004-01-03 00:43:19 +0000640 /* Disable all gigE address decoder */
641 MV_REG_WRITE (MV64360_ETH_BASE_ADDR_ENABLE_REG, 0x3f);
642 DP (printf ("%s Ethernet stop called ... \n", __FUNCTION__));
643 mv64360_eth_real_stop (dev);
644
645 return 0;
646};
647
648/* Helper function for mv64360_eth_stop */
649
650static int mv64360_eth_real_stop (struct eth_device *dev)
651{
652 ETH_PORT_INFO *ethernet_private;
653 struct mv64360_eth_priv *port_private;
654 unsigned int port_num;
655
656 ethernet_private = (ETH_PORT_INFO *) dev->priv;
657 port_private =
658 (struct mv64360_eth_priv *) ethernet_private->port_private;
659 port_num = port_private->port_num;
660
661
662 mv64360_eth_free_tx_rings (dev);
663 mv64360_eth_free_rx_rings (dev);
664
665 eth_port_reset (ethernet_private->port_num);
666 /* Disable ethernet port interrupts */
667 MV_REG_WRITE (MV64360_ETH_INTERRUPT_CAUSE_REG (port_num), 0);
668 MV_REG_WRITE (MV64360_ETH_INTERRUPT_CAUSE_EXTEND_REG (port_num), 0);
669 /* Mask RX buffer and TX end interrupt */
670 MV_REG_WRITE (MV64360_ETH_INTERRUPT_MASK_REG (port_num), 0);
671 /* Mask phy and link status changes interrupts */
672 MV_REG_WRITE (MV64360_ETH_INTERRUPT_EXTEND_MASK_REG (port_num), 0);
673 MV_RESET_REG_BITS (MV64360_CPU_INTERRUPT0_MASK_HIGH,
674 BIT0 << port_num);
675 /* Print Network statistics */
676#ifndef UPDATE_STATS_BY_SOFTWARE
677 /*
678 * Print statistics (only if ethernet is running),
679 * then zero all the stats fields in memory
680 */
681 if (port_private->eth_running == MAGIC_ETH_RUNNING) {
682 port_private->eth_running = 0;
683 mv64360_eth_print_stat (dev);
684 }
685 memset (port_private->stats, 0, sizeof (struct net_device_stats));
686#endif
687 DP (printf ("\nEthernet stopped ... \n"));
688 return 0;
689}
690
691
692/**********************************************************************
693 * mv64360_eth_start_xmit
694 *
695 * This function is queues a packet in the Tx descriptor for
696 * required port.
697 *
698 * Input : skb - a pointer to socket buffer
699 * dev - a pointer to the required port
700 *
701 * Output : zero upon success
702 **********************************************************************/
703
704int mv64360_eth_xmit (struct eth_device *dev, volatile void *dataPtr,
705 int dataSize)
706{
707 ETH_PORT_INFO *ethernet_private;
708 struct mv64360_eth_priv *port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000709 PKT_INFO pkt_info;
710 ETH_FUNC_RET_STATUS status;
711 struct net_device_stats *stats;
712 ETH_FUNC_RET_STATUS release_result;
713
714 ethernet_private = (ETH_PORT_INFO *) dev->priv;
715 port_private =
716 (struct mv64360_eth_priv *) ethernet_private->port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000717
718 stats = port_private->stats;
719
720 /* Update packet info data structure */
721 pkt_info.cmd_sts = ETH_TX_FIRST_DESC | ETH_TX_LAST_DESC; /* DMA owned, first last */
722 pkt_info.byte_cnt = dataSize;
723 pkt_info.buf_ptr = (unsigned int) dataPtr;
Wolfgang Denk6405a152006-03-31 18:32:53 +0200724 pkt_info.return_info = 0;
wdenk5da7f2f2004-01-03 00:43:19 +0000725
726 status = eth_port_send (ethernet_private, ETH_Q0, &pkt_info);
727 if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL)) {
728 printf ("Error on transmitting packet ..");
729 if (status == ETH_QUEUE_FULL)
730 printf ("ETH Queue is full. \n");
731 if (status == ETH_QUEUE_LAST_RESOURCE)
732 printf ("ETH Queue: using last available resource. \n");
733 goto error;
734 }
735
736 /* Update statistics and start of transmittion time */
737 stats->tx_bytes += dataSize;
738 stats->tx_packets++;
739
740 /* Check if packet(s) is(are) transmitted correctly (release everything) */
741 do {
742 release_result =
743 eth_tx_return_desc (ethernet_private, ETH_Q0,
744 &pkt_info);
745 switch (release_result) {
746 case ETH_OK:
747 DP (printf ("descriptor released\n"));
748 if (pkt_info.cmd_sts & BIT0) {
749 printf ("Error in TX\n");
750 stats->tx_errors++;
751
752 }
753 break;
754 case ETH_RETRY:
755 DP (printf ("transmission still in process\n"));
756 break;
757
758 case ETH_ERROR:
759 printf ("routine can not access Tx desc ring\n");
760 break;
761
762 case ETH_END_OF_JOB:
763 DP (printf ("the routine has nothing to release\n"));
764 break;
765 default: /* should not happen */
766 break;
767 }
768 } while (release_result == ETH_OK);
769
770
771 return 0; /* success */
772 error:
773 return 1; /* Failed - higher layers will free the skb */
774}
775
776/**********************************************************************
777 * mv64360_eth_receive
778 *
779 * This function is forward packets that are received from the port's
780 * queues toward kernel core or FastRoute them to another interface.
781 *
782 * Input : dev - a pointer to the required interface
783 * max - maximum number to receive (0 means unlimted)
784 *
785 * Output : number of served packets
786 **********************************************************************/
787
788int mv64360_eth_receive (struct eth_device *dev)
789{
790 ETH_PORT_INFO *ethernet_private;
791 struct mv64360_eth_priv *port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000792 PKT_INFO pkt_info;
793 struct net_device_stats *stats;
794
wdenk5da7f2f2004-01-03 00:43:19 +0000795 ethernet_private = (ETH_PORT_INFO *) dev->priv;
796 port_private =
797 (struct mv64360_eth_priv *) ethernet_private->port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000798 stats = port_private->stats;
799
800 while ((eth_port_receive (ethernet_private, ETH_Q0, &pkt_info) ==
801 ETH_OK)) {
802
803#ifdef DEBUG_MV_ETH
804 if (pkt_info.byte_cnt != 0) {
805 printf ("%s: Received %d byte Packet @ 0x%x\n",
806 __FUNCTION__, pkt_info.byte_cnt,
807 pkt_info.buf_ptr);
808 }
809#endif
810 /* Update statistics. Note byte count includes 4 byte CRC count */
811 stats->rx_packets++;
812 stats->rx_bytes += pkt_info.byte_cnt;
813
814 /*
815 * In case received a packet without first / last bits on OR the error
816 * summary bit is on, the packets needs to be dropeed.
817 */
818 if (((pkt_info.
819 cmd_sts & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
820 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
821 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
822 stats->rx_dropped++;
823
824 printf ("Received packet spread on multiple descriptors\n");
825
826 /* Is this caused by an error ? */
827 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY) {
828 stats->rx_errors++;
829 }
830
831 /* free these descriptors again without forwarding them to the higher layers */
832 pkt_info.buf_ptr &= ~0x7; /* realign buffer again */
833 pkt_info.byte_cnt = 0x0000; /* Reset Byte count */
834
835 if (eth_rx_return_buff
836 (ethernet_private, ETH_Q0, &pkt_info) != ETH_OK) {
837 printf ("Error while returning the RX Desc to Ring\n");
838 } else {
839 DP (printf ("RX Desc returned to Ring\n"));
840 }
841 /* /free these descriptors again */
842 } else {
843
844/* !!! call higher layer processing */
845#ifdef DEBUG_MV_ETH
846 printf ("\nNow send it to upper layer protocols (NetReceive) ...\n");
847#endif
848 /* let the upper layer handle the packet */
849 NetReceive ((uchar *) pkt_info.buf_ptr,
850 (int) pkt_info.byte_cnt);
851
852/* **************************************************************** */
853/* free descriptor */
854 pkt_info.buf_ptr &= ~0x7; /* realign buffer again */
855 pkt_info.byte_cnt = 0x0000; /* Reset Byte count */
856 DP (printf
857 ("RX: pkt_info.buf_ptr = %x\n",
858 pkt_info.buf_ptr));
859 if (eth_rx_return_buff
860 (ethernet_private, ETH_Q0, &pkt_info) != ETH_OK) {
861 printf ("Error while returning the RX Desc to Ring\n");
862 } else {
863 DP (printf ("RX Desc returned to Ring\n"));
864 }
865
866/* **************************************************************** */
867
868 }
869 }
870 mv64360_eth_get_stats (dev); /* update statistics */
871 return 1;
872}
873
874/**********************************************************************
875 * mv64360_eth_get_stats
876 *
877 * Returns a pointer to the interface statistics.
878 *
879 * Input : dev - a pointer to the required interface
880 *
881 * Output : a pointer to the interface's statistics
882 **********************************************************************/
883
884static struct net_device_stats *mv64360_eth_get_stats (struct eth_device *dev)
885{
886 ETH_PORT_INFO *ethernet_private;
887 struct mv64360_eth_priv *port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000888
889 ethernet_private = (ETH_PORT_INFO *) dev->priv;
890 port_private =
891 (struct mv64360_eth_priv *) ethernet_private->port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000892
893 mv64360_eth_update_stat (dev);
894
895 return port_private->stats;
896}
897
898
899/**********************************************************************
900 * mv64360_eth_update_stat
901 *
902 * Update the statistics structure in the private data structure
903 *
904 * Input : pointer to ethernet interface network device structure
905 * Output : N/A
906 **********************************************************************/
907
908static void mv64360_eth_update_stat (struct eth_device *dev)
909{
910 ETH_PORT_INFO *ethernet_private;
911 struct mv64360_eth_priv *port_private;
912 struct net_device_stats *stats;
wdenk5da7f2f2004-01-03 00:43:19 +0000913
914 ethernet_private = (ETH_PORT_INFO *) dev->priv;
915 port_private =
916 (struct mv64360_eth_priv *) ethernet_private->port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000917 stats = port_private->stats;
918
919 /* These are false updates */
920 stats->rx_packets += (unsigned long)
921 eth_read_mib_counter (ethernet_private->port_num,
922 ETH_MIB_GOOD_FRAMES_RECEIVED);
923 stats->tx_packets += (unsigned long)
924 eth_read_mib_counter (ethernet_private->port_num,
925 ETH_MIB_GOOD_FRAMES_SENT);
926 stats->rx_bytes += (unsigned long)
927 eth_read_mib_counter (ethernet_private->port_num,
928 ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
929 /*
930 * Ideally this should be as follows -
931 *
932 * stats->rx_bytes += stats->rx_bytes +
933 * ((unsigned long) ethReadMibCounter (ethernet_private->port_num ,
934 * ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32);
935 *
936 * But the unsigned long in PowerPC and MIPS are 32bit. So the next read
937 * is just a dummy read for proper work of the GigE port
938 */
Wolfgang Denk17725802011-11-29 22:17:48 +0000939 eth_read_mib_counter (ethernet_private->port_num,
wdenk5da7f2f2004-01-03 00:43:19 +0000940 ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH);
941 stats->tx_bytes += (unsigned long)
942 eth_read_mib_counter (ethernet_private->port_num,
943 ETH_MIB_GOOD_OCTETS_SENT_LOW);
Wolfgang Denk17725802011-11-29 22:17:48 +0000944 eth_read_mib_counter (ethernet_private->port_num,
wdenk5da7f2f2004-01-03 00:43:19 +0000945 ETH_MIB_GOOD_OCTETS_SENT_HIGH);
946 stats->rx_errors += (unsigned long)
947 eth_read_mib_counter (ethernet_private->port_num,
948 ETH_MIB_MAC_RECEIVE_ERROR);
949
950 /* Rx dropped is for received packet with CRC error */
951 stats->rx_dropped +=
952 (unsigned long) eth_read_mib_counter (ethernet_private->
953 port_num,
954 ETH_MIB_BAD_CRC_EVENT);
955 stats->multicast += (unsigned long)
956 eth_read_mib_counter (ethernet_private->port_num,
957 ETH_MIB_MULTICAST_FRAMES_RECEIVED);
958 stats->collisions +=
959 (unsigned long) eth_read_mib_counter (ethernet_private->
960 port_num,
961 ETH_MIB_COLLISION) +
962 (unsigned long) eth_read_mib_counter (ethernet_private->
963 port_num,
964 ETH_MIB_LATE_COLLISION);
965 /* detailed rx errors */
966 stats->rx_length_errors +=
967 (unsigned long) eth_read_mib_counter (ethernet_private->
968 port_num,
969 ETH_MIB_UNDERSIZE_RECEIVED)
970 +
971 (unsigned long) eth_read_mib_counter (ethernet_private->
972 port_num,
973 ETH_MIB_OVERSIZE_RECEIVED);
974 /* detailed tx errors */
975}
976
977#ifndef UPDATE_STATS_BY_SOFTWARE
978/**********************************************************************
979 * mv64360_eth_print_stat
980 *
981 * Update the statistics structure in the private data structure
982 *
983 * Input : pointer to ethernet interface network device structure
984 * Output : N/A
985 **********************************************************************/
986
987static void mv64360_eth_print_stat (struct eth_device *dev)
988{
989 ETH_PORT_INFO *ethernet_private;
990 struct mv64360_eth_priv *port_private;
991 struct net_device_stats *stats;
wdenk5da7f2f2004-01-03 00:43:19 +0000992
993 ethernet_private = (ETH_PORT_INFO *) dev->priv;
994 port_private =
995 (struct mv64360_eth_priv *) ethernet_private->port_private;
wdenk5da7f2f2004-01-03 00:43:19 +0000996 stats = port_private->stats;
997
998 /* These are false updates */
999 printf ("\n### Network statistics: ###\n");
1000 printf ("--------------------------\n");
1001 printf (" Packets received: %ld\n", stats->rx_packets);
1002 printf (" Packets send: %ld\n", stats->tx_packets);
1003 printf (" Received bytes: %ld\n", stats->rx_bytes);
1004 printf (" Send bytes: %ld\n", stats->tx_bytes);
1005 if (stats->rx_errors != 0)
1006 printf (" Rx Errors: %ld\n",
1007 stats->rx_errors);
1008 if (stats->rx_dropped != 0)
1009 printf (" Rx dropped (CRC Errors): %ld\n",
1010 stats->rx_dropped);
1011 if (stats->multicast != 0)
1012 printf (" Rx mulicast frames: %ld\n",
1013 stats->multicast);
1014 if (stats->collisions != 0)
1015 printf (" No. of collisions: %ld\n",
1016 stats->collisions);
1017 if (stats->rx_length_errors != 0)
1018 printf (" Rx length errors: %ld\n",
1019 stats->rx_length_errors);
1020}
1021#endif
1022
1023/**************************************************************************
1024 *network_start - Network Kick Off Routine UBoot
1025 *Inputs :
1026 *Outputs :
1027 **************************************************************************/
1028
1029bool db64360_eth_start (struct eth_device *dev)
1030{
1031 return (mv64360_eth_open (dev)); /* calls real open */
1032}
1033
1034/*************************************************************************
1035**************************************************************************
1036**************************************************************************
1037* The second part is the low level driver of the gigE ethernet ports. *
1038**************************************************************************
1039**************************************************************************
1040*************************************************************************/
1041/*
1042 * based on Linux code
Stefan Roese88fbf932010-04-15 16:07:28 +02001043 * arch/powerpc/galileo/EVB64360/mv64360_eth.c - Driver for MV64360X ethernet ports
wdenk5da7f2f2004-01-03 00:43:19 +00001044 * Copyright (C) 2002 rabeeh@galileo.co.il
1045
1046 * This program is free software; you can redistribute it and/or
1047 * modify it under the terms of the GNU General Public License
1048 * as published by the Free Software Foundation; either version 2
1049 * of the License, or (at your option) any later version.
1050
1051 * This program is distributed in the hope that it will be useful,
1052 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1053 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1054 * GNU General Public License for more details.
1055
1056 * You should have received a copy of the GNU General Public License
1057 * along with this program; if not, write to the Free Software
1058 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1059 *
1060 */
1061
1062/********************************************************************************
1063 * Marvell's Gigabit Ethernet controller low level driver
1064 *
1065 * DESCRIPTION:
1066 * This file introduce low level API to Marvell's Gigabit Ethernet
1067 * controller. This Gigabit Ethernet Controller driver API controls
1068 * 1) Operations (i.e. port init, start, reset etc').
1069 * 2) Data flow (i.e. port send, receive etc').
1070 * Each Gigabit Ethernet port is controlled via ETH_PORT_INFO
1071 * struct.
1072 * This struct includes user configuration information as well as
1073 * driver internal data needed for its operations.
1074 *
1075 * Supported Features:
1076 * - This low level driver is OS independent. Allocating memory for
1077 * the descriptor rings and buffers are not within the scope of
1078 * this driver.
1079 * - The user is free from Rx/Tx queue managing.
1080 * - This low level driver introduce functionality API that enable
1081 * the to operate Marvell's Gigabit Ethernet Controller in a
1082 * convenient way.
1083 * - Simple Gigabit Ethernet port operation API.
1084 * - Simple Gigabit Ethernet port data flow API.
1085 * - Data flow and operation API support per queue functionality.
1086 * - Support cached descriptors for better performance.
1087 * - Enable access to all four DRAM banks and internal SRAM memory
1088 * spaces.
1089 * - PHY access and control API.
1090 * - Port control register configuration API.
1091 * - Full control over Unicast and Multicast MAC configurations.
1092 *
1093 * Operation flow:
1094 *
1095 * Initialization phase
1096 * This phase complete the initialization of the ETH_PORT_INFO
1097 * struct.
1098 * User information regarding port configuration has to be set
1099 * prior to calling the port initialization routine. For example,
1100 * the user has to assign the port_phy_addr field which is board
1101 * depended parameter.
1102 * In this phase any port Tx/Rx activity is halted, MIB counters
1103 * are cleared, PHY address is set according to user parameter and
1104 * access to DRAM and internal SRAM memory spaces.
1105 *
1106 * Driver ring initialization
1107 * Allocating memory for the descriptor rings and buffers is not
1108 * within the scope of this driver. Thus, the user is required to
1109 * allocate memory for the descriptors ring and buffers. Those
1110 * memory parameters are used by the Rx and Tx ring initialization
1111 * routines in order to curve the descriptor linked list in a form
1112 * of a ring.
1113 * Note: Pay special attention to alignment issues when using
1114 * cached descriptors/buffers. In this phase the driver store
1115 * information in the ETH_PORT_INFO struct regarding each queue
1116 * ring.
1117 *
1118 * Driver start
1119 * This phase prepares the Ethernet port for Rx and Tx activity.
1120 * It uses the information stored in the ETH_PORT_INFO struct to
1121 * initialize the various port registers.
1122 *
1123 * Data flow:
1124 * All packet references to/from the driver are done using PKT_INFO
1125 * struct.
1126 * This struct is a unified struct used with Rx and Tx operations.
1127 * This way the user is not required to be familiar with neither
1128 * Tx nor Rx descriptors structures.
1129 * The driver's descriptors rings are management by indexes.
1130 * Those indexes controls the ring resources and used to indicate
1131 * a SW resource error:
1132 * 'current'
1133 * This index points to the current available resource for use. For
1134 * example in Rx process this index will point to the descriptor
1135 * that will be passed to the user upon calling the receive routine.
1136 * In Tx process, this index will point to the descriptor
1137 * that will be assigned with the user packet info and transmitted.
1138 * 'used'
1139 * This index points to the descriptor that need to restore its
1140 * resources. For example in Rx process, using the Rx buffer return
1141 * API will attach the buffer returned in packet info to the
1142 * descriptor pointed by 'used'. In Tx process, using the Tx
1143 * descriptor return will merely return the user packet info with
1144 * the command status of the transmitted buffer pointed by the
1145 * 'used' index. Nevertheless, it is essential to use this routine
1146 * to update the 'used' index.
1147 * 'first'
1148 * This index supports Tx Scatter-Gather. It points to the first
1149 * descriptor of a packet assembled of multiple buffers. For example
1150 * when in middle of Such packet we have a Tx resource error the
1151 * 'curr' index get the value of 'first' to indicate that the ring
1152 * returned to its state before trying to transmit this packet.
1153 *
1154 * Receive operation:
1155 * The eth_port_receive API set the packet information struct,
1156 * passed by the caller, with received information from the
1157 * 'current' SDMA descriptor.
1158 * It is the user responsibility to return this resource back
1159 * to the Rx descriptor ring to enable the reuse of this source.
1160 * Return Rx resource is done using the eth_rx_return_buff API.
1161 *
1162 * Transmit operation:
1163 * The eth_port_send API supports Scatter-Gather which enables to
1164 * send a packet spanned over multiple buffers. This means that
1165 * for each packet info structure given by the user and put into
1166 * the Tx descriptors ring, will be transmitted only if the 'LAST'
1167 * bit will be set in the packet info command status field. This
1168 * API also consider restriction regarding buffer alignments and
1169 * sizes.
1170 * The user must return a Tx resource after ensuring the buffer
1171 * has been transmitted to enable the Tx ring indexes to update.
1172 *
1173 * BOARD LAYOUT
1174 * This device is on-board. No jumper diagram is necessary.
1175 *
1176 * EXTERNAL INTERFACE
1177 *
1178 * Prior to calling the initialization routine eth_port_init() the user
1179 * must set the following fields under ETH_PORT_INFO struct:
1180 * port_num User Ethernet port number.
1181 * port_phy_addr User PHY address of Ethernet port.
1182 * port_mac_addr[6] User defined port MAC address.
1183 * port_config User port configuration value.
1184 * port_config_extend User port config extend value.
1185 * port_sdma_config User port SDMA config value.
1186 * port_serial_control User port serial control value.
1187 * *port_virt_to_phys () User function to cast virtual addr to CPU bus addr.
1188 * *port_private User scratch pad for user specific data structures.
1189 *
1190 * This driver introduce a set of default values:
1191 * PORT_CONFIG_VALUE Default port configuration value
1192 * PORT_CONFIG_EXTEND_VALUE Default port extend configuration value
1193 * PORT_SDMA_CONFIG_VALUE Default sdma control value
1194 * PORT_SERIAL_CONTROL_VALUE Default port serial control value
1195 *
1196 * This driver data flow is done using the PKT_INFO struct which is
1197 * a unified struct for Rx and Tx operations:
1198 * byte_cnt Tx/Rx descriptor buffer byte count.
1199 * l4i_chk CPU provided TCP Checksum. For Tx operation only.
1200 * cmd_sts Tx/Rx descriptor command status.
1201 * buf_ptr Tx/Rx descriptor buffer pointer.
1202 * return_info Tx/Rx user resource return information.
1203 *
1204 *
1205 * EXTERNAL SUPPORT REQUIREMENTS
1206 *
1207 * This driver requires the following external support:
1208 *
1209 * D_CACHE_FLUSH_LINE (address, address offset)
1210 *
1211 * This macro applies assembly code to flush and invalidate cache
1212 * line.
1213 * address - address base.
1214 * address offset - address offset
1215 *
1216 *
1217 * CPU_PIPE_FLUSH
1218 *
1219 * This macro applies assembly code to flush the CPU pipeline.
1220 *
1221 *******************************************************************************/
1222/* includes */
1223
1224/* defines */
1225/* SDMA command macros */
1226#define ETH_ENABLE_TX_QUEUE(tx_queue, eth_port) \
1227 MV_REG_WRITE(MV64360_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port), (1 << tx_queue))
1228
1229#define ETH_DISABLE_TX_QUEUE(tx_queue, eth_port) \
1230 MV_REG_WRITE(MV64360_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port),\
1231 (1 << (8 + tx_queue)))
1232
1233#define ETH_ENABLE_RX_QUEUE(rx_queue, eth_port) \
1234MV_REG_WRITE(MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG(eth_port), (1 << rx_queue))
1235
1236#define ETH_DISABLE_RX_QUEUE(rx_queue, eth_port) \
1237MV_REG_WRITE(MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG(eth_port), (1 << (8 + rx_queue)))
1238
1239#define CURR_RFD_GET(p_curr_desc, queue) \
1240 ((p_curr_desc) = p_eth_port_ctrl->p_rx_curr_desc_q[queue])
1241
1242#define CURR_RFD_SET(p_curr_desc, queue) \
1243 (p_eth_port_ctrl->p_rx_curr_desc_q[queue] = (p_curr_desc))
1244
1245#define USED_RFD_GET(p_used_desc, queue) \
1246 ((p_used_desc) = p_eth_port_ctrl->p_rx_used_desc_q[queue])
1247
1248#define USED_RFD_SET(p_used_desc, queue)\
1249(p_eth_port_ctrl->p_rx_used_desc_q[queue] = (p_used_desc))
1250
1251
1252#define CURR_TFD_GET(p_curr_desc, queue) \
1253 ((p_curr_desc) = p_eth_port_ctrl->p_tx_curr_desc_q[queue])
1254
1255#define CURR_TFD_SET(p_curr_desc, queue) \
1256 (p_eth_port_ctrl->p_tx_curr_desc_q[queue] = (p_curr_desc))
1257
1258#define USED_TFD_GET(p_used_desc, queue) \
1259 ((p_used_desc) = p_eth_port_ctrl->p_tx_used_desc_q[queue])
1260
1261#define USED_TFD_SET(p_used_desc, queue) \
1262 (p_eth_port_ctrl->p_tx_used_desc_q[queue] = (p_used_desc))
1263
1264#define FIRST_TFD_GET(p_first_desc, queue) \
1265 ((p_first_desc) = p_eth_port_ctrl->p_tx_first_desc_q[queue])
1266
1267#define FIRST_TFD_SET(p_first_desc, queue) \
1268 (p_eth_port_ctrl->p_tx_first_desc_q[queue] = (p_first_desc))
1269
1270
1271/* Macros that save access to desc in order to find next desc pointer */
1272#define RX_NEXT_DESC_PTR(p_rx_desc, queue) (ETH_RX_DESC*)(((((unsigned int)p_rx_desc - (unsigned int)p_eth_port_ctrl->p_rx_desc_area_base[queue]) + RX_DESC_ALIGNED_SIZE) % p_eth_port_ctrl->rx_desc_area_size[queue]) + (unsigned int)p_eth_port_ctrl->p_rx_desc_area_base[queue])
1273
1274#define TX_NEXT_DESC_PTR(p_tx_desc, queue) (ETH_TX_DESC*)(((((unsigned int)p_tx_desc - (unsigned int)p_eth_port_ctrl->p_tx_desc_area_base[queue]) + TX_DESC_ALIGNED_SIZE) % p_eth_port_ctrl->tx_desc_area_size[queue]) + (unsigned int)p_eth_port_ctrl->p_tx_desc_area_base[queue])
1275
1276#define LINK_UP_TIMEOUT 100000
1277#define PHY_BUSY_TIMEOUT 10000000
1278
1279/* locals */
1280
1281/* PHY routines */
1282static void ethernet_phy_set (ETH_PORT eth_port_num, int phy_addr);
1283static int ethernet_phy_get (ETH_PORT eth_port_num);
1284
1285/* Ethernet Port routines */
1286static void eth_set_access_control (ETH_PORT eth_port_num,
1287 ETH_WIN_PARAM * param);
1288static bool eth_port_uc_addr (ETH_PORT eth_port_num, unsigned char uc_nibble,
1289 ETH_QUEUE queue, int option);
1290#if 0 /* FIXME */
1291static bool eth_port_smc_addr (ETH_PORT eth_port_num,
1292 unsigned char mc_byte,
1293 ETH_QUEUE queue, int option);
1294static bool eth_port_omc_addr (ETH_PORT eth_port_num,
1295 unsigned char crc8,
1296 ETH_QUEUE queue, int option);
1297#endif
1298
1299static void eth_b_copy (unsigned int src_addr, unsigned int dst_addr,
1300 int byte_count);
1301
1302void eth_dbg (ETH_PORT_INFO * p_eth_port_ctrl);
1303
1304
1305typedef enum _memory_bank { BANK0, BANK1, BANK2, BANK3 } MEMORY_BANK;
1306u32 mv_get_dram_bank_base_addr (MEMORY_BANK bank)
1307{
1308 u32 result = 0;
1309 u32 enable = MV_REG_READ (MV64360_BASE_ADDR_ENABLE);
1310
1311 if (enable & (1 << bank))
1312 return 0;
1313 if (bank == BANK0)
1314 result = MV_REG_READ (MV64360_CS_0_BASE_ADDR);
1315 if (bank == BANK1)
1316 result = MV_REG_READ (MV64360_CS_1_BASE_ADDR);
1317 if (bank == BANK2)
1318 result = MV_REG_READ (MV64360_CS_2_BASE_ADDR);
1319 if (bank == BANK3)
1320 result = MV_REG_READ (MV64360_CS_3_BASE_ADDR);
1321 result &= 0x0000ffff;
1322 result = result << 16;
1323 return result;
1324}
1325
1326u32 mv_get_dram_bank_size (MEMORY_BANK bank)
1327{
1328 u32 result = 0;
1329 u32 enable = MV_REG_READ (MV64360_BASE_ADDR_ENABLE);
1330
1331 if (enable & (1 << bank))
1332 return 0;
1333 if (bank == BANK0)
1334 result = MV_REG_READ (MV64360_CS_0_SIZE);
1335 if (bank == BANK1)
1336 result = MV_REG_READ (MV64360_CS_1_SIZE);
1337 if (bank == BANK2)
1338 result = MV_REG_READ (MV64360_CS_2_SIZE);
1339 if (bank == BANK3)
1340 result = MV_REG_READ (MV64360_CS_3_SIZE);
1341 result += 1;
1342 result &= 0x0000ffff;
1343 result = result << 16;
1344 return result;
1345}
1346
1347u32 mv_get_internal_sram_base (void)
1348{
1349 u32 result;
1350
1351 result = MV_REG_READ (MV64360_INTEGRATED_SRAM_BASE_ADDR);
1352 result &= 0x0000ffff;
1353 result = result << 16;
1354 return result;
1355}
1356
1357/*******************************************************************************
1358* eth_port_init - Initialize the Ethernet port driver
1359*
1360* DESCRIPTION:
1361* This function prepares the ethernet port to start its activity:
1362* 1) Completes the ethernet port driver struct initialization toward port
1363* start routine.
1364* 2) Resets the device to a quiescent state in case of warm reboot.
1365* 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
1366* 4) Clean MAC tables. The reset status of those tables is unknown.
1367* 5) Set PHY address.
1368* Note: Call this routine prior to eth_port_start routine and after setting
1369* user values in the user fields of Ethernet port control struct (i.e.
1370* port_phy_addr).
1371*
1372* INPUT:
Wolfgang Denka1be4762008-05-20 16:00:29 +02001373* ETH_PORT_INFO *p_eth_port_ctrl Ethernet port control struct
wdenk5da7f2f2004-01-03 00:43:19 +00001374*
1375* OUTPUT:
1376* See description.
1377*
1378* RETURN:
1379* None.
1380*
1381*******************************************************************************/
1382static void eth_port_init (ETH_PORT_INFO * p_eth_port_ctrl)
1383{
1384 int queue;
1385 ETH_WIN_PARAM win_param;
1386
1387 p_eth_port_ctrl->port_config = PORT_CONFIG_VALUE;
1388 p_eth_port_ctrl->port_config_extend = PORT_CONFIG_EXTEND_VALUE;
1389 p_eth_port_ctrl->port_sdma_config = PORT_SDMA_CONFIG_VALUE;
1390 p_eth_port_ctrl->port_serial_control = PORT_SERIAL_CONTROL_VALUE;
1391
1392 p_eth_port_ctrl->port_rx_queue_command = 0;
1393 p_eth_port_ctrl->port_tx_queue_command = 0;
1394
1395 /* Zero out SW structs */
1396 for (queue = 0; queue < MAX_RX_QUEUE_NUM; queue++) {
1397 CURR_RFD_SET ((ETH_RX_DESC *) 0x00000000, queue);
1398 USED_RFD_SET ((ETH_RX_DESC *) 0x00000000, queue);
1399 p_eth_port_ctrl->rx_resource_err[queue] = false;
1400 }
1401
1402 for (queue = 0; queue < MAX_TX_QUEUE_NUM; queue++) {
1403 CURR_TFD_SET ((ETH_TX_DESC *) 0x00000000, queue);
1404 USED_TFD_SET ((ETH_TX_DESC *) 0x00000000, queue);
1405 FIRST_TFD_SET ((ETH_TX_DESC *) 0x00000000, queue);
1406 p_eth_port_ctrl->tx_resource_err[queue] = false;
1407 }
1408
1409 eth_port_reset (p_eth_port_ctrl->port_num);
1410
1411 /* Set access parameters for DRAM bank 0 */
1412 win_param.win = ETH_WIN0; /* Use Ethernet window 0 */
1413 win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
1414 win_param.attributes = EBAR_ATTR_DRAM_CS0; /* Enable DRAM bank */
1415#ifndef CONFIG_NOT_COHERENT_CACHE
1416 win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
1417#endif
1418 win_param.high_addr = 0;
1419 /* Get bank base */
1420 win_param.base_addr = mv_get_dram_bank_base_addr (BANK0);
1421 win_param.size = mv_get_dram_bank_size (BANK0); /* Get bank size */
1422 if (win_param.size == 0)
1423 win_param.enable = 0;
1424 else
1425 win_param.enable = 1; /* Enable the access */
1426 win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
1427
1428 /* Set the access control for address window (EPAPR) READ & WRITE */
1429 eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
1430
1431 /* Set access parameters for DRAM bank 1 */
1432 win_param.win = ETH_WIN1; /* Use Ethernet window 1 */
1433 win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
1434 win_param.attributes = EBAR_ATTR_DRAM_CS1; /* Enable DRAM bank */
1435#ifndef CONFIG_NOT_COHERENT_CACHE
1436 win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
1437#endif
1438 win_param.high_addr = 0;
1439 /* Get bank base */
1440 win_param.base_addr = mv_get_dram_bank_base_addr (BANK1);
1441 win_param.size = mv_get_dram_bank_size (BANK1); /* Get bank size */
1442 if (win_param.size == 0)
1443 win_param.enable = 0;
1444 else
1445 win_param.enable = 1; /* Enable the access */
1446 win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
1447
1448 /* Set the access control for address window (EPAPR) READ & WRITE */
1449 eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
1450
1451 /* Set access parameters for DRAM bank 2 */
1452 win_param.win = ETH_WIN2; /* Use Ethernet window 2 */
1453 win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
1454 win_param.attributes = EBAR_ATTR_DRAM_CS2; /* Enable DRAM bank */
1455#ifndef CONFIG_NOT_COHERENT_CACHE
1456 win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
1457#endif
1458 win_param.high_addr = 0;
1459 /* Get bank base */
1460 win_param.base_addr = mv_get_dram_bank_base_addr (BANK2);
1461 win_param.size = mv_get_dram_bank_size (BANK2); /* Get bank size */
1462 if (win_param.size == 0)
1463 win_param.enable = 0;
1464 else
1465 win_param.enable = 1; /* Enable the access */
1466 win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
1467
1468 /* Set the access control for address window (EPAPR) READ & WRITE */
1469 eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
1470
1471 /* Set access parameters for DRAM bank 3 */
1472 win_param.win = ETH_WIN3; /* Use Ethernet window 3 */
1473 win_param.target = ETH_TARGET_DRAM; /* Window target - DDR */
1474 win_param.attributes = EBAR_ATTR_DRAM_CS3; /* Enable DRAM bank */
1475#ifndef CONFIG_NOT_COHERENT_CACHE
1476 win_param.attributes |= EBAR_ATTR_DRAM_CACHE_COHERENCY_WB;
1477#endif
1478 win_param.high_addr = 0;
1479 /* Get bank base */
1480 win_param.base_addr = mv_get_dram_bank_base_addr (BANK3);
1481 win_param.size = mv_get_dram_bank_size (BANK3); /* Get bank size */
1482 if (win_param.size == 0)
1483 win_param.enable = 0;
1484 else
1485 win_param.enable = 1; /* Enable the access */
1486 win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
1487
1488 /* Set the access control for address window (EPAPR) READ & WRITE */
1489 eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
1490
1491 /* Set access parameters for Internal SRAM */
1492 win_param.win = ETH_WIN4; /* Use Ethernet window 0 */
1493 win_param.target = EBAR_TARGET_CBS; /* Target - Internal SRAM */
1494 win_param.attributes = EBAR_ATTR_CBS_SRAM | EBAR_ATTR_CBS_SRAM_BLOCK0;
1495 win_param.high_addr = 0;
1496 win_param.base_addr = mv_get_internal_sram_base (); /* Get base addr */
1497 win_param.size = MV64360_INTERNAL_SRAM_SIZE; /* Get bank size */
1498 win_param.enable = 1; /* Enable the access */
1499 win_param.access_ctrl = EWIN_ACCESS_FULL; /* Enable full access */
1500
1501 /* Set the access control for address window (EPAPR) READ & WRITE */
1502 eth_set_access_control (p_eth_port_ctrl->port_num, &win_param);
1503
1504 eth_port_init_mac_tables (p_eth_port_ctrl->port_num);
1505
1506 ethernet_phy_set (p_eth_port_ctrl->port_num,
1507 p_eth_port_ctrl->port_phy_addr);
1508
1509 return;
1510
1511}
1512
1513/*******************************************************************************
1514* eth_port_start - Start the Ethernet port activity.
1515*
1516* DESCRIPTION:
1517* This routine prepares the Ethernet port for Rx and Tx activity:
1518* 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
1519* has been initialized a descriptor's ring (using ether_init_tx_desc_ring
1520* for Tx and ether_init_rx_desc_ring for Rx)
1521* 2. Initialize and enable the Ethernet configuration port by writing to
1522* the port's configuration and command registers.
1523* 3. Initialize and enable the SDMA by writing to the SDMA's
1524* configuration and command registers.
1525* After completing these steps, the ethernet port SDMA can starts to
1526* perform Rx and Tx activities.
1527*
1528* Note: Each Rx and Tx queue descriptor's list must be initialized prior
1529* to calling this function (use ether_init_tx_desc_ring for Tx queues and
1530* ether_init_rx_desc_ring for Rx queues).
1531*
1532* INPUT:
Wolfgang Denka1be4762008-05-20 16:00:29 +02001533* ETH_PORT_INFO *p_eth_port_ctrl Ethernet port control struct
wdenk5da7f2f2004-01-03 00:43:19 +00001534*
1535* OUTPUT:
1536* Ethernet port is ready to receive and transmit.
1537*
1538* RETURN:
1539* false if the port PHY is not up.
1540* true otherwise.
1541*
1542*******************************************************************************/
1543static bool eth_port_start (ETH_PORT_INFO * p_eth_port_ctrl)
1544{
1545 int queue;
1546 volatile ETH_TX_DESC *p_tx_curr_desc;
1547 volatile ETH_RX_DESC *p_rx_curr_desc;
1548 unsigned int phy_reg_data;
1549 ETH_PORT eth_port_num = p_eth_port_ctrl->port_num;
1550
1551
1552 /* Assignment of Tx CTRP of given queue */
1553 for (queue = 0; queue < MAX_TX_QUEUE_NUM; queue++) {
1554 CURR_TFD_GET (p_tx_curr_desc, queue);
1555 MV_REG_WRITE ((MV64360_ETH_TX_CURRENT_QUEUE_DESC_PTR_0
1556 (eth_port_num)
1557 + (4 * queue)),
1558 ((unsigned int) p_tx_curr_desc));
1559
1560 }
1561
1562 /* Assignment of Rx CRDP of given queue */
1563 for (queue = 0; queue < MAX_RX_QUEUE_NUM; queue++) {
1564 CURR_RFD_GET (p_rx_curr_desc, queue);
1565 MV_REG_WRITE ((MV64360_ETH_RX_CURRENT_QUEUE_DESC_PTR_0
1566 (eth_port_num)
1567 + (4 * queue)),
1568 ((unsigned int) p_rx_curr_desc));
1569
1570 if (p_rx_curr_desc != NULL)
1571 /* Add the assigned Ethernet address to the port's address table */
1572 eth_port_uc_addr_set (p_eth_port_ctrl->port_num,
1573 p_eth_port_ctrl->port_mac_addr,
1574 queue);
1575 }
1576
1577 /* Assign port configuration and command. */
1578 MV_REG_WRITE (MV64360_ETH_PORT_CONFIG_REG (eth_port_num),
1579 p_eth_port_ctrl->port_config);
1580
1581 MV_REG_WRITE (MV64360_ETH_PORT_CONFIG_EXTEND_REG (eth_port_num),
1582 p_eth_port_ctrl->port_config_extend);
1583
1584 MV_REG_WRITE (MV64360_ETH_PORT_SERIAL_CONTROL_REG (eth_port_num),
1585 p_eth_port_ctrl->port_serial_control);
1586
1587 MV_SET_REG_BITS (MV64360_ETH_PORT_SERIAL_CONTROL_REG (eth_port_num),
1588 ETH_SERIAL_PORT_ENABLE);
1589
1590 /* Assign port SDMA configuration */
1591 MV_REG_WRITE (MV64360_ETH_SDMA_CONFIG_REG (eth_port_num),
1592 p_eth_port_ctrl->port_sdma_config);
1593
1594 MV_REG_WRITE (MV64360_ETH_TX_QUEUE_0_TOKEN_BUCKET_COUNT
1595 (eth_port_num), 0x3fffffff);
1596 MV_REG_WRITE (MV64360_ETH_TX_QUEUE_0_TOKEN_BUCKET_CONFIG
1597 (eth_port_num), 0x03fffcff);
1598 /* Turn off the port/queue bandwidth limitation */
1599 MV_REG_WRITE (MV64360_ETH_MAXIMUM_TRANSMIT_UNIT (eth_port_num), 0x0);
1600
1601 /* Enable port Rx. */
1602 MV_REG_WRITE (MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG (eth_port_num),
1603 p_eth_port_ctrl->port_rx_queue_command);
1604
1605 /* Check if link is up */
1606 eth_port_read_smi_reg (eth_port_num, 1, &phy_reg_data);
1607
1608 if (!(phy_reg_data & 0x20))
1609 return false;
1610
1611 return true;
1612}
1613
1614/*******************************************************************************
1615* eth_port_uc_addr_set - This function Set the port Unicast address.
1616*
1617* DESCRIPTION:
1618* This function Set the port Ethernet MAC address.
1619*
1620* INPUT:
1621* ETH_PORT eth_port_num Port number.
1622* char * p_addr Address to be set
Wolfgang Denka1be4762008-05-20 16:00:29 +02001623* ETH_QUEUE queue Rx queue number for this MAC address.
wdenk5da7f2f2004-01-03 00:43:19 +00001624*
1625* OUTPUT:
1626* Set MAC address low and high registers. also calls eth_port_uc_addr()
1627* To set the unicast table with the proper information.
1628*
1629* RETURN:
1630* N/A.
1631*
1632*******************************************************************************/
1633static void eth_port_uc_addr_set (ETH_PORT eth_port_num,
1634 unsigned char *p_addr, ETH_QUEUE queue)
1635{
1636 unsigned int mac_h;
1637 unsigned int mac_l;
1638
1639 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1640 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) |
1641 (p_addr[2] << 8) | (p_addr[3] << 0);
1642
1643 MV_REG_WRITE (MV64360_ETH_MAC_ADDR_LOW (eth_port_num), mac_l);
1644 MV_REG_WRITE (MV64360_ETH_MAC_ADDR_HIGH (eth_port_num), mac_h);
1645
1646 /* Accept frames of this address */
1647 eth_port_uc_addr (eth_port_num, p_addr[5], queue, ACCEPT_MAC_ADDR);
1648
1649 return;
1650}
1651
1652/*******************************************************************************
1653* eth_port_uc_addr - This function Set the port unicast address table
1654*
1655* DESCRIPTION:
1656* This function locates the proper entry in the Unicast table for the
1657* specified MAC nibble and sets its properties according to function
1658* parameters.
1659*
1660* INPUT:
Wolfgang Denka1be4762008-05-20 16:00:29 +02001661* ETH_PORT eth_port_num Port number.
wdenk5da7f2f2004-01-03 00:43:19 +00001662* unsigned char uc_nibble Unicast MAC Address last nibble.
Wolfgang Denka1be4762008-05-20 16:00:29 +02001663* ETH_QUEUE queue Rx queue number for this MAC address.
1664* int option 0 = Add, 1 = remove address.
wdenk5da7f2f2004-01-03 00:43:19 +00001665*
1666* OUTPUT:
1667* This function add/removes MAC addresses from the port unicast address
1668* table.
1669*
1670* RETURN:
1671* true is output succeeded.
1672* false if option parameter is invalid.
1673*
1674*******************************************************************************/
1675static bool eth_port_uc_addr (ETH_PORT eth_port_num,
1676 unsigned char uc_nibble,
1677 ETH_QUEUE queue, int option)
1678{
1679 unsigned int unicast_reg;
1680 unsigned int tbl_offset;
1681 unsigned int reg_offset;
1682
1683 /* Locate the Unicast table entry */
1684 uc_nibble = (0xf & uc_nibble);
1685 tbl_offset = (uc_nibble / 4) * 4; /* Register offset from unicast table base */
1686 reg_offset = uc_nibble % 4; /* Entry offset within the above register */
1687
1688 switch (option) {
1689 case REJECT_MAC_ADDR:
1690 /* Clear accepts frame bit at specified unicast DA table entry */
1691 unicast_reg =
1692 MV_REG_READ ((MV64360_ETH_DA_FILTER_UNICAST_TABLE_BASE
1693 (eth_port_num)
1694 + tbl_offset));
1695
1696 unicast_reg &= (0x0E << (8 * reg_offset));
1697
1698 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_UNICAST_TABLE_BASE
1699 (eth_port_num)
1700 + tbl_offset), unicast_reg);
1701 break;
1702
1703 case ACCEPT_MAC_ADDR:
1704 /* Set accepts frame bit at unicast DA filter table entry */
1705 unicast_reg =
1706 MV_REG_READ ((MV64360_ETH_DA_FILTER_UNICAST_TABLE_BASE
1707 (eth_port_num)
1708 + tbl_offset));
1709
1710 unicast_reg |= ((0x01 | queue) << (8 * reg_offset));
1711
1712 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_UNICAST_TABLE_BASE
1713 (eth_port_num)
1714 + tbl_offset), unicast_reg);
1715
1716 break;
1717
1718 default:
1719 return false;
1720 }
1721 return true;
1722}
1723
1724#if 0 /* FIXME */
1725/*******************************************************************************
1726* eth_port_mc_addr - Multicast address settings.
1727*
1728* DESCRIPTION:
1729* This API controls the MV device MAC multicast support.
1730* The MV device supports multicast using two tables:
1731* 1) Special Multicast Table for MAC addresses of the form
1732* 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_fF).
1733* The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1734* Table entries in the DA-Filter table.
1735* In this case, the function calls eth_port_smc_addr() routine to set the
1736* Special Multicast Table.
1737* 2) Other Multicast Table for multicast of another type. A CRC-8bit
1738* is used as an index to the Other Multicast Table entries in the
1739* DA-Filter table.
1740* In this case, the function calculates the CRC-8bit value and calls
1741* eth_port_omc_addr() routine to set the Other Multicast Table.
1742* INPUT:
Wolfgang Denka1be4762008-05-20 16:00:29 +02001743* ETH_PORT eth_port_num Port number.
1744* unsigned char *p_addr Unicast MAC Address.
1745* ETH_QUEUE queue Rx queue number for this MAC address.
1746* int option 0 = Add, 1 = remove address.
wdenk5da7f2f2004-01-03 00:43:19 +00001747*
1748* OUTPUT:
1749* See description.
1750*
1751* RETURN:
1752* true is output succeeded.
1753* false if add_address_table_entry( ) failed.
1754*
1755*******************************************************************************/
1756static void eth_port_mc_addr (ETH_PORT eth_port_num,
1757 unsigned char *p_addr,
1758 ETH_QUEUE queue, int option)
1759{
1760 unsigned int mac_h;
1761 unsigned int mac_l;
1762 unsigned char crc_result = 0;
1763 int mac_array[48];
1764 int crc[8];
1765 int i;
1766
1767
1768 if ((p_addr[0] == 0x01) &&
1769 (p_addr[1] == 0x00) &&
1770 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00))
1771
1772 eth_port_smc_addr (eth_port_num, p_addr[5], queue, option);
1773 else {
1774 /* Calculate CRC-8 out of the given address */
1775 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1776 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1777 (p_addr[4] << 8) | (p_addr[5] << 0);
1778
1779 for (i = 0; i < 32; i++)
1780 mac_array[i] = (mac_l >> i) & 0x1;
1781 for (i = 32; i < 48; i++)
1782 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1783
1784
1785 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^
1786 mac_array[39] ^ mac_array[35] ^ mac_array[34] ^
1787 mac_array[31] ^ mac_array[30] ^ mac_array[28] ^
1788 mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1789 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1790 mac_array[12] ^ mac_array[8] ^ mac_array[7] ^
1791 mac_array[6] ^ mac_array[0];
1792
1793 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^
1794 mac_array[43] ^ mac_array[41] ^ mac_array[39] ^
1795 mac_array[36] ^ mac_array[34] ^ mac_array[32] ^
1796 mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1797 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^
1798 mac_array[21] ^ mac_array[20] ^ mac_array[18] ^
1799 mac_array[17] ^ mac_array[16] ^ mac_array[15] ^
1800 mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1801 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^
1802 mac_array[0];
1803
1804 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^
1805 mac_array[43] ^ mac_array[42] ^ mac_array[39] ^
1806 mac_array[37] ^ mac_array[34] ^ mac_array[33] ^
1807 mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1808 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^
1809 mac_array[15] ^ mac_array[13] ^ mac_array[12] ^
1810 mac_array[10] ^ mac_array[8] ^ mac_array[6] ^
1811 mac_array[2] ^ mac_array[1] ^ mac_array[0];
1812
1813 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^
1814 mac_array[43] ^ mac_array[40] ^ mac_array[38] ^
1815 mac_array[35] ^ mac_array[34] ^ mac_array[30] ^
1816 mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1817 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^
1818 mac_array[14] ^ mac_array[13] ^ mac_array[11] ^
1819 mac_array[9] ^ mac_array[7] ^ mac_array[3] ^
1820 mac_array[2] ^ mac_array[1];
1821
1822 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^
1823 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^
1824 mac_array[35] ^ mac_array[31] ^ mac_array[30] ^
1825 mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1826 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^
1827 mac_array[14] ^ mac_array[12] ^ mac_array[10] ^
1828 mac_array[8] ^ mac_array[4] ^ mac_array[3] ^
1829 mac_array[2];
1830
1831 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^
1832 mac_array[42] ^ mac_array[40] ^ mac_array[37] ^
1833 mac_array[36] ^ mac_array[32] ^ mac_array[31] ^
1834 mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1835 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^
1836 mac_array[15] ^ mac_array[13] ^ mac_array[11] ^
1837 mac_array[9] ^ mac_array[5] ^ mac_array[4] ^
1838 mac_array[3];
1839
1840 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^
1841 mac_array[41] ^ mac_array[38] ^ mac_array[37] ^
1842 mac_array[33] ^ mac_array[32] ^ mac_array[29] ^
1843 mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1844 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^
1845 mac_array[14] ^ mac_array[12] ^ mac_array[10] ^
1846 mac_array[6] ^ mac_array[5] ^ mac_array[4];
1847
1848 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^
1849 mac_array[39] ^ mac_array[38] ^ mac_array[34] ^
1850 mac_array[33] ^ mac_array[30] ^ mac_array[29] ^
1851 mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1852 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^
1853 mac_array[13] ^ mac_array[11] ^ mac_array[7] ^
1854 mac_array[6] ^ mac_array[5];
1855
1856 for (i = 0; i < 8; i++)
1857 crc_result = crc_result | (crc[i] << i);
1858
1859 eth_port_omc_addr (eth_port_num, crc_result, queue, option);
1860 }
1861 return;
1862}
1863
1864/*******************************************************************************
1865* eth_port_smc_addr - Special Multicast address settings.
1866*
1867* DESCRIPTION:
1868* This routine controls the MV device special MAC multicast support.
1869* The Special Multicast Table for MAC addresses supports MAC of the form
1870* 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_fF).
1871* The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1872* Table entries in the DA-Filter table.
1873* This function set the Special Multicast Table appropriate entry
1874* according to the argument given.
1875*
1876* INPUT:
Wolfgang Denka1be4762008-05-20 16:00:29 +02001877* ETH_PORT eth_port_num Port number.
1878* unsigned char mc_byte Multicast addr last byte (MAC DA[7:0] bits).
1879* ETH_QUEUE queue Rx queue number for this MAC address.
1880* int option 0 = Add, 1 = remove address.
wdenk5da7f2f2004-01-03 00:43:19 +00001881*
1882* OUTPUT:
1883* See description.
1884*
1885* RETURN:
1886* true is output succeeded.
1887* false if option parameter is invalid.
1888*
1889*******************************************************************************/
1890static bool eth_port_smc_addr (ETH_PORT eth_port_num,
1891 unsigned char mc_byte,
1892 ETH_QUEUE queue, int option)
1893{
1894 unsigned int smc_table_reg;
1895 unsigned int tbl_offset;
1896 unsigned int reg_offset;
1897
1898 /* Locate the SMC table entry */
1899 tbl_offset = (mc_byte / 4) * 4; /* Register offset from SMC table base */
1900 reg_offset = mc_byte % 4; /* Entry offset within the above register */
1901 queue &= 0x7;
1902
1903 switch (option) {
1904 case REJECT_MAC_ADDR:
1905 /* Clear accepts frame bit at specified Special DA table entry */
1906 smc_table_reg =
1907 MV_REG_READ ((MV64360_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
1908 smc_table_reg &= (0x0E << (8 * reg_offset));
1909
1910 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), smc_table_reg);
1911 break;
1912
1913 case ACCEPT_MAC_ADDR:
1914 /* Set accepts frame bit at specified Special DA table entry */
1915 smc_table_reg =
1916 MV_REG_READ ((MV64360_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
1917 smc_table_reg |= ((0x01 | queue) << (8 * reg_offset));
1918
1919 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), smc_table_reg);
1920 break;
1921
1922 default:
1923 return false;
1924 }
1925 return true;
1926}
1927
1928/*******************************************************************************
1929* eth_port_omc_addr - Multicast address settings.
1930*
1931* DESCRIPTION:
1932* This routine controls the MV device Other MAC multicast support.
1933* The Other Multicast Table is used for multicast of another type.
1934* A CRC-8bit is used as an index to the Other Multicast Table entries
1935* in the DA-Filter table.
1936* The function gets the CRC-8bit value from the calling routine and
1937* set the Other Multicast Table appropriate entry according to the
1938* CRC-8 argument given.
1939*
1940* INPUT:
Wolfgang Denka1be4762008-05-20 16:00:29 +02001941* ETH_PORT eth_port_num Port number.
1942* unsigned char crc8 A CRC-8bit (Polynomial: x^8+x^2+x^1+1).
1943* ETH_QUEUE queue Rx queue number for this MAC address.
1944* int option 0 = Add, 1 = remove address.
wdenk5da7f2f2004-01-03 00:43:19 +00001945*
1946* OUTPUT:
1947* See description.
1948*
1949* RETURN:
1950* true is output succeeded.
1951* false if option parameter is invalid.
1952*
1953*******************************************************************************/
1954static bool eth_port_omc_addr (ETH_PORT eth_port_num,
1955 unsigned char crc8,
1956 ETH_QUEUE queue, int option)
1957{
1958 unsigned int omc_table_reg;
1959 unsigned int tbl_offset;
1960 unsigned int reg_offset;
1961
1962 /* Locate the OMC table entry */
1963 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1964 reg_offset = crc8 % 4; /* Entry offset within the above register */
1965 queue &= 0x7;
1966
1967 switch (option) {
1968 case REJECT_MAC_ADDR:
1969 /* Clear accepts frame bit at specified Other DA table entry */
1970 omc_table_reg =
1971 MV_REG_READ ((MV64360_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
1972 omc_table_reg &= (0x0E << (8 * reg_offset));
1973
1974 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), omc_table_reg);
1975 break;
1976
1977 case ACCEPT_MAC_ADDR:
1978 /* Set accepts frame bit at specified Other DA table entry */
1979 omc_table_reg =
1980 MV_REG_READ ((MV64360_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset));
1981 omc_table_reg |= ((0x01 | queue) << (8 * reg_offset));
1982
1983 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + tbl_offset), omc_table_reg);
1984 break;
1985
1986 default:
1987 return false;
1988 }
1989 return true;
1990}
1991#endif
1992
1993/*******************************************************************************
1994* eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1995*
1996* DESCRIPTION:
1997* Go through all the DA filter tables (Unicast, Special Multicast & Other
1998* Multicast) and set each entry to 0.
1999*
2000* INPUT:
2001* ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2002*
2003* OUTPUT:
2004* Multicast and Unicast packets are rejected.
2005*
2006* RETURN:
2007* None.
2008*
2009*******************************************************************************/
2010static void eth_port_init_mac_tables (ETH_PORT eth_port_num)
2011{
2012 int table_index;
2013
2014 /* Clear DA filter unicast table (Ex_dFUT) */
2015 for (table_index = 0; table_index <= 0xC; table_index += 4)
2016 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_UNICAST_TABLE_BASE
2017 (eth_port_num) + table_index), 0);
2018
2019 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2020 /* Clear DA filter special multicast table (Ex_dFSMT) */
2021 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE (eth_port_num) + table_index), 0);
2022 /* Clear DA filter other multicast table (Ex_dFOMT) */
2023 MV_REG_WRITE ((MV64360_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE (eth_port_num) + table_index), 0);
2024 }
2025}
2026
2027/*******************************************************************************
2028* eth_clear_mib_counters - Clear all MIB counters
2029*
2030* DESCRIPTION:
2031* This function clears all MIB counters of a specific ethernet port.
2032* A read from the MIB counter will reset the counter.
2033*
2034* INPUT:
2035* ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2036*
2037* OUTPUT:
2038* After reading all MIB counters, the counters resets.
2039*
2040* RETURN:
2041* MIB counter value.
2042*
2043*******************************************************************************/
2044static void eth_clear_mib_counters (ETH_PORT eth_port_num)
2045{
2046 int i;
wdenk5da7f2f2004-01-03 00:43:19 +00002047
2048 /* Perform dummy reads from MIB counters */
2049 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
2050 i += 4)
Wolfgang Denk17725802011-11-29 22:17:48 +00002051 MV_REG_READ((MV64360_ETH_MIB_COUNTERS_BASE(eth_port_num) + i));
wdenk5da7f2f2004-01-03 00:43:19 +00002052
2053 return;
2054}
2055
2056/*******************************************************************************
2057* eth_read_mib_counter - Read a MIB counter
2058*
2059* DESCRIPTION:
2060* This function reads a MIB counter of a specific ethernet port.
2061* NOTE - If read from ETH_MIB_GOOD_OCTETS_RECEIVED_LOW, then the
2062* following read must be from ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH
2063* register. The same applies for ETH_MIB_GOOD_OCTETS_SENT_LOW and
2064* ETH_MIB_GOOD_OCTETS_SENT_HIGH
2065*
2066* INPUT:
2067* ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2068* unsigned int mib_offset MIB counter offset (use ETH_MIB_... macros).
2069*
2070* OUTPUT:
2071* After reading the MIB counter, the counter resets.
2072*
2073* RETURN:
2074* MIB counter value.
2075*
2076*******************************************************************************/
2077unsigned int eth_read_mib_counter (ETH_PORT eth_port_num,
2078 unsigned int mib_offset)
2079{
2080 return (MV_REG_READ (MV64360_ETH_MIB_COUNTERS_BASE (eth_port_num)
2081 + mib_offset));
2082}
2083
2084/*******************************************************************************
2085* ethernet_phy_set - Set the ethernet port PHY address.
2086*
2087* DESCRIPTION:
2088* This routine set the ethernet port PHY address according to given
2089* parameter.
2090*
2091* INPUT:
2092* ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2093*
2094* OUTPUT:
2095* Set PHY Address Register with given PHY address parameter.
2096*
2097* RETURN:
2098* None.
2099*
2100*******************************************************************************/
2101static void ethernet_phy_set (ETH_PORT eth_port_num, int phy_addr)
2102{
2103 unsigned int reg_data;
2104
2105 reg_data = MV_REG_READ (MV64360_ETH_PHY_ADDR_REG);
2106
2107 reg_data &= ~(0x1F << (5 * eth_port_num));
2108 reg_data |= (phy_addr << (5 * eth_port_num));
2109
2110 MV_REG_WRITE (MV64360_ETH_PHY_ADDR_REG, reg_data);
2111
2112 return;
2113}
2114
2115/*******************************************************************************
2116 * ethernet_phy_get - Get the ethernet port PHY address.
2117 *
2118 * DESCRIPTION:
2119 * This routine returns the given ethernet port PHY address.
2120 *
2121 * INPUT:
2122 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2123 *
2124 * OUTPUT:
2125 * None.
2126 *
2127 * RETURN:
2128 * PHY address.
2129 *
2130 *******************************************************************************/
2131static int ethernet_phy_get (ETH_PORT eth_port_num)
2132{
2133 unsigned int reg_data;
2134
2135 reg_data = MV_REG_READ (MV64360_ETH_PHY_ADDR_REG);
2136
2137 return ((reg_data >> (5 * eth_port_num)) & 0x1f);
2138}
2139
2140/*******************************************************************************
2141 * ethernet_phy_reset - Reset Ethernet port PHY.
2142 *
2143 * DESCRIPTION:
2144 * This routine utilize the SMI interface to reset the ethernet port PHY.
2145 * The routine waits until the link is up again or link up is timeout.
2146 *
2147 * INPUT:
2148 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2149 *
2150 * OUTPUT:
2151 * The ethernet port PHY renew its link.
2152 *
2153 * RETURN:
2154 * None.
2155 *
2156*******************************************************************************/
2157static bool ethernet_phy_reset (ETH_PORT eth_port_num)
2158{
2159 unsigned int time_out = 50;
2160 unsigned int phy_reg_data;
2161
2162 /* Reset the PHY */
2163 eth_port_read_smi_reg (eth_port_num, 0, &phy_reg_data);
2164 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2165 eth_port_write_smi_reg (eth_port_num, 0, phy_reg_data);
2166
2167 /* Poll on the PHY LINK */
2168 do {
2169 eth_port_read_smi_reg (eth_port_num, 1, &phy_reg_data);
2170
2171 if (time_out-- == 0)
2172 return false;
2173 }
2174 while (!(phy_reg_data & 0x20));
2175
2176 return true;
2177}
2178
2179/*******************************************************************************
2180 * eth_port_reset - Reset Ethernet port
2181 *
2182 * DESCRIPTION:
Wolfgang Denka1be4762008-05-20 16:00:29 +02002183 * This routine resets the chip by aborting any SDMA engine activity and
wdenk5da7f2f2004-01-03 00:43:19 +00002184 * clearing the MIB counters. The Receiver and the Transmit unit are in
2185 * idle state after this command is performed and the port is disabled.
2186 *
2187 * INPUT:
2188 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2189 *
2190 * OUTPUT:
2191 * Channel activity is halted.
2192 *
2193 * RETURN:
2194 * None.
2195 *
2196 *******************************************************************************/
2197static void eth_port_reset (ETH_PORT eth_port_num)
2198{
2199 unsigned int reg_data;
2200
2201 /* Stop Tx port activity. Check port Tx activity. */
2202 reg_data =
2203 MV_REG_READ (MV64360_ETH_TRANSMIT_QUEUE_COMMAND_REG
2204 (eth_port_num));
2205
2206 if (reg_data & 0xFF) {
2207 /* Issue stop command for active channels only */
2208 MV_REG_WRITE (MV64360_ETH_TRANSMIT_QUEUE_COMMAND_REG
2209 (eth_port_num), (reg_data << 8));
2210
2211 /* Wait for all Tx activity to terminate. */
2212 do {
2213 /* Check port cause register that all Tx queues are stopped */
2214 reg_data =
2215 MV_REG_READ
2216 (MV64360_ETH_TRANSMIT_QUEUE_COMMAND_REG
2217 (eth_port_num));
2218 }
2219 while (reg_data & 0xFF);
2220 }
2221
2222 /* Stop Rx port activity. Check port Rx activity. */
2223 reg_data =
2224 MV_REG_READ (MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG
2225 (eth_port_num));
2226
2227 if (reg_data & 0xFF) {
2228 /* Issue stop command for active channels only */
2229 MV_REG_WRITE (MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG
2230 (eth_port_num), (reg_data << 8));
2231
2232 /* Wait for all Rx activity to terminate. */
2233 do {
2234 /* Check port cause register that all Rx queues are stopped */
2235 reg_data =
2236 MV_REG_READ
2237 (MV64360_ETH_RECEIVE_QUEUE_COMMAND_REG
2238 (eth_port_num));
2239 }
2240 while (reg_data & 0xFF);
2241 }
2242
2243
2244 /* Clear all MIB counters */
2245 eth_clear_mib_counters (eth_port_num);
2246
2247 /* Reset the Enable bit in the Configuration Register */
2248 reg_data =
2249 MV_REG_READ (MV64360_ETH_PORT_SERIAL_CONTROL_REG
2250 (eth_port_num));
2251 reg_data &= ~ETH_SERIAL_PORT_ENABLE;
2252 MV_REG_WRITE (MV64360_ETH_PORT_SERIAL_CONTROL_REG (eth_port_num),
2253 reg_data);
2254
2255 return;
2256}
2257
2258#if 0 /* Not needed here */
2259/*******************************************************************************
2260 * ethernet_set_config_reg - Set specified bits in configuration register.
2261 *
2262 * DESCRIPTION:
2263 * This function sets specified bits in the given ethernet
2264 * configuration register.
2265 *
2266 * INPUT:
2267 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2268 * unsigned int value 32 bit value.
2269 *
2270 * OUTPUT:
2271 * The set bits in the value parameter are set in the configuration
2272 * register.
2273 *
2274 * RETURN:
2275 * None.
2276 *
2277 *******************************************************************************/
2278static void ethernet_set_config_reg (ETH_PORT eth_port_num,
2279 unsigned int value)
2280{
2281 unsigned int eth_config_reg;
2282
2283 eth_config_reg =
2284 MV_REG_READ (MV64360_ETH_PORT_CONFIG_REG (eth_port_num));
2285 eth_config_reg |= value;
2286 MV_REG_WRITE (MV64360_ETH_PORT_CONFIG_REG (eth_port_num),
2287 eth_config_reg);
2288
2289 return;
2290}
2291#endif
2292
2293#if 0 /* FIXME */
2294/*******************************************************************************
2295 * ethernet_reset_config_reg - Reset specified bits in configuration register.
2296 *
2297 * DESCRIPTION:
2298 * This function resets specified bits in the given Ethernet
2299 * configuration register.
2300 *
2301 * INPUT:
2302 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2303 * unsigned int value 32 bit value.
2304 *
2305 * OUTPUT:
2306 * The set bits in the value parameter are reset in the configuration
2307 * register.
2308 *
2309 * RETURN:
2310 * None.
2311 *
2312 *******************************************************************************/
2313static void ethernet_reset_config_reg (ETH_PORT eth_port_num,
2314 unsigned int value)
2315{
2316 unsigned int eth_config_reg;
2317
2318 eth_config_reg = MV_REG_READ (MV64360_ETH_PORT_CONFIG_EXTEND_REG
2319 (eth_port_num));
2320 eth_config_reg &= ~value;
2321 MV_REG_WRITE (MV64360_ETH_PORT_CONFIG_EXTEND_REG (eth_port_num),
2322 eth_config_reg);
2323
2324 return;
2325}
2326#endif
2327
2328#if 0 /* Not needed here */
2329/*******************************************************************************
2330 * ethernet_get_config_reg - Get the port configuration register
2331 *
2332 * DESCRIPTION:
2333 * This function returns the configuration register value of the given
2334 * ethernet port.
2335 *
2336 * INPUT:
2337 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2338 *
2339 * OUTPUT:
2340 * None.
2341 *
2342 * RETURN:
2343 * Port configuration register value.
2344 *
2345 *******************************************************************************/
2346static unsigned int ethernet_get_config_reg (ETH_PORT eth_port_num)
2347{
2348 unsigned int eth_config_reg;
2349
2350 eth_config_reg = MV_REG_READ (MV64360_ETH_PORT_CONFIG_EXTEND_REG
2351 (eth_port_num));
2352 return eth_config_reg;
2353}
2354
2355#endif
2356
2357/*******************************************************************************
2358 * eth_port_read_smi_reg - Read PHY registers
2359 *
2360 * DESCRIPTION:
2361 * This routine utilize the SMI interface to interact with the PHY in
2362 * order to perform PHY register read.
2363 *
2364 * INPUT:
2365 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2366 * unsigned int phy_reg PHY register address offset.
2367 * unsigned int *value Register value buffer.
2368 *
2369 * OUTPUT:
2370 * Write the value of a specified PHY register into given buffer.
2371 *
2372 * RETURN:
2373 * false if the PHY is busy or read data is not in valid state.
2374 * true otherwise.
2375 *
2376 *******************************************************************************/
2377static bool eth_port_read_smi_reg (ETH_PORT eth_port_num,
2378 unsigned int phy_reg, unsigned int *value)
2379{
2380 unsigned int reg_value;
2381 unsigned int time_out = PHY_BUSY_TIMEOUT;
2382 int phy_addr;
2383
2384 phy_addr = ethernet_phy_get (eth_port_num);
2385/* printf(" Phy-Port %d has addess %d \n",eth_port_num, phy_addr );*/
2386
2387 /* first check that it is not busy */
2388 do {
2389 reg_value = MV_REG_READ (MV64360_ETH_SMI_REG);
2390 if (time_out-- == 0) {
2391 return false;
2392 }
2393 }
2394 while (reg_value & ETH_SMI_BUSY);
2395
2396 /* not busy */
2397
2398 MV_REG_WRITE (MV64360_ETH_SMI_REG,
2399 (phy_addr << 16) | (phy_reg << 21) |
2400 ETH_SMI_OPCODE_READ);
2401
2402 time_out = PHY_BUSY_TIMEOUT; /* initialize the time out var again */
2403
2404 do {
2405 reg_value = MV_REG_READ (MV64360_ETH_SMI_REG);
2406 if (time_out-- == 0) {
2407 return false;
2408 }
2409 }
2410 while ((reg_value & ETH_SMI_READ_VALID) != ETH_SMI_READ_VALID); /* Bit set equ operation done */
2411
2412 /* Wait for the data to update in the SMI register */
2413#define PHY_UPDATE_TIMEOUT 10000
2414 for (time_out = 0; time_out < PHY_UPDATE_TIMEOUT; time_out++);
2415
2416 reg_value = MV_REG_READ (MV64360_ETH_SMI_REG);
2417
2418 *value = reg_value & 0xffff;
2419
2420 return true;
2421}
2422
2423/*******************************************************************************
2424 * eth_port_write_smi_reg - Write to PHY registers
2425 *
2426 * DESCRIPTION:
2427 * This routine utilize the SMI interface to interact with the PHY in
2428 * order to perform writes to PHY registers.
2429 *
2430 * INPUT:
2431 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2432 * unsigned int phy_reg PHY register address offset.
2433 * unsigned int value Register value.
2434 *
2435 * OUTPUT:
2436 * Write the given value to the specified PHY register.
2437 *
2438 * RETURN:
2439 * false if the PHY is busy.
2440 * true otherwise.
2441 *
2442 *******************************************************************************/
2443static bool eth_port_write_smi_reg (ETH_PORT eth_port_num,
2444 unsigned int phy_reg, unsigned int value)
2445{
2446 unsigned int reg_value;
2447 unsigned int time_out = PHY_BUSY_TIMEOUT;
2448 int phy_addr;
2449
2450 phy_addr = ethernet_phy_get (eth_port_num);
2451
2452 /* first check that it is not busy */
2453 do {
2454 reg_value = MV_REG_READ (MV64360_ETH_SMI_REG);
2455 if (time_out-- == 0) {
2456 return false;
2457 }
2458 }
2459 while (reg_value & ETH_SMI_BUSY);
2460
2461 /* not busy */
2462 MV_REG_WRITE (MV64360_ETH_SMI_REG,
2463 (phy_addr << 16) | (phy_reg << 21) |
2464 ETH_SMI_OPCODE_WRITE | (value & 0xffff));
2465 return true;
2466}
2467
2468/*******************************************************************************
2469 * eth_set_access_control - Config address decode parameters for Ethernet unit
2470 *
2471 * DESCRIPTION:
2472 * This function configures the address decode parameters for the Gigabit
2473 * Ethernet Controller according the given parameters struct.
2474 *
2475 * INPUT:
2476 * ETH_PORT eth_port_num Ethernet Port number. See ETH_PORT enum.
2477 * ETH_WIN_PARAM *param Address decode parameter struct.
2478 *
2479 * OUTPUT:
2480 * An access window is opened using the given access parameters.
2481 *
2482 * RETURN:
2483 * None.
2484 *
2485 *******************************************************************************/
2486static void eth_set_access_control (ETH_PORT eth_port_num,
2487 ETH_WIN_PARAM * param)
2488{
2489 unsigned int access_prot_reg;
2490
2491 /* Set access control register */
2492 access_prot_reg = MV_REG_READ (MV64360_ETH_ACCESS_PROTECTION_REG
2493 (eth_port_num));
2494 access_prot_reg &= (~(3 << (param->win * 2))); /* clear window permission */
2495 access_prot_reg |= (param->access_ctrl << (param->win * 2));
2496 MV_REG_WRITE (MV64360_ETH_ACCESS_PROTECTION_REG (eth_port_num),
2497 access_prot_reg);
2498
2499 /* Set window Size reg (SR) */
2500 MV_REG_WRITE ((MV64360_ETH_SIZE_REG_0 +
2501 (ETH_SIZE_REG_GAP * param->win)),
2502 (((param->size / 0x10000) - 1) << 16));
2503
2504 /* Set window Base address reg (BA) */
2505 MV_REG_WRITE ((MV64360_ETH_BAR_0 + (ETH_BAR_GAP * param->win)),
2506 (param->target | param->attributes | param->base_addr));
2507 /* High address remap reg (HARR) */
2508 if (param->win < 4)
2509 MV_REG_WRITE ((MV64360_ETH_HIGH_ADDR_REMAP_REG_0 +
2510 (ETH_HIGH_ADDR_REMAP_REG_GAP * param->win)),
2511 param->high_addr);
2512
2513 /* Base address enable reg (BARER) */
2514 if (param->enable == 1)
2515 MV_RESET_REG_BITS (MV64360_ETH_BASE_ADDR_ENABLE_REG,
2516 (1 << param->win));
2517 else
2518 MV_SET_REG_BITS (MV64360_ETH_BASE_ADDR_ENABLE_REG,
2519 (1 << param->win));
2520}
2521
2522/*******************************************************************************
2523 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
2524 *
2525 * DESCRIPTION:
2526 * This function prepares a Rx chained list of descriptors and packet
2527 * buffers in a form of a ring. The routine must be called after port
2528 * initialization routine and before port start routine.
2529 * The Ethernet SDMA engine uses CPU bus addresses to access the various
2530 * devices in the system (i.e. DRAM). This function uses the ethernet
2531 * struct 'virtual to physical' routine (set by the user) to set the ring
2532 * with physical addresses.
2533 *
2534 * INPUT:
2535 * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
Wolfgang Denka1be4762008-05-20 16:00:29 +02002536 * ETH_QUEUE rx_queue Number of Rx queue.
2537 * int rx_desc_num Number of Rx descriptors
2538 * int rx_buff_size Size of Rx buffer
wdenk5da7f2f2004-01-03 00:43:19 +00002539 * unsigned int rx_desc_base_addr Rx descriptors memory area base addr.
2540 * unsigned int rx_buff_base_addr Rx buffer memory area base addr.
2541 *
2542 * OUTPUT:
2543 * The routine updates the Ethernet port control struct with information
2544 * regarding the Rx descriptors and buffers.
2545 *
2546 * RETURN:
2547 * false if the given descriptors memory area is not aligned according to
2548 * Ethernet SDMA specifications.
2549 * true otherwise.
2550 *
2551 *******************************************************************************/
2552static bool ether_init_rx_desc_ring (ETH_PORT_INFO * p_eth_port_ctrl,
2553 ETH_QUEUE rx_queue,
2554 int rx_desc_num,
2555 int rx_buff_size,
2556 unsigned int rx_desc_base_addr,
2557 unsigned int rx_buff_base_addr)
2558{
2559 ETH_RX_DESC *p_rx_desc;
2560 ETH_RX_DESC *p_rx_prev_desc; /* pointer to link with the last descriptor */
2561 unsigned int buffer_addr;
2562 int ix; /* a counter */
2563
2564
2565 p_rx_desc = (ETH_RX_DESC *) rx_desc_base_addr;
2566 p_rx_prev_desc = p_rx_desc;
2567 buffer_addr = rx_buff_base_addr;
2568
2569 /* Rx desc Must be 4LW aligned (i.e. Descriptor_Address[3:0]=0000). */
2570 if (rx_buff_base_addr & 0xF)
2571 return false;
2572
2573 /* Rx buffers are limited to 64K bytes and Minimum size is 8 bytes */
2574 if ((rx_buff_size < 8) || (rx_buff_size > RX_BUFFER_MAX_SIZE))
2575 return false;
2576
2577 /* Rx buffers must be 64-bit aligned. */
2578 if ((rx_buff_base_addr + rx_buff_size) & 0x7)
2579 return false;
2580
2581 /* initialize the Rx descriptors ring */
2582 for (ix = 0; ix < rx_desc_num; ix++) {
2583 p_rx_desc->buf_size = rx_buff_size;
2584 p_rx_desc->byte_cnt = 0x0000;
2585 p_rx_desc->cmd_sts =
2586 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
2587 p_rx_desc->next_desc_ptr =
2588 ((unsigned int) p_rx_desc) + RX_DESC_ALIGNED_SIZE;
2589 p_rx_desc->buf_ptr = buffer_addr;
2590 p_rx_desc->return_info = 0x00000000;
2591 D_CACHE_FLUSH_LINE (p_rx_desc, 0);
2592 buffer_addr += rx_buff_size;
2593 p_rx_prev_desc = p_rx_desc;
2594 p_rx_desc = (ETH_RX_DESC *)
2595 ((unsigned int) p_rx_desc + RX_DESC_ALIGNED_SIZE);
2596 }
2597
2598 /* Closing Rx descriptors ring */
2599 p_rx_prev_desc->next_desc_ptr = (rx_desc_base_addr);
2600 D_CACHE_FLUSH_LINE (p_rx_prev_desc, 0);
2601
2602 /* Save Rx desc pointer to driver struct. */
2603 CURR_RFD_SET ((ETH_RX_DESC *) rx_desc_base_addr, rx_queue);
2604 USED_RFD_SET ((ETH_RX_DESC *) rx_desc_base_addr, rx_queue);
2605
2606 p_eth_port_ctrl->p_rx_desc_area_base[rx_queue] =
2607 (ETH_RX_DESC *) rx_desc_base_addr;
2608 p_eth_port_ctrl->rx_desc_area_size[rx_queue] =
2609 rx_desc_num * RX_DESC_ALIGNED_SIZE;
2610
2611 p_eth_port_ctrl->port_rx_queue_command |= (1 << rx_queue);
2612
2613 return true;
2614}
2615
2616/*******************************************************************************
2617 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
2618 *
2619 * DESCRIPTION:
2620 * This function prepares a Tx chained list of descriptors and packet
2621 * buffers in a form of a ring. The routine must be called after port
2622 * initialization routine and before port start routine.
2623 * The Ethernet SDMA engine uses CPU bus addresses to access the various
2624 * devices in the system (i.e. DRAM). This function uses the ethernet
2625 * struct 'virtual to physical' routine (set by the user) to set the ring
2626 * with physical addresses.
2627 *
2628 * INPUT:
2629 * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
Wolfgang Denka1be4762008-05-20 16:00:29 +02002630 * ETH_QUEUE tx_queue Number of Tx queue.
2631 * int tx_desc_num Number of Tx descriptors
2632 * int tx_buff_size Size of Tx buffer
wdenk5da7f2f2004-01-03 00:43:19 +00002633 * unsigned int tx_desc_base_addr Tx descriptors memory area base addr.
2634 * unsigned int tx_buff_base_addr Tx buffer memory area base addr.
2635 *
2636 * OUTPUT:
2637 * The routine updates the Ethernet port control struct with information
2638 * regarding the Tx descriptors and buffers.
2639 *
2640 * RETURN:
2641 * false if the given descriptors memory area is not aligned according to
2642 * Ethernet SDMA specifications.
2643 * true otherwise.
2644 *
2645 *******************************************************************************/
2646static bool ether_init_tx_desc_ring (ETH_PORT_INFO * p_eth_port_ctrl,
2647 ETH_QUEUE tx_queue,
2648 int tx_desc_num,
2649 int tx_buff_size,
2650 unsigned int tx_desc_base_addr,
2651 unsigned int tx_buff_base_addr)
2652{
2653
2654 ETH_TX_DESC *p_tx_desc;
2655 ETH_TX_DESC *p_tx_prev_desc;
2656 unsigned int buffer_addr;
2657 int ix; /* a counter */
2658
2659
2660 /* save the first desc pointer to link with the last descriptor */
2661 p_tx_desc = (ETH_TX_DESC *) tx_desc_base_addr;
2662 p_tx_prev_desc = p_tx_desc;
2663 buffer_addr = tx_buff_base_addr;
2664
2665 /* Tx desc Must be 4LW aligned (i.e. Descriptor_Address[3:0]=0000). */
2666 if (tx_buff_base_addr & 0xF)
2667 return false;
2668
2669 /* Tx buffers are limited to 64K bytes and Minimum size is 8 bytes */
2670 if ((tx_buff_size > TX_BUFFER_MAX_SIZE)
2671 || (tx_buff_size < TX_BUFFER_MIN_SIZE))
2672 return false;
2673
2674 /* Initialize the Tx descriptors ring */
2675 for (ix = 0; ix < tx_desc_num; ix++) {
2676 p_tx_desc->byte_cnt = 0x0000;
2677 p_tx_desc->l4i_chk = 0x0000;
2678 p_tx_desc->cmd_sts = 0x00000000;
2679 p_tx_desc->next_desc_ptr =
2680 ((unsigned int) p_tx_desc) + TX_DESC_ALIGNED_SIZE;
2681
2682 p_tx_desc->buf_ptr = buffer_addr;
2683 p_tx_desc->return_info = 0x00000000;
2684 D_CACHE_FLUSH_LINE (p_tx_desc, 0);
2685 buffer_addr += tx_buff_size;
2686 p_tx_prev_desc = p_tx_desc;
2687 p_tx_desc = (ETH_TX_DESC *)
2688 ((unsigned int) p_tx_desc + TX_DESC_ALIGNED_SIZE);
2689
2690 }
2691 /* Closing Tx descriptors ring */
2692 p_tx_prev_desc->next_desc_ptr = tx_desc_base_addr;
2693 D_CACHE_FLUSH_LINE (p_tx_prev_desc, 0);
2694 /* Set Tx desc pointer in driver struct. */
2695 CURR_TFD_SET ((ETH_TX_DESC *) tx_desc_base_addr, tx_queue);
2696 USED_TFD_SET ((ETH_TX_DESC *) tx_desc_base_addr, tx_queue);
2697
2698 /* Init Tx ring base and size parameters */
2699 p_eth_port_ctrl->p_tx_desc_area_base[tx_queue] =
2700 (ETH_TX_DESC *) tx_desc_base_addr;
2701 p_eth_port_ctrl->tx_desc_area_size[tx_queue] =
2702 (tx_desc_num * TX_DESC_ALIGNED_SIZE);
2703
2704 /* Add the queue to the list of Tx queues of this port */
2705 p_eth_port_ctrl->port_tx_queue_command |= (1 << tx_queue);
2706
2707 return true;
2708}
2709
2710/*******************************************************************************
2711 * eth_port_send - Send an Ethernet packet
2712 *
2713 * DESCRIPTION:
2714 * This routine send a given packet described by p_pktinfo parameter. It
2715 * supports transmitting of a packet spaned over multiple buffers. The
2716 * routine updates 'curr' and 'first' indexes according to the packet
2717 * segment passed to the routine. In case the packet segment is first,
2718 * the 'first' index is update. In any case, the 'curr' index is updated.
2719 * If the routine get into Tx resource error it assigns 'curr' index as
2720 * 'first'. This way the function can abort Tx process of multiple
2721 * descriptors per packet.
2722 *
2723 * INPUT:
2724 * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
Wolfgang Denka1be4762008-05-20 16:00:29 +02002725 * ETH_QUEUE tx_queue Number of Tx queue.
wdenk5da7f2f2004-01-03 00:43:19 +00002726 * PKT_INFO *p_pkt_info User packet buffer.
2727 *
2728 * OUTPUT:
2729 * Tx ring 'curr' and 'first' indexes are updated.
2730 *
2731 * RETURN:
2732 * ETH_QUEUE_FULL in case of Tx resource error.
2733 * ETH_ERROR in case the routine can not access Tx desc ring.
2734 * ETH_QUEUE_LAST_RESOURCE if the routine uses the last Tx resource.
2735 * ETH_OK otherwise.
2736 *
2737 *******************************************************************************/
2738static ETH_FUNC_RET_STATUS eth_port_send (ETH_PORT_INFO * p_eth_port_ctrl,
2739 ETH_QUEUE tx_queue,
2740 PKT_INFO * p_pkt_info)
2741{
2742 volatile ETH_TX_DESC *p_tx_desc_first;
2743 volatile ETH_TX_DESC *p_tx_desc_curr;
2744 volatile ETH_TX_DESC *p_tx_next_desc_curr;
2745 volatile ETH_TX_DESC *p_tx_desc_used;
2746 unsigned int command_status;
2747
2748 /* Do not process Tx ring in case of Tx ring resource error */
2749 if (p_eth_port_ctrl->tx_resource_err[tx_queue] == true)
2750 return ETH_QUEUE_FULL;
2751
2752 /* Get the Tx Desc ring indexes */
2753 CURR_TFD_GET (p_tx_desc_curr, tx_queue);
2754 USED_TFD_GET (p_tx_desc_used, tx_queue);
2755
2756 if (p_tx_desc_curr == NULL)
2757 return ETH_ERROR;
2758
2759 /* The following parameters are used to save readings from memory */
2760 p_tx_next_desc_curr = TX_NEXT_DESC_PTR (p_tx_desc_curr, tx_queue);
2761 command_status = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC;
2762
2763 if (command_status & (ETH_TX_FIRST_DESC)) {
2764 /* Update first desc */
2765 FIRST_TFD_SET (p_tx_desc_curr, tx_queue);
2766 p_tx_desc_first = p_tx_desc_curr;
2767 } else {
2768 FIRST_TFD_GET (p_tx_desc_first, tx_queue);
2769 command_status |= ETH_BUFFER_OWNED_BY_DMA;
2770 }
2771
2772 /* Buffers with a payload smaller than 8 bytes must be aligned to 64-bit */
2773 /* boundary. We use the memory allocated for Tx descriptor. This memory */
2774 /* located in TX_BUF_OFFSET_IN_DESC offset within the Tx descriptor. */
2775 if (p_pkt_info->byte_cnt <= 8) {
2776 printf ("You have failed in the < 8 bytes errata - fixme\n"); /* RABEEH - TBD */
2777 return ETH_ERROR;
2778
2779 p_tx_desc_curr->buf_ptr =
2780 (unsigned int) p_tx_desc_curr + TX_BUF_OFFSET_IN_DESC;
2781 eth_b_copy (p_pkt_info->buf_ptr, p_tx_desc_curr->buf_ptr,
2782 p_pkt_info->byte_cnt);
2783 } else
2784 p_tx_desc_curr->buf_ptr = p_pkt_info->buf_ptr;
2785
2786 p_tx_desc_curr->byte_cnt = p_pkt_info->byte_cnt;
2787 p_tx_desc_curr->return_info = p_pkt_info->return_info;
2788
2789 if (p_pkt_info->cmd_sts & (ETH_TX_LAST_DESC)) {
2790 /* Set last desc with DMA ownership and interrupt enable. */
2791 p_tx_desc_curr->cmd_sts = command_status |
2792 ETH_BUFFER_OWNED_BY_DMA | ETH_TX_ENABLE_INTERRUPT;
2793
2794 if (p_tx_desc_curr != p_tx_desc_first)
2795 p_tx_desc_first->cmd_sts |= ETH_BUFFER_OWNED_BY_DMA;
2796
2797 /* Flush CPU pipe */
2798
2799 D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_curr, 0);
2800 D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_first, 0);
2801 CPU_PIPE_FLUSH;
2802
2803 /* Apply send command */
2804 ETH_ENABLE_TX_QUEUE (tx_queue, p_eth_port_ctrl->port_num);
2805
2806 /* Finish Tx packet. Update first desc in case of Tx resource error */
2807 p_tx_desc_first = p_tx_next_desc_curr;
2808 FIRST_TFD_SET (p_tx_desc_first, tx_queue);
2809
2810 } else {
2811 p_tx_desc_curr->cmd_sts = command_status;
2812 D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_curr, 0);
2813 }
2814
2815 /* Check for ring index overlap in the Tx desc ring */
2816 if (p_tx_next_desc_curr == p_tx_desc_used) {
2817 /* Update the current descriptor */
2818 CURR_TFD_SET (p_tx_desc_first, tx_queue);
2819
2820 p_eth_port_ctrl->tx_resource_err[tx_queue] = true;
2821 return ETH_QUEUE_LAST_RESOURCE;
2822 } else {
2823 /* Update the current descriptor */
2824 CURR_TFD_SET (p_tx_next_desc_curr, tx_queue);
2825 return ETH_OK;
2826 }
2827}
2828
2829/*******************************************************************************
2830 * eth_tx_return_desc - Free all used Tx descriptors
2831 *
2832 * DESCRIPTION:
2833 * This routine returns the transmitted packet information to the caller.
2834 * It uses the 'first' index to support Tx desc return in case a transmit
2835 * of a packet spanned over multiple buffer still in process.
2836 * In case the Tx queue was in "resource error" condition, where there are
2837 * no available Tx resources, the function resets the resource error flag.
2838 *
2839 * INPUT:
2840 * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
Wolfgang Denka1be4762008-05-20 16:00:29 +02002841 * ETH_QUEUE tx_queue Number of Tx queue.
wdenk5da7f2f2004-01-03 00:43:19 +00002842 * PKT_INFO *p_pkt_info User packet buffer.
2843 *
2844 * OUTPUT:
2845 * Tx ring 'first' and 'used' indexes are updated.
2846 *
2847 * RETURN:
2848 * ETH_ERROR in case the routine can not access Tx desc ring.
2849 * ETH_RETRY in case there is transmission in process.
2850 * ETH_END_OF_JOB if the routine has nothing to release.
2851 * ETH_OK otherwise.
2852 *
2853 *******************************************************************************/
2854static ETH_FUNC_RET_STATUS eth_tx_return_desc (ETH_PORT_INFO *
2855 p_eth_port_ctrl,
2856 ETH_QUEUE tx_queue,
2857 PKT_INFO * p_pkt_info)
2858{
2859 volatile ETH_TX_DESC *p_tx_desc_used = NULL;
2860 volatile ETH_TX_DESC *p_tx_desc_first = NULL;
2861 unsigned int command_status;
2862
2863
2864 /* Get the Tx Desc ring indexes */
2865 USED_TFD_GET (p_tx_desc_used, tx_queue);
2866 FIRST_TFD_GET (p_tx_desc_first, tx_queue);
2867
2868
2869 /* Sanity check */
2870 if (p_tx_desc_used == NULL)
2871 return ETH_ERROR;
2872
2873 command_status = p_tx_desc_used->cmd_sts;
2874
2875 /* Still transmitting... */
2876 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
2877 D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_used, 0);
2878 return ETH_RETRY;
2879 }
2880
2881 /* Stop release. About to overlap the current available Tx descriptor */
2882 if ((p_tx_desc_used == p_tx_desc_first) &&
2883 (p_eth_port_ctrl->tx_resource_err[tx_queue] == false)) {
2884 D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_used, 0);
2885 return ETH_END_OF_JOB;
2886 }
2887
2888 /* Pass the packet information to the caller */
2889 p_pkt_info->cmd_sts = command_status;
2890 p_pkt_info->return_info = p_tx_desc_used->return_info;
2891 p_tx_desc_used->return_info = 0;
2892
2893 /* Update the next descriptor to release. */
2894 USED_TFD_SET (TX_NEXT_DESC_PTR (p_tx_desc_used, tx_queue), tx_queue);
2895
2896 /* Any Tx return cancels the Tx resource error status */
2897 if (p_eth_port_ctrl->tx_resource_err[tx_queue] == true)
2898 p_eth_port_ctrl->tx_resource_err[tx_queue] = false;
2899
2900 D_CACHE_FLUSH_LINE ((unsigned int) p_tx_desc_used, 0);
2901
2902 return ETH_OK;
2903
2904}
2905
2906/*******************************************************************************
2907 * eth_port_receive - Get received information from Rx ring.
2908 *
2909 * DESCRIPTION:
Wolfgang Denka1be4762008-05-20 16:00:29 +02002910 * This routine returns the received data to the caller. There is no
wdenk5da7f2f2004-01-03 00:43:19 +00002911 * data copying during routine operation. All information is returned
2912 * using pointer to packet information struct passed from the caller.
2913 * If the routine exhausts Rx ring resources then the resource error flag
2914 * is set.
2915 *
2916 * INPUT:
2917 * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
Wolfgang Denka1be4762008-05-20 16:00:29 +02002918 * ETH_QUEUE rx_queue Number of Rx queue.
wdenk5da7f2f2004-01-03 00:43:19 +00002919 * PKT_INFO *p_pkt_info User packet buffer.
2920 *
2921 * OUTPUT:
2922 * Rx ring current and used indexes are updated.
2923 *
2924 * RETURN:
2925 * ETH_ERROR in case the routine can not access Rx desc ring.
2926 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
2927 * ETH_END_OF_JOB if there is no received data.
2928 * ETH_OK otherwise.
2929 *
2930 *******************************************************************************/
2931static ETH_FUNC_RET_STATUS eth_port_receive (ETH_PORT_INFO * p_eth_port_ctrl,
2932 ETH_QUEUE rx_queue,
2933 PKT_INFO * p_pkt_info)
2934{
2935 volatile ETH_RX_DESC *p_rx_curr_desc;
2936 volatile ETH_RX_DESC *p_rx_next_curr_desc;
2937 volatile ETH_RX_DESC *p_rx_used_desc;
2938 unsigned int command_status;
2939
2940 /* Do not process Rx ring in case of Rx ring resource error */
2941 if (p_eth_port_ctrl->rx_resource_err[rx_queue] == true) {
2942 printf ("\nRx Queue is full ...\n");
2943 return ETH_QUEUE_FULL;
2944 }
2945
2946 /* Get the Rx Desc ring 'curr and 'used' indexes */
2947 CURR_RFD_GET (p_rx_curr_desc, rx_queue);
2948 USED_RFD_GET (p_rx_used_desc, rx_queue);
2949
2950 /* Sanity check */
2951 if (p_rx_curr_desc == NULL)
2952 return ETH_ERROR;
2953
2954 /* The following parameters are used to save readings from memory */
2955 p_rx_next_curr_desc = RX_NEXT_DESC_PTR (p_rx_curr_desc, rx_queue);
2956 command_status = p_rx_curr_desc->cmd_sts;
2957
2958 /* Nothing to receive... */
2959 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Wolfgang Denka1be4762008-05-20 16:00:29 +02002960/* DP(printf("Rx: command_status: %08x\n", command_status)); */
wdenk5da7f2f2004-01-03 00:43:19 +00002961 D_CACHE_FLUSH_LINE ((unsigned int) p_rx_curr_desc, 0);
2962/* DP(printf("\nETH_END_OF_JOB ...\n"));*/
2963 return ETH_END_OF_JOB;
2964 }
2965
2966 p_pkt_info->byte_cnt = (p_rx_curr_desc->byte_cnt) - RX_BUF_OFFSET;
2967 p_pkt_info->cmd_sts = command_status;
2968 p_pkt_info->buf_ptr = (p_rx_curr_desc->buf_ptr) + RX_BUF_OFFSET;
2969 p_pkt_info->return_info = p_rx_curr_desc->return_info;
2970 p_pkt_info->l4i_chk = p_rx_curr_desc->buf_size; /* IP fragment indicator */
2971
2972 /* Clean the return info field to indicate that the packet has been */
2973 /* moved to the upper layers */
2974 p_rx_curr_desc->return_info = 0;
2975
2976 /* Update 'curr' in data structure */
2977 CURR_RFD_SET (p_rx_next_curr_desc, rx_queue);
2978
2979 /* Rx descriptors resource exhausted. Set the Rx ring resource error flag */
2980 if (p_rx_next_curr_desc == p_rx_used_desc)
2981 p_eth_port_ctrl->rx_resource_err[rx_queue] = true;
2982
2983 D_CACHE_FLUSH_LINE ((unsigned int) p_rx_curr_desc, 0);
2984 CPU_PIPE_FLUSH;
2985 return ETH_OK;
2986}
2987
2988/*******************************************************************************
2989 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
2990 *
2991 * DESCRIPTION:
2992 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
2993 * next 'used' descriptor and attached the returned buffer to it.
2994 * In case the Rx ring was in "resource error" condition, where there are
2995 * no available Rx resources, the function resets the resource error flag.
2996 *
2997 * INPUT:
2998 * ETH_PORT_INFO *p_eth_port_ctrl Ethernet Port Control srtuct.
Wolfgang Denka1be4762008-05-20 16:00:29 +02002999 * ETH_QUEUE rx_queue Number of Rx queue.
wdenk5da7f2f2004-01-03 00:43:19 +00003000 * PKT_INFO *p_pkt_info Information on the returned buffer.
3001 *
3002 * OUTPUT:
3003 * New available Rx resource in Rx descriptor ring.
3004 *
3005 * RETURN:
3006 * ETH_ERROR in case the routine can not access Rx desc ring.
3007 * ETH_OK otherwise.
3008 *
3009 *******************************************************************************/
3010static ETH_FUNC_RET_STATUS eth_rx_return_buff (ETH_PORT_INFO *
3011 p_eth_port_ctrl,
3012 ETH_QUEUE rx_queue,
3013 PKT_INFO * p_pkt_info)
3014{
3015 volatile ETH_RX_DESC *p_used_rx_desc; /* Where to return Rx resource */
3016
3017 /* Get 'used' Rx descriptor */
3018 USED_RFD_GET (p_used_rx_desc, rx_queue);
3019
3020 /* Sanity check */
3021 if (p_used_rx_desc == NULL)
3022 return ETH_ERROR;
3023
3024 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
3025 p_used_rx_desc->return_info = p_pkt_info->return_info;
3026 p_used_rx_desc->byte_cnt = p_pkt_info->byte_cnt;
3027 p_used_rx_desc->buf_size = MV64360_RX_BUFFER_SIZE; /* Reset Buffer size */
3028
3029 /* Flush the write pipe */
3030 CPU_PIPE_FLUSH;
3031
3032 /* Return the descriptor to DMA ownership */
3033 p_used_rx_desc->cmd_sts =
3034 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
3035
3036 /* Flush descriptor and CPU pipe */
3037 D_CACHE_FLUSH_LINE ((unsigned int) p_used_rx_desc, 0);
3038 CPU_PIPE_FLUSH;
3039
3040 /* Move the used descriptor pointer to the next descriptor */
3041 USED_RFD_SET (RX_NEXT_DESC_PTR (p_used_rx_desc, rx_queue), rx_queue);
3042
3043 /* Any Rx return cancels the Rx resource error status */
3044 if (p_eth_port_ctrl->rx_resource_err[rx_queue] == true)
3045 p_eth_port_ctrl->rx_resource_err[rx_queue] = false;
3046
3047 return ETH_OK;
3048}
3049
3050/*******************************************************************************
3051 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
3052 *
3053 * DESCRIPTION:
3054 * This routine sets the RX coalescing interrupt mechanism parameter.
3055 * This parameter is a timeout counter, that counts in 64 t_clk
3056 * chunks ; that when timeout event occurs a maskable interrupt
3057 * occurs.
3058 * The parameter is calculated using the tClk of the MV-643xx chip
3059 * , and the required delay of the interrupt in usec.
3060 *
3061 * INPUT:
3062 * ETH_PORT eth_port_num Ethernet port number
3063 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
3064 * unsigned int delay Delay in usec
3065 *
3066 * OUTPUT:
3067 * Interrupt coalescing mechanism value is set in MV-643xx chip.
3068 *
3069 * RETURN:
3070 * The interrupt coalescing value set in the gigE port.
3071 *
3072 *******************************************************************************/
3073#if 0 /* FIXME */
3074static unsigned int eth_port_set_rx_coal (ETH_PORT eth_port_num,
3075 unsigned int t_clk,
3076 unsigned int delay)
3077{
3078 unsigned int coal;
3079
3080 coal = ((t_clk / 1000000) * delay) / 64;
3081 /* Set RX Coalescing mechanism */
3082 MV_REG_WRITE (MV64360_ETH_SDMA_CONFIG_REG (eth_port_num),
3083 ((coal & 0x3fff) << 8) |
3084 (MV_REG_READ
3085 (MV64360_ETH_SDMA_CONFIG_REG (eth_port_num))
3086 & 0xffc000ff));
3087 return coal;
3088}
3089
3090#endif
3091/*******************************************************************************
3092 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
3093 *
3094 * DESCRIPTION:
3095 * This routine sets the TX coalescing interrupt mechanism parameter.
3096 * This parameter is a timeout counter, that counts in 64 t_clk
3097 * chunks ; that when timeout event occurs a maskable interrupt
3098 * occurs.
3099 * The parameter is calculated using the t_cLK frequency of the
3100 * MV-643xx chip and the required delay in the interrupt in uSec
3101 *
3102 * INPUT:
3103 * ETH_PORT eth_port_num Ethernet port number
3104 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
3105 * unsigned int delay Delay in uSeconds
3106 *
3107 * OUTPUT:
3108 * Interrupt coalescing mechanism value is set in MV-643xx chip.
3109 *
3110 * RETURN:
3111 * The interrupt coalescing value set in the gigE port.
3112 *
3113 *******************************************************************************/
3114#if 0 /* FIXME */
3115static unsigned int eth_port_set_tx_coal (ETH_PORT eth_port_num,
3116 unsigned int t_clk,
3117 unsigned int delay)
3118{
3119 unsigned int coal;
3120
3121 coal = ((t_clk / 1000000) * delay) / 64;
3122 /* Set TX Coalescing mechanism */
3123 MV_REG_WRITE (MV64360_ETH_TX_FIFO_URGENT_THRESHOLD_REG (eth_port_num),
3124 coal << 4);
3125 return coal;
3126}
3127#endif
3128
3129/*******************************************************************************
3130 * eth_b_copy - Copy bytes from source to destination
3131 *
3132 * DESCRIPTION:
3133 * This function supports the eight bytes limitation on Tx buffer size.
3134 * The routine will zero eight bytes starting from the destination address
3135 * followed by copying bytes from the source address to the destination.
3136 *
3137 * INPUT:
3138 * unsigned int src_addr 32 bit source address.
3139 * unsigned int dst_addr 32 bit destination address.
3140 * int byte_count Number of bytes to copy.
3141 *
3142 * OUTPUT:
3143 * See description.
3144 *
3145 * RETURN:
3146 * None.
3147 *
3148 *******************************************************************************/
3149static void eth_b_copy (unsigned int src_addr, unsigned int dst_addr,
3150 int byte_count)
3151{
3152 /* Zero the dst_addr area */
3153 *(unsigned int *) dst_addr = 0x0;
3154
3155 while (byte_count != 0) {
3156 *(char *) dst_addr = *(char *) src_addr;
3157 dst_addr++;
3158 src_addr++;
3159 byte_count--;
3160 }
3161}