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