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