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