Purge warnings from tg3.c
[people/xl0/gpxe-arm.git] / src / drivers / net / tg3.c
1 /* $Id$
2  * tg3.c: Broadcom Tigon3 ethernet driver.
3  *
4  * Copyright (C) 2001, 2002 David S. Miller (davem@redhat.com)
5  * Copyright (C) 2001, 2002 Jeff Garzik (jgarzik@mandrakesoft.com)
6  * Copyright (C) 2003 Eric Biederman (ebiederman@lnxi.com)  [etherboot port]
7  */
8
9 /* 11-13-2003   timlegge        Fix Issue with NetGear GA302T 
10  * 11-18-2003   ebiederm        Generalize NetGear Fix to what the code was supposed to be.
11  * 01-06-2005   Alf (Frederic Olivie) Add Dell bcm 5751 (0x1677) support
12  * 04-15-2005   Martin Vogt Add Fujitsu Siemens Computer (FSC) 0x1734 bcm 5751 0x105d support
13  */
14
15 #include "etherboot.h"
16 #include "nic.h"
17 #include <gpxe/pci.h>
18 #include <gpxe/ethernet.h>
19 #include "timer.h"
20 #include "string.h"
21 #include "tg3.h"
22
23 #define SUPPORT_COPPER_PHY  1
24 #define SUPPORT_FIBER_PHY   1
25 #define SUPPORT_LINK_REPORT 1
26 #define SUPPORT_PARTNO_STR  1
27 #define SUPPORT_PHY_STR     1
28
29 static struct tg3 tg3;
30
31 /* These numbers seem to be hard coded in the NIC firmware somehow.
32  * You can't change the ring sizes, but you can change where you place
33  * them in the NIC onboard memory.
34  */
35 #define TG3_RX_RING_SIZE                512
36 #define TG3_DEF_RX_RING_PENDING         20      /* RX_RING_PENDING seems to be o.k. at 20 and 200 */
37 #define TG3_RX_RCB_RING_SIZE    1024
38
39 /*      (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ? \
40          512 : 1024) */
41 #define TG3_TX_RING_SIZE                512
42 #define TG3_DEF_TX_RING_PENDING         (TG3_TX_RING_SIZE - 1)
43
44 #define TG3_RX_RING_BYTES       (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_RING_SIZE)
45 #define TG3_RX_RCB_RING_BYTES   (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_RCB_RING_SIZE)
46
47 #define TG3_TX_RING_BYTES       (sizeof(struct tg3_tx_buffer_desc) * TG3_TX_RING_SIZE)
48 #define NEXT_TX(N)              (((N) + 1) & (TG3_TX_RING_SIZE - 1))
49 #define PREV_TX(N)              (((N) - 1) & (TG3_TX_RING_SIZE - 1))
50
51 #define RX_PKT_BUF_SZ           (1536 + 2 + 64)
52
53 struct eth_frame {
54         uint8_t  dst_addr[ETH_ALEN];
55         uint8_t  src_addr[ETH_ALEN];
56         uint16_t type;
57         uint8_t  data [ETH_FRAME_LEN - ETH_HLEN];
58 };
59
60 struct bss {
61         struct tg3_rx_buffer_desc rx_std[TG3_RX_RING_SIZE];
62         struct tg3_rx_buffer_desc rx_rcb[TG3_RX_RCB_RING_SIZE];
63         struct tg3_tx_buffer_desc tx_ring[TG3_TX_RING_SIZE];
64         struct tg3_hw_status      hw_status;
65         struct tg3_hw_stats       hw_stats;
66         unsigned char             rx_bufs[TG3_DEF_RX_RING_PENDING][RX_PKT_BUF_SZ];
67         struct eth_frame          tx_frame[2];
68 } tg3_bss __shared;
69
70 /**
71  * pci_save_state - save the PCI configuration space of a device before suspending
72  * @dev: - PCI device that we're dealing with
73  * @buffer: - buffer to hold config space context
74  *
75  * @buffer must be large enough to hold the entire PCI 2.2 config space 
76  * (>= 64 bytes).
77  */
78 static int pci_save_state(struct pci_device *dev, uint32_t *buffer)
79 {
80         int i;
81         for (i = 0; i < 16; i++)
82                 pci_read_config_dword(dev, i * 4,&buffer[i]);
83         return 0;
84 }
85
86 /** 
87  * pci_restore_state - Restore the saved state of a PCI device
88  * @dev: - PCI device that we're dealing with
89  * @buffer: - saved PCI config space
90  *
91  */
92 static int pci_restore_state(struct pci_device *dev, uint32_t *buffer)
93 {
94         int i;
95
96         for (i = 0; i < 16; i++)
97                 pci_write_config_dword(dev,i * 4, buffer[i]);
98         return 0;
99 }
100
101 static void tg3_write_indirect_reg32(uint32_t off, uint32_t val)
102 {
103         pci_write_config_dword(tg3.pdev, TG3PCI_REG_BASE_ADDR, off);
104         pci_write_config_dword(tg3.pdev, TG3PCI_REG_DATA, val);
105 }
106
107 #define tw32(reg,val)           tg3_write_indirect_reg32((reg),(val))
108 #define tw32_mailbox(reg, val)  writel(((val) & 0xffffffff), tg3.regs + (reg))
109 #define tw16(reg,val)           writew(((val) & 0xffff), tg3.regs + (reg))
110 #define tw8(reg,val)            writeb(((val) & 0xff), tg3.regs + (reg))
111 #define tr32(reg)               readl(tg3.regs + (reg))
112 #define tr16(reg)               readw(tg3.regs + (reg))
113 #define tr8(reg)                readb(tg3.regs + (reg))
114
115 static void tw32_carefully(uint32_t reg, uint32_t val)
116 {
117         tw32(reg, val);
118         tr32(reg);
119         udelay(100);
120 }
121
122 static void tw32_mailbox2(uint32_t reg, uint32_t val)
123 {
124         tw32_mailbox(reg, val);
125         tr32(reg);
126 }
127
128 static void tg3_write_mem(uint32_t off, uint32_t val)
129 {
130         pci_write_config_dword(tg3.pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
131         pci_write_config_dword(tg3.pdev, TG3PCI_MEM_WIN_DATA, val);
132
133         /* Always leave this as zero. */
134         pci_write_config_dword(tg3.pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
135 }
136
137 static void tg3_read_mem(uint32_t off, uint32_t *val)
138 {
139         pci_write_config_dword(tg3.pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
140         pci_read_config_dword(tg3.pdev, TG3PCI_MEM_WIN_DATA, val);
141
142         /* Always leave this as zero. */
143         pci_write_config_dword(tg3.pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
144 }
145
146 static void tg3_disable_ints(struct tg3 *tp)
147 {
148         tw32(TG3PCI_MISC_HOST_CTRL,
149              (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
150         tw32_mailbox2(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
151 }
152
153 static void tg3_switch_clocks(struct tg3 *tp)
154 {
155         uint32_t orig_clock_ctrl, clock_ctrl;
156
157         clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
158
159         orig_clock_ctrl = clock_ctrl;
160         clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN | CLOCK_CTRL_CLKRUN_OENABLE | 0x1f);
161         tp->pci_clock_ctrl = clock_ctrl;
162         
163         if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) &&
164             (!((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
165                && (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) &&
166                 (orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE)!=0) {
167                 tw32_carefully(TG3PCI_CLOCK_CTRL, 
168                         clock_ctrl | (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK));
169                 tw32_carefully(TG3PCI_CLOCK_CTRL, 
170                         clock_ctrl | (CLOCK_CTRL_ALTCLK));
171         }
172         tw32_carefully(TG3PCI_CLOCK_CTRL, clock_ctrl);
173 }
174
175 #define PHY_BUSY_LOOPS  5000
176
177 static int tg3_readphy(struct tg3 *tp, int reg, uint32_t *val)
178 {
179         uint32_t frame_val;
180         int loops, ret;
181
182         tw32_carefully(MAC_MI_MODE, tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL);
183
184         *val = 0xffffffff;
185
186         frame_val  = ((PHY_ADDR << MI_COM_PHY_ADDR_SHIFT) &
187                       MI_COM_PHY_ADDR_MASK);
188         frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
189                       MI_COM_REG_ADDR_MASK);
190         frame_val |= (MI_COM_CMD_READ | MI_COM_START);
191         
192         tw32_carefully(MAC_MI_COM, frame_val);
193
194         loops = PHY_BUSY_LOOPS;
195         while (loops-- > 0) {
196                 udelay(10);
197                 frame_val = tr32(MAC_MI_COM);
198
199                 if ((frame_val & MI_COM_BUSY) == 0) {
200                         udelay(5);
201                         frame_val = tr32(MAC_MI_COM);
202                         break;
203                 }
204         }
205
206         ret = -EBUSY;
207         if (loops > 0) {
208                 *val = frame_val & MI_COM_DATA_MASK;
209                 ret = 0;
210         }
211
212         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
213
214         return ret;
215 }
216
217 static int tg3_writephy(struct tg3 *tp, int reg, uint32_t val)
218 {
219         uint32_t frame_val;
220         int loops, ret;
221
222         tw32_carefully(MAC_MI_MODE, tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL);
223
224         frame_val  = ((PHY_ADDR << MI_COM_PHY_ADDR_SHIFT) &
225                       MI_COM_PHY_ADDR_MASK);
226         frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
227                       MI_COM_REG_ADDR_MASK);
228         frame_val |= (val & MI_COM_DATA_MASK);
229         frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
230         
231         tw32_carefully(MAC_MI_COM, frame_val);
232
233         loops = PHY_BUSY_LOOPS;
234         while (loops-- > 0) {
235                 udelay(10);
236                 frame_val = tr32(MAC_MI_COM);
237                 if ((frame_val & MI_COM_BUSY) == 0) {
238                         udelay(5);
239                         frame_val = tr32(MAC_MI_COM);
240                         break;
241                 }
242         }
243
244         ret = -EBUSY;
245         if (loops > 0)
246                 ret = 0;
247
248         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
249
250         return ret;
251 }
252
253 static int tg3_writedsp(struct tg3 *tp, uint16_t addr, uint16_t val)
254 {
255         int err;
256         err  = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, addr);
257         err |= tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
258         return err;
259 }
260
261
262 static void tg3_phy_set_wirespeed(struct tg3 *tp)
263 {
264         uint32_t val;
265
266         if (tp->tg3_flags2 & TG3_FLG2_NO_ETH_WIRE_SPEED)
267                 return;
268
269         tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007);
270         tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
271         tg3_writephy(tp, MII_TG3_AUX_CTRL, (val | (1 << 15) | (1 << 4)));
272 }
273
274 static int tg3_bmcr_reset(struct tg3 *tp)
275 {
276         uint32_t phy_control;
277         int limit, err;
278
279         /* OK, reset it, and poll the BMCR_RESET bit until it
280          * clears or we time out.
281          */
282         phy_control = BMCR_RESET;
283         err = tg3_writephy(tp, MII_BMCR, phy_control);
284         if (err != 0)
285                 return -EBUSY;
286
287         limit = 5000;
288         while (limit--) {
289                 err = tg3_readphy(tp, MII_BMCR, &phy_control);
290                 if (err != 0)
291                         return -EBUSY;
292
293                 if ((phy_control & BMCR_RESET) == 0) {
294                         udelay(40);
295                         break;
296                 }
297                 udelay(10);
298         }
299         if (limit <= 0)
300                 return -EBUSY;
301
302         return 0;
303 }
304
305 static int tg3_wait_macro_done(struct tg3 *tp)
306 {
307         int limit = 100;
308
309         while (limit--) {
310                 uint32_t tmp32;
311
312                 tg3_readphy(tp, 0x16, &tmp32);
313                 if ((tmp32 & 0x1000) == 0)
314                         break;
315         }
316         if (limit <= 0)
317                 return -EBUSY;
318
319         return 0;
320 }
321
322 static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
323 {
324         static const uint32_t test_pat[4][6] = {
325         { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
326         { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
327         { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
328         { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
329         };
330         int chan;
331
332         for (chan = 0; chan < 4; chan++) {
333                 int i;
334
335                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
336                         (chan * 0x2000) | 0x0200);
337                 tg3_writephy(tp, 0x16, 0x0002);
338
339                 for (i = 0; i < 6; i++)
340                         tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
341                                 test_pat[chan][i]);
342
343                 tg3_writephy(tp, 0x16, 0x0202);
344                 if (tg3_wait_macro_done(tp)) {
345                         *resetp = 1;
346                         return -EBUSY;
347                 }
348
349                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
350                              (chan * 0x2000) | 0x0200);
351                 tg3_writephy(tp, 0x16, 0x0082);
352                 if (tg3_wait_macro_done(tp)) {
353                         *resetp = 1;
354                         return -EBUSY;
355                 }
356
357                 tg3_writephy(tp, 0x16, 0x0802);
358                 if (tg3_wait_macro_done(tp)) {
359                         *resetp = 1;
360                         return -EBUSY;
361                 }
362
363                 for (i = 0; i < 6; i += 2) {
364                         uint32_t low, high;
365
366                         tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low);
367                         tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high);
368                         if (tg3_wait_macro_done(tp)) {
369                                 *resetp = 1;
370                                 return -EBUSY;
371                         }
372                         low &= 0x7fff;
373                         high &= 0x000f;
374                         if (low != test_pat[chan][i] ||
375                             high != test_pat[chan][i+1]) {
376                                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
377                                 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
378                                 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
379
380                                 return -EBUSY;
381                         }
382                 }
383         }
384
385         return 0;
386 }
387
388 static int tg3_phy_reset_chanpat(struct tg3 *tp)
389 {
390         int chan;
391
392         for (chan = 0; chan < 4; chan++) {
393                 int i;
394
395                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
396                              (chan * 0x2000) | 0x0200);
397                 tg3_writephy(tp, 0x16, 0x0002);
398                 for (i = 0; i < 6; i++)
399                         tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
400                 tg3_writephy(tp, 0x16, 0x0202);
401                 if (tg3_wait_macro_done(tp))
402                         return -EBUSY;
403         }
404
405         return 0;
406 }
407
408 static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
409 {
410         uint32_t reg32, phy9_orig;
411         int retries, do_phy_reset, err;
412
413         retries = 10;
414         do_phy_reset = 1;
415         do {
416                 if (do_phy_reset) {
417                         err = tg3_bmcr_reset(tp);
418                         if (err)
419                                 return err;
420                         do_phy_reset = 0;
421                 }
422                 
423                 /* Disable transmitter and interrupt.  */
424                 tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
425                 reg32 |= 0x3000;
426                 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
427
428                 /* Set full-duplex, 1000 mbps.  */
429                 tg3_writephy(tp, MII_BMCR,
430                         BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
431
432                 /* Set to master mode.  */
433                 tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig);
434                 tg3_writephy(tp, MII_TG3_CTRL,
435                         (MII_TG3_CTRL_AS_MASTER |
436                                 MII_TG3_CTRL_ENABLE_AS_MASTER));
437
438                 /* Enable SM_DSP_CLOCK and 6dB.  */
439                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
440
441                 /* Block the PHY control access.  */
442                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
443                 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0800);
444
445                 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
446                 if (!err)
447                         break;
448         } while (--retries);
449
450         err = tg3_phy_reset_chanpat(tp);
451         if (err)
452                 return err;
453
454         tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8005);
455         tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x0000);
456
457         tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
458         tg3_writephy(tp, 0x16, 0x0000);
459
460         tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
461
462         tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
463
464         tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32);
465         reg32 &= ~0x3000;
466         tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
467
468         return err;
469 }
470
471 /* This will reset the tigon3 PHY if there is no valid
472  * link.
473  */
474 static int tg3_phy_reset(struct tg3 *tp)
475 {
476         uint32_t phy_status;
477         int err;
478
479         err  = tg3_readphy(tp, MII_BMSR, &phy_status);
480         err |= tg3_readphy(tp, MII_BMSR, &phy_status);
481         if (err != 0)
482                 return -EBUSY;
483
484         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
485                 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
486                 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) {
487                 err = tg3_phy_reset_5703_4_5(tp);
488                 if (err)
489                         return err;
490                 goto out;
491         }
492         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
493           // Taken from Broadcom's source code
494           tg3_writephy(tp, 0x18, 0x0c00);
495           tg3_writephy(tp, 0x17, 0x000a);
496           tg3_writephy(tp, 0x15, 0x310b);
497           tg3_writephy(tp, 0x17, 0x201f);
498           tg3_writephy(tp, 0x15, 0x9506);
499           tg3_writephy(tp, 0x17, 0x401f);
500           tg3_writephy(tp, 0x15, 0x14e2);
501           tg3_writephy(tp, 0x18, 0x0400);
502         }
503         err = tg3_bmcr_reset(tp);
504         if (err)
505                 return err;
506  out:
507         tg3_phy_set_wirespeed(tp);
508         return 0;
509 }
510
511 static void tg3_set_power_state_0(struct tg3 *tp)
512 {
513         uint16_t power_control;
514         int pm = tp->pm_cap;
515
516         /* Make sure register accesses (indirect or otherwise)
517          * will function correctly.
518          */
519         pci_write_config_dword(tp->pdev,  TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
520
521         pci_read_config_word(tp->pdev, pm + PCI_PM_CTRL, &power_control);
522
523         power_control |= PCI_PM_CTRL_PME_STATUS;
524         power_control &= ~(PCI_PM_CTRL_STATE_MASK);
525         power_control |= 0;
526         pci_write_config_word(tp->pdev, pm + PCI_PM_CTRL, power_control);
527
528         tw32_carefully(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
529
530         return;
531 }
532
533
534 #if SUPPORT_LINK_REPORT
535 static void tg3_link_report(struct tg3 *tp)
536 {
537         if (!tp->carrier_ok) {
538                 printf("Link is down.\n");
539         } else {
540                 printf("Link is up at %d Mbps, %s duplex. %s %s %s\n",
541                         (tp->link_config.active_speed == SPEED_1000 ?
542                                1000 :
543                         (tp->link_config.active_speed == SPEED_100 ?
544                                 100 : 10)),
545                         (tp->link_config.active_duplex == DUPLEX_FULL ?  
546                                 "full" : "half"),
547                         (tp->tg3_flags & TG3_FLAG_TX_PAUSE) ? "TX" : "",
548                         (tp->tg3_flags & TG3_FLAG_RX_PAUSE) ? "RX" : "",
549                         (tp->tg3_flags & (TG3_FLAG_TX_PAUSE |TG3_FLAG_RX_PAUSE)) ? "flow control" : "");
550         }
551 }
552 #else
553 #define tg3_link_report(tp)
554 #endif
555
556 static void tg3_setup_flow_control(struct tg3 *tp, uint32_t local_adv, uint32_t remote_adv)
557 {
558         uint32_t new_tg3_flags = 0;
559
560         if (local_adv & ADVERTISE_PAUSE_CAP) {
561                 if (local_adv & ADVERTISE_PAUSE_ASYM) {
562                         if (remote_adv & LPA_PAUSE_CAP)
563                                 new_tg3_flags |=
564                                         (TG3_FLAG_RX_PAUSE |
565                                          TG3_FLAG_TX_PAUSE);
566                         else if (remote_adv & LPA_PAUSE_ASYM)
567                                 new_tg3_flags |=
568                                         (TG3_FLAG_RX_PAUSE);
569                 } else {
570                         if (remote_adv & LPA_PAUSE_CAP)
571                                 new_tg3_flags |=
572                                         (TG3_FLAG_RX_PAUSE |
573                                          TG3_FLAG_TX_PAUSE);
574                 }
575         } else if (local_adv & ADVERTISE_PAUSE_ASYM) {
576                 if ((remote_adv & LPA_PAUSE_CAP) &&
577                     (remote_adv & LPA_PAUSE_ASYM))
578                         new_tg3_flags |= TG3_FLAG_TX_PAUSE;
579         }
580
581         tp->tg3_flags &= ~(TG3_FLAG_RX_PAUSE | TG3_FLAG_TX_PAUSE);
582         tp->tg3_flags |= new_tg3_flags;
583
584         if (new_tg3_flags & TG3_FLAG_RX_PAUSE)
585                 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
586         else
587                 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
588
589         if (new_tg3_flags & TG3_FLAG_TX_PAUSE)
590                 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
591         else
592                 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
593 }
594
595 #if SUPPORT_COPPER_PHY
596 static void tg3_aux_stat_to_speed_duplex(
597         struct tg3 *tp __unused, uint32_t val, uint8_t *speed, uint8_t *duplex)
598 {
599         static const uint8_t map[] = {
600                 [0] = (SPEED_INVALID << 2) | DUPLEX_INVALID,
601                 [MII_TG3_AUX_STAT_10HALF >> 8]   = (SPEED_10 << 2) | DUPLEX_HALF,
602                 [MII_TG3_AUX_STAT_10FULL >> 8]   = (SPEED_10 << 2) | DUPLEX_FULL,
603                 [MII_TG3_AUX_STAT_100HALF >> 8]  = (SPEED_100 << 2) | DUPLEX_HALF,
604                 [MII_TG3_AUX_STAT_100_4 >> 8] = (SPEED_INVALID << 2) | DUPLEX_INVALID,
605                 [MII_TG3_AUX_STAT_100FULL >> 8]  = (SPEED_100 << 2) | DUPLEX_FULL,
606                 [MII_TG3_AUX_STAT_1000HALF >> 8] = (SPEED_1000 << 2) | DUPLEX_HALF,
607                 [MII_TG3_AUX_STAT_1000FULL >> 8] = (SPEED_1000 << 2) | DUPLEX_FULL,
608         };
609         uint8_t result;
610         result = map[(val & MII_TG3_AUX_STAT_SPDMASK) >> 8];
611         *speed = result >> 2;
612         *duplex = result & 3;
613 }
614
615 static int tg3_phy_copper_begin(struct tg3 *tp)
616 {
617         uint32_t new_adv;
618
619         tp->link_config.advertising =
620                 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
621                         ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
622                         ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
623                         ADVERTISED_Autoneg | ADVERTISED_MII);
624         
625         if (tp->tg3_flags & TG3_FLAG_10_100_ONLY) {
626                 tp->link_config.advertising &=
627                         ~(ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
628         }
629         
630         new_adv = (ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
631         if (tp->link_config.advertising & ADVERTISED_10baseT_Half) {
632                 new_adv |= ADVERTISE_10HALF;
633         }
634         if (tp->link_config.advertising & ADVERTISED_10baseT_Full) {
635                 new_adv |= ADVERTISE_10FULL;
636         }
637         if (tp->link_config.advertising & ADVERTISED_100baseT_Half) {
638                 new_adv |= ADVERTISE_100HALF;
639         }
640         if (tp->link_config.advertising & ADVERTISED_100baseT_Full) {
641                 new_adv |= ADVERTISE_100FULL;
642         }
643         tg3_writephy(tp, MII_ADVERTISE, new_adv);
644         
645         if (tp->link_config.advertising &
646                 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
647                 new_adv = 0;
648                 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half) {
649                         new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
650                 }
651                 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full) {
652                         new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
653                 }
654                 if (!(tp->tg3_flags & TG3_FLAG_10_100_ONLY) &&
655                         (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
656                                 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)) {
657                         new_adv |= (MII_TG3_CTRL_AS_MASTER |
658                                 MII_TG3_CTRL_ENABLE_AS_MASTER);
659                 }
660                 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
661         } else {
662                 tg3_writephy(tp, MII_TG3_CTRL, 0);
663         }
664
665         tg3_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
666
667         return 0;
668 }
669
670 static int tg3_init_5401phy_dsp(struct tg3 *tp)
671 {
672         int err;
673
674         /* Turn off tap power management. */
675         err  = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c20);
676         
677         err |= tg3_writedsp(tp, 0x0012, 0x1804);
678         err |= tg3_writedsp(tp, 0x0013, 0x1204);
679         err |= tg3_writedsp(tp, 0x8006, 0x0132);
680         err |= tg3_writedsp(tp, 0x8006, 0x0232);
681         err |= tg3_writedsp(tp, 0x201f, 0x0a20);
682
683         udelay(40);
684
685         return err;
686 }
687
688 static int tg3_setup_copper_phy(struct tg3 *tp)
689 {
690         int current_link_up;
691         uint32_t bmsr, dummy;
692         int i, err;
693
694         tw32_carefully(MAC_STATUS,
695                 (MAC_STATUS_SYNC_CHANGED | MAC_STATUS_CFG_CHANGED
696                  | MAC_STATUS_LNKSTATE_CHANGED));
697
698         tp->mi_mode = MAC_MI_MODE_BASE;
699         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
700
701         tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
702
703         /* Some third-party PHYs need to be reset on link going
704          * down.
705          */
706         if (    (       (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
707                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
708                         (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)) &&
709                 (tp->carrier_ok)) {
710                 tg3_readphy(tp, MII_BMSR, &bmsr);
711                 tg3_readphy(tp, MII_BMSR, &bmsr);
712                 if (!(bmsr & BMSR_LSTATUS))
713                         tg3_phy_reset(tp);
714         }
715
716         if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401) {
717                 tg3_readphy(tp, MII_BMSR, &bmsr);
718                 tg3_readphy(tp, MII_BMSR, &bmsr);
719
720                 if (!(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
721                         bmsr = 0;
722
723                 if (!(bmsr & BMSR_LSTATUS)) {
724                         err = tg3_init_5401phy_dsp(tp);
725                         if (err)
726                                 return err;
727
728                         tg3_readphy(tp, MII_BMSR, &bmsr);
729                         for (i = 0; i < 1000; i++) {
730                                 udelay(10);
731                                 tg3_readphy(tp, MII_BMSR, &bmsr);
732                                 if (bmsr & BMSR_LSTATUS) {
733                                         udelay(40);
734                                         break;
735                                 }
736                         }
737
738                         if ((tp->phy_id & PHY_ID_REV_MASK) == PHY_REV_BCM5401_B0 &&
739                             !(bmsr & BMSR_LSTATUS) &&
740                             tp->link_config.active_speed == SPEED_1000) {
741                                 err = tg3_phy_reset(tp);
742                                 if (!err)
743                                         err = tg3_init_5401phy_dsp(tp);
744                                 if (err)
745                                         return err;
746                         }
747                 }
748         } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
749                    tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
750                 /* 5701 {A0,B0} CRC bug workaround */
751                 tg3_writephy(tp, 0x15, 0x0a75);
752                 tg3_writephy(tp, 0x1c, 0x8c68);
753                 tg3_writephy(tp, 0x1c, 0x8d68);
754                 tg3_writephy(tp, 0x1c, 0x8c68);
755         }
756
757         /* Clear pending interrupts... */
758         tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
759         tg3_readphy(tp, MII_TG3_ISTAT, &dummy);
760
761         tg3_writephy(tp, MII_TG3_IMASK, ~0);
762
763         if (tp->led_mode == led_mode_three_link)
764                 tg3_writephy(tp, MII_TG3_EXT_CTRL,
765                              MII_TG3_EXT_CTRL_LNK3_LED_MODE);
766         else
767                 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
768
769         current_link_up = 0;
770
771         tg3_readphy(tp, MII_BMSR, &bmsr);
772         tg3_readphy(tp, MII_BMSR, &bmsr);
773
774         if (bmsr & BMSR_LSTATUS) {
775                 uint32_t aux_stat, bmcr;
776
777                 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
778                 for (i = 0; i < 2000; i++) {
779                         udelay(10);
780                         tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
781                         if (aux_stat)
782                                 break;
783                 }
784
785                 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
786                         &tp->link_config.active_speed,
787                         &tp->link_config.active_duplex);
788                 tg3_readphy(tp, MII_BMCR, &bmcr);
789                 tg3_readphy(tp, MII_BMCR, &bmcr);
790                 if (bmcr & BMCR_ANENABLE) {
791                         uint32_t gig_ctrl;
792                         
793                         current_link_up = 1;
794                         
795                         /* Force autoneg restart if we are exiting
796                          * low power mode.
797                          */
798                         tg3_readphy(tp, MII_TG3_CTRL, &gig_ctrl);
799                         if (!(gig_ctrl & (MII_TG3_CTRL_ADV_1000_HALF |
800                                       MII_TG3_CTRL_ADV_1000_FULL))) {
801                                 current_link_up = 0;
802                         }
803                 } else {
804                         current_link_up = 0;
805                 }
806         }
807
808         if (current_link_up == 1 &&
809                 (tp->link_config.active_duplex == DUPLEX_FULL)) {
810                 uint32_t local_adv, remote_adv;
811
812                 tg3_readphy(tp, MII_ADVERTISE, &local_adv);
813                 local_adv &= (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
814
815                 tg3_readphy(tp, MII_LPA, &remote_adv);
816                 remote_adv &= (LPA_PAUSE_CAP | LPA_PAUSE_ASYM);
817
818                 /* If we are not advertising full pause capability,
819                  * something is wrong.  Bring the link down and reconfigure.
820                  */
821                 if (local_adv != ADVERTISE_PAUSE_CAP) {
822                         current_link_up = 0;
823                 } else {
824                         tg3_setup_flow_control(tp, local_adv, remote_adv);
825                 }
826         }
827
828         if (current_link_up == 0) {
829                 uint32_t tmp;
830
831                 tg3_phy_copper_begin(tp);
832
833                 tg3_readphy(tp, MII_BMSR, &tmp);
834                 tg3_readphy(tp, MII_BMSR, &tmp);
835                 if (tmp & BMSR_LSTATUS)
836                         current_link_up = 1;
837         }
838
839         tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
840         if (current_link_up == 1) {
841                 if (tp->link_config.active_speed == SPEED_100 ||
842                     tp->link_config.active_speed == SPEED_10)
843                         tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
844                 else
845                         tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
846         } else
847                 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
848
849         tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
850         if (tp->link_config.active_duplex == DUPLEX_HALF)
851                 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
852
853         tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
854         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
855                 if ((tp->led_mode == led_mode_link10) ||
856                     (current_link_up == 1 &&
857                      tp->link_config.active_speed == SPEED_10))
858                         tp->mac_mode |= MAC_MODE_LINK_POLARITY;
859         } else {
860                 if (current_link_up == 1)
861                         tp->mac_mode |= MAC_MODE_LINK_POLARITY;
862                 tw32(MAC_LED_CTRL, LED_CTRL_PHY_MODE_1);
863         }
864
865         /* ??? Without this setting Netgear GA302T PHY does not
866          * ??? send/receive packets...
867          * With this other PHYs cannot bring up the link
868          */
869         if ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5411 &&
870                 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
871                 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
872                 tw32_carefully(MAC_MI_MODE, tp->mi_mode);
873         }
874
875         tw32_carefully(MAC_MODE, tp->mac_mode);
876
877         /* Link change polled. */
878         tw32_carefully(MAC_EVENT, 0);
879
880         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
881             current_link_up == 1 &&
882             tp->link_config.active_speed == SPEED_1000 &&
883             ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
884              (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
885                 udelay(120);
886                 tw32_carefully(MAC_STATUS,
887                         (MAC_STATUS_SYNC_CHANGED | MAC_STATUS_CFG_CHANGED));
888                 tg3_write_mem(
889                               NIC_SRAM_FIRMWARE_MBOX,
890                               NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
891         }
892
893         if (current_link_up != tp->carrier_ok) {
894                 tp->carrier_ok = current_link_up;
895                 tg3_link_report(tp);
896         }
897
898         return 0;
899 }
900 #else
901 #define tg3_setup_copper_phy(TP) (-EINVAL)
902 #endif /* SUPPORT_COPPER_PHY */
903
904 #if SUPPORT_FIBER_PHY
905 struct tg3_fiber_aneginfo {
906         int state;
907 #define ANEG_STATE_UNKNOWN              0
908 #define ANEG_STATE_AN_ENABLE            1
909 #define ANEG_STATE_RESTART_INIT         2
910 #define ANEG_STATE_RESTART              3
911 #define ANEG_STATE_DISABLE_LINK_OK      4
912 #define ANEG_STATE_ABILITY_DETECT_INIT  5
913 #define ANEG_STATE_ABILITY_DETECT       6
914 #define ANEG_STATE_ACK_DETECT_INIT      7
915 #define ANEG_STATE_ACK_DETECT           8
916 #define ANEG_STATE_COMPLETE_ACK_INIT    9
917 #define ANEG_STATE_COMPLETE_ACK         10
918 #define ANEG_STATE_IDLE_DETECT_INIT     11
919 #define ANEG_STATE_IDLE_DETECT          12
920 #define ANEG_STATE_LINK_OK              13
921 #define ANEG_STATE_NEXT_PAGE_WAIT_INIT  14
922 #define ANEG_STATE_NEXT_PAGE_WAIT       15
923
924         uint32_t flags;
925 #define MR_AN_ENABLE            0x00000001
926 #define MR_RESTART_AN           0x00000002
927 #define MR_AN_COMPLETE          0x00000004
928 #define MR_PAGE_RX              0x00000008
929 #define MR_NP_LOADED            0x00000010
930 #define MR_TOGGLE_TX            0x00000020
931 #define MR_LP_ADV_FULL_DUPLEX   0x00000040
932 #define MR_LP_ADV_HALF_DUPLEX   0x00000080
933 #define MR_LP_ADV_SYM_PAUSE     0x00000100
934 #define MR_LP_ADV_ASYM_PAUSE    0x00000200
935 #define MR_LP_ADV_REMOTE_FAULT1 0x00000400
936 #define MR_LP_ADV_REMOTE_FAULT2 0x00000800
937 #define MR_LP_ADV_NEXT_PAGE     0x00001000
938 #define MR_TOGGLE_RX            0x00002000
939 #define MR_NP_RX                0x00004000
940
941 #define MR_LINK_OK              0x80000000
942
943         unsigned long link_time, cur_time;
944
945         uint32_t ability_match_cfg;
946         int ability_match_count;
947
948         char ability_match, idle_match, ack_match;
949
950         uint32_t txconfig, rxconfig;
951 #define ANEG_CFG_NP             0x00000080
952 #define ANEG_CFG_ACK            0x00000040
953 #define ANEG_CFG_RF2            0x00000020
954 #define ANEG_CFG_RF1            0x00000010
955 #define ANEG_CFG_PS2            0x00000001
956 #define ANEG_CFG_PS1            0x00008000
957 #define ANEG_CFG_HD             0x00004000
958 #define ANEG_CFG_FD             0x00002000
959 #define ANEG_CFG_INVAL          0x00001f06
960
961 };
962 #define ANEG_OK         0
963 #define ANEG_DONE       1
964 #define ANEG_TIMER_ENAB 2
965 #define ANEG_FAILED     -1
966
967 #define ANEG_STATE_SETTLE_TIME  10000
968
969 static int tg3_fiber_aneg_smachine(struct tg3 *tp,
970                                    struct tg3_fiber_aneginfo *ap)
971 {
972         unsigned long delta;
973         uint32_t rx_cfg_reg;
974         int ret;
975
976         if (ap->state == ANEG_STATE_UNKNOWN) {
977                 ap->rxconfig = 0;
978                 ap->link_time = 0;
979                 ap->cur_time = 0;
980                 ap->ability_match_cfg = 0;
981                 ap->ability_match_count = 0;
982                 ap->ability_match = 0;
983                 ap->idle_match = 0;
984                 ap->ack_match = 0;
985         }
986         ap->cur_time++;
987
988         if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
989                 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
990
991                 if (rx_cfg_reg != ap->ability_match_cfg) {
992                         ap->ability_match_cfg = rx_cfg_reg;
993                         ap->ability_match = 0;
994                         ap->ability_match_count = 0;
995                 } else {
996                         if (++ap->ability_match_count > 1) {
997                                 ap->ability_match = 1;
998                                 ap->ability_match_cfg = rx_cfg_reg;
999                         }
1000                 }
1001                 if (rx_cfg_reg & ANEG_CFG_ACK)
1002                         ap->ack_match = 1;
1003                 else
1004                         ap->ack_match = 0;
1005
1006                 ap->idle_match = 0;
1007         } else {
1008                 ap->idle_match = 1;
1009                 ap->ability_match_cfg = 0;
1010                 ap->ability_match_count = 0;
1011                 ap->ability_match = 0;
1012                 ap->ack_match = 0;
1013
1014                 rx_cfg_reg = 0;
1015         }
1016
1017         ap->rxconfig = rx_cfg_reg;
1018         ret = ANEG_OK;
1019
1020         switch(ap->state) {
1021         case ANEG_STATE_UNKNOWN:
1022                 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
1023                         ap->state = ANEG_STATE_AN_ENABLE;
1024
1025                 /* fallthru */
1026         case ANEG_STATE_AN_ENABLE:
1027                 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
1028                 if (ap->flags & MR_AN_ENABLE) {
1029                         ap->link_time = 0;
1030                         ap->cur_time = 0;
1031                         ap->ability_match_cfg = 0;
1032                         ap->ability_match_count = 0;
1033                         ap->ability_match = 0;
1034                         ap->idle_match = 0;
1035                         ap->ack_match = 0;
1036
1037                         ap->state = ANEG_STATE_RESTART_INIT;
1038                 } else {
1039                         ap->state = ANEG_STATE_DISABLE_LINK_OK;
1040                 }
1041                 break;
1042
1043         case ANEG_STATE_RESTART_INIT:
1044                 ap->link_time = ap->cur_time;
1045                 ap->flags &= ~(MR_NP_LOADED);
1046                 ap->txconfig = 0;
1047                 tw32(MAC_TX_AUTO_NEG, 0);
1048                 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
1049                 tw32_carefully(MAC_MODE, tp->mac_mode);
1050
1051                 ret = ANEG_TIMER_ENAB;
1052                 ap->state = ANEG_STATE_RESTART;
1053
1054                 /* fallthru */
1055         case ANEG_STATE_RESTART:
1056                 delta = ap->cur_time - ap->link_time;
1057                 if (delta > ANEG_STATE_SETTLE_TIME) {
1058                         ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
1059                 } else {
1060                         ret = ANEG_TIMER_ENAB;
1061                 }
1062                 break;
1063
1064         case ANEG_STATE_DISABLE_LINK_OK:
1065                 ret = ANEG_DONE;
1066                 break;
1067
1068         case ANEG_STATE_ABILITY_DETECT_INIT:
1069                 ap->flags &= ~(MR_TOGGLE_TX);
1070                 ap->txconfig = (ANEG_CFG_FD | ANEG_CFG_PS1);
1071                 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
1072                 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
1073                 tw32_carefully(MAC_MODE, tp->mac_mode);
1074
1075                 ap->state = ANEG_STATE_ABILITY_DETECT;
1076                 break;
1077
1078         case ANEG_STATE_ABILITY_DETECT:
1079                 if (ap->ability_match != 0 && ap->rxconfig != 0) {
1080                         ap->state = ANEG_STATE_ACK_DETECT_INIT;
1081                 }
1082                 break;
1083
1084         case ANEG_STATE_ACK_DETECT_INIT:
1085                 ap->txconfig |= ANEG_CFG_ACK;
1086                 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
1087                 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
1088                 tw32_carefully(MAC_MODE, tp->mac_mode);
1089
1090                 ap->state = ANEG_STATE_ACK_DETECT;
1091
1092                 /* fallthru */
1093         case ANEG_STATE_ACK_DETECT:
1094                 if (ap->ack_match != 0) {
1095                         if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
1096                             (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
1097                                 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
1098                         } else {
1099                                 ap->state = ANEG_STATE_AN_ENABLE;
1100                         }
1101                 } else if (ap->ability_match != 0 &&
1102                            ap->rxconfig == 0) {
1103                         ap->state = ANEG_STATE_AN_ENABLE;
1104                 }
1105                 break;
1106
1107         case ANEG_STATE_COMPLETE_ACK_INIT:
1108                 if (ap->rxconfig & ANEG_CFG_INVAL) {
1109                         ret = ANEG_FAILED;
1110                         break;
1111                 }
1112                 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
1113                                MR_LP_ADV_HALF_DUPLEX |
1114                                MR_LP_ADV_SYM_PAUSE |
1115                                MR_LP_ADV_ASYM_PAUSE |
1116                                MR_LP_ADV_REMOTE_FAULT1 |
1117                                MR_LP_ADV_REMOTE_FAULT2 |
1118                                MR_LP_ADV_NEXT_PAGE |
1119                                MR_TOGGLE_RX |
1120                                MR_NP_RX);
1121                 if (ap->rxconfig & ANEG_CFG_FD)
1122                         ap->flags |= MR_LP_ADV_FULL_DUPLEX;
1123                 if (ap->rxconfig & ANEG_CFG_HD)
1124                         ap->flags |= MR_LP_ADV_HALF_DUPLEX;
1125                 if (ap->rxconfig & ANEG_CFG_PS1)
1126                         ap->flags |= MR_LP_ADV_SYM_PAUSE;
1127                 if (ap->rxconfig & ANEG_CFG_PS2)
1128                         ap->flags |= MR_LP_ADV_ASYM_PAUSE;
1129                 if (ap->rxconfig & ANEG_CFG_RF1)
1130                         ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
1131                 if (ap->rxconfig & ANEG_CFG_RF2)
1132                         ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
1133                 if (ap->rxconfig & ANEG_CFG_NP)
1134                         ap->flags |= MR_LP_ADV_NEXT_PAGE;
1135
1136                 ap->link_time = ap->cur_time;
1137
1138                 ap->flags ^= (MR_TOGGLE_TX);
1139                 if (ap->rxconfig & 0x0008)
1140                         ap->flags |= MR_TOGGLE_RX;
1141                 if (ap->rxconfig & ANEG_CFG_NP)
1142                         ap->flags |= MR_NP_RX;
1143                 ap->flags |= MR_PAGE_RX;
1144
1145                 ap->state = ANEG_STATE_COMPLETE_ACK;
1146                 ret = ANEG_TIMER_ENAB;
1147                 break;
1148
1149         case ANEG_STATE_COMPLETE_ACK:
1150                 if (ap->ability_match != 0 &&
1151                     ap->rxconfig == 0) {
1152                         ap->state = ANEG_STATE_AN_ENABLE;
1153                         break;
1154                 }
1155                 delta = ap->cur_time - ap->link_time;
1156                 if (delta > ANEG_STATE_SETTLE_TIME) {
1157                         if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
1158                                 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
1159                         } else {
1160                                 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
1161                                     !(ap->flags & MR_NP_RX)) {
1162                                         ap->state = ANEG_STATE_IDLE_DETECT_INIT;
1163                                 } else {
1164                                         ret = ANEG_FAILED;
1165                                 }
1166                         }
1167                 }
1168                 break;
1169
1170         case ANEG_STATE_IDLE_DETECT_INIT:
1171                 ap->link_time = ap->cur_time;
1172                 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
1173                 tw32_carefully(MAC_MODE, tp->mac_mode);
1174
1175                 ap->state = ANEG_STATE_IDLE_DETECT;
1176                 ret = ANEG_TIMER_ENAB;
1177                 break;
1178
1179         case ANEG_STATE_IDLE_DETECT:
1180                 if (ap->ability_match != 0 &&
1181                     ap->rxconfig == 0) {
1182                         ap->state = ANEG_STATE_AN_ENABLE;
1183                         break;
1184                 }
1185                 delta = ap->cur_time - ap->link_time;
1186                 if (delta > ANEG_STATE_SETTLE_TIME) {
1187                         /* XXX another gem from the Broadcom driver :( */
1188                         ap->state = ANEG_STATE_LINK_OK;
1189                 }
1190                 break;
1191
1192         case ANEG_STATE_LINK_OK:
1193                 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
1194                 ret = ANEG_DONE;
1195                 break;
1196
1197         case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
1198                 /* ??? unimplemented */
1199                 break;
1200
1201         case ANEG_STATE_NEXT_PAGE_WAIT:
1202                 /* ??? unimplemented */
1203                 break;
1204
1205         default:
1206                 ret = ANEG_FAILED;
1207                 break;
1208         };
1209
1210         return ret;
1211 }
1212
1213 static int tg3_setup_fiber_phy(struct tg3 *tp)
1214 {
1215         uint32_t orig_pause_cfg;
1216         uint16_t orig_active_speed;
1217         uint8_t orig_active_duplex;
1218         int current_link_up;
1219         int i;
1220
1221         orig_pause_cfg =
1222                 (tp->tg3_flags & (TG3_FLAG_RX_PAUSE |
1223                                   TG3_FLAG_TX_PAUSE));
1224         orig_active_speed = tp->link_config.active_speed;
1225         orig_active_duplex = tp->link_config.active_duplex;
1226
1227         tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
1228         tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
1229         tw32_carefully(MAC_MODE, tp->mac_mode);
1230
1231         /* Reset when initting first time or we have a link. */
1232         if (!(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) ||
1233             (tr32(MAC_STATUS) & MAC_STATUS_PCS_SYNCED)) {
1234                 /* Set PLL lock range. */
1235                 tg3_writephy(tp, 0x16, 0x8007);
1236
1237                 /* SW reset */
1238                 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
1239
1240                 /* Wait for reset to complete. */
1241                 mdelay(5);
1242
1243                 /* Config mode; select PMA/Ch 1 regs. */
1244                 tg3_writephy(tp, 0x10, 0x8411);
1245
1246                 /* Enable auto-lock and comdet, select txclk for tx. */
1247                 tg3_writephy(tp, 0x11, 0x0a10);
1248
1249                 tg3_writephy(tp, 0x18, 0x00a0);
1250                 tg3_writephy(tp, 0x16, 0x41ff);
1251
1252                 /* Assert and deassert POR. */
1253                 tg3_writephy(tp, 0x13, 0x0400);
1254                 udelay(40);
1255                 tg3_writephy(tp, 0x13, 0x0000);
1256
1257                 tg3_writephy(tp, 0x11, 0x0a50);
1258                 udelay(40);
1259                 tg3_writephy(tp, 0x11, 0x0a10);
1260
1261                 /* Wait for signal to stabilize */
1262                 mdelay(150);
1263
1264                 /* Deselect the channel register so we can read the PHYID
1265                  * later.
1266                  */
1267                 tg3_writephy(tp, 0x10, 0x8011);
1268         }
1269
1270         /* Disable link change interrupt.  */
1271         tw32_carefully(MAC_EVENT, 0);
1272
1273         current_link_up = 0;
1274         if (tr32(MAC_STATUS) & MAC_STATUS_PCS_SYNCED) {
1275                 if (!(tp->tg3_flags & TG3_FLAG_GOT_SERDES_FLOWCTL)) {
1276                         struct tg3_fiber_aneginfo aninfo;
1277                         int status = ANEG_FAILED;
1278                         unsigned int tick;
1279                         uint32_t tmp;
1280
1281                         memset(&aninfo, 0, sizeof(aninfo));
1282                         aninfo.flags |= (MR_AN_ENABLE);
1283
1284                         tw32(MAC_TX_AUTO_NEG, 0);
1285
1286                         tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
1287                         tw32_carefully(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
1288
1289                         tw32_carefully(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
1290
1291                         aninfo.state = ANEG_STATE_UNKNOWN;
1292                         aninfo.cur_time = 0;
1293                         tick = 0;
1294                         while (++tick < 195000) {
1295                                 status = tg3_fiber_aneg_smachine(tp, &aninfo);
1296                                 if (status == ANEG_DONE ||
1297                                     status == ANEG_FAILED)
1298                                         break;
1299
1300                                 udelay(1);
1301                         }
1302
1303                         tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
1304                         tw32_carefully(MAC_MODE, tp->mac_mode);
1305
1306                         if (status == ANEG_DONE &&
1307                             (aninfo.flags &
1308                              (MR_AN_COMPLETE | MR_LINK_OK |
1309                               MR_LP_ADV_FULL_DUPLEX))) {
1310                                 uint32_t local_adv, remote_adv;
1311
1312                                 local_adv = ADVERTISE_PAUSE_CAP;
1313                                 remote_adv = 0;
1314                                 if (aninfo.flags & MR_LP_ADV_SYM_PAUSE)
1315                                         remote_adv |= LPA_PAUSE_CAP;
1316                                 if (aninfo.flags & MR_LP_ADV_ASYM_PAUSE)
1317                                         remote_adv |= LPA_PAUSE_ASYM;
1318
1319                                 tg3_setup_flow_control(tp, local_adv, remote_adv);
1320
1321                                 tp->tg3_flags |=
1322                                         TG3_FLAG_GOT_SERDES_FLOWCTL;
1323                                 current_link_up = 1;
1324                         }
1325                         for (i = 0; i < 60; i++) {
1326                                 udelay(20);
1327                                 tw32_carefully(MAC_STATUS,
1328                                         (MAC_STATUS_SYNC_CHANGED | MAC_STATUS_CFG_CHANGED));
1329                                 if ((tr32(MAC_STATUS) &
1330                                      (MAC_STATUS_SYNC_CHANGED |
1331                                       MAC_STATUS_CFG_CHANGED)) == 0)
1332                                         break;
1333                         }
1334                         if (current_link_up == 0 &&
1335                             (tr32(MAC_STATUS) & MAC_STATUS_PCS_SYNCED)) {
1336                                 current_link_up = 1;
1337                         }
1338                 } else {
1339                         /* Forcing 1000FD link up. */
1340                         current_link_up = 1;
1341                 }
1342         }
1343
1344         tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
1345         tw32_carefully(MAC_MODE, tp->mac_mode);
1346
1347         tp->hw_status->status =
1348                 (SD_STATUS_UPDATED |
1349                  (tp->hw_status->status & ~SD_STATUS_LINK_CHG));
1350
1351         for (i = 0; i < 100; i++) {
1352                 udelay(20);
1353                 tw32_carefully(MAC_STATUS,
1354                         (MAC_STATUS_SYNC_CHANGED | MAC_STATUS_CFG_CHANGED));
1355                 if ((tr32(MAC_STATUS) &
1356                      (MAC_STATUS_SYNC_CHANGED |
1357                       MAC_STATUS_CFG_CHANGED)) == 0)
1358                         break;
1359         }
1360
1361         if ((tr32(MAC_STATUS) & MAC_STATUS_PCS_SYNCED) == 0)
1362                 current_link_up = 0;
1363
1364         if (current_link_up == 1) {
1365                 tp->link_config.active_speed = SPEED_1000;
1366                 tp->link_config.active_duplex = DUPLEX_FULL;
1367         } else {
1368                 tp->link_config.active_speed = SPEED_INVALID;
1369                 tp->link_config.active_duplex = DUPLEX_INVALID;
1370         }
1371
1372         if (current_link_up != tp->carrier_ok) {
1373                 tp->carrier_ok = current_link_up;
1374                 tg3_link_report(tp);
1375         } else {
1376                 uint32_t now_pause_cfg =
1377                         tp->tg3_flags & (TG3_FLAG_RX_PAUSE |
1378                                          TG3_FLAG_TX_PAUSE);
1379                 if (orig_pause_cfg != now_pause_cfg ||
1380                     orig_active_speed != tp->link_config.active_speed ||
1381                     orig_active_duplex != tp->link_config.active_duplex)
1382                         tg3_link_report(tp);
1383         }
1384
1385         if ((tr32(MAC_STATUS) & MAC_STATUS_PCS_SYNCED) == 0) {
1386                 tw32_carefully(MAC_MODE, tp->mac_mode | MAC_MODE_LINK_POLARITY);
1387                 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) {
1388                         tw32_carefully(MAC_MODE, tp->mac_mode);
1389                 }
1390         }
1391
1392         return 0;
1393 }
1394 #else
1395 #define tg3_setup_fiber_phy(TP) (-EINVAL)
1396 #endif /* SUPPORT_FIBER_PHY */
1397
1398 static int tg3_setup_phy(struct tg3 *tp)
1399 {
1400         int err;
1401
1402         if (tp->phy_id == PHY_ID_SERDES) {
1403                 err = tg3_setup_fiber_phy(tp);
1404         } else {
1405                 err = tg3_setup_copper_phy(tp);
1406         }
1407
1408         if (tp->link_config.active_speed == SPEED_1000 &&
1409             tp->link_config.active_duplex == DUPLEX_HALF)
1410                 tw32(MAC_TX_LENGTHS,
1411                      ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1412                       (6 << TX_LENGTHS_IPG_SHIFT) |
1413                       (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1414         else
1415                 tw32(MAC_TX_LENGTHS,
1416                      ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1417                       (6 << TX_LENGTHS_IPG_SHIFT) |
1418                       (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1419
1420         return err;
1421 }
1422
1423
1424 #define MAX_WAIT_CNT 1000
1425
1426 /* To stop a block, clear the enable bit and poll till it
1427  * clears.  
1428  */
1429 static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, uint32_t enable_bit)
1430 {
1431         unsigned int i;
1432         uint32_t val;
1433
1434         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1435                 switch(ofs) {
1436                 case RCVLSC_MODE:
1437                 case DMAC_MODE:
1438                 case MBFREE_MODE:
1439                 case BUFMGR_MODE:
1440                 case MEMARB_MODE:
1441                         /* We can't enable/disable these bits of the
1442                          * 5705, just say success.
1443                          */
1444                         return 0;
1445                 default:
1446                         break;
1447                 }
1448         }
1449         val = tr32(ofs);
1450         val &= ~enable_bit;
1451         tw32(ofs, val);
1452         tr32(ofs);
1453
1454         for (i = 0; i < MAX_WAIT_CNT; i++) {
1455                 udelay(100);
1456                 val = tr32(ofs);
1457                 if ((val & enable_bit) == 0)
1458                         break;
1459         }
1460
1461         if (i == MAX_WAIT_CNT) {
1462                 printf( "tg3_stop_block timed out, ofs=%#lx enable_bit=%3lx\n",
1463                        ofs, enable_bit );
1464                 return -ENODEV;
1465         }
1466
1467         return 0;
1468 }
1469
1470 static int tg3_abort_hw(struct tg3 *tp)
1471 {
1472         int i, err;
1473
1474         tg3_disable_ints(tp);
1475
1476         tp->rx_mode &= ~RX_MODE_ENABLE;
1477         tw32_carefully(MAC_RX_MODE, tp->rx_mode);
1478
1479         err  = tg3_stop_block(tp, RCVBDI_MODE,   RCVBDI_MODE_ENABLE);
1480         err |= tg3_stop_block(tp, RCVLPC_MODE,   RCVLPC_MODE_ENABLE);
1481         err |= tg3_stop_block(tp, RCVLSC_MODE,   RCVLSC_MODE_ENABLE);
1482         err |= tg3_stop_block(tp, RCVDBDI_MODE,  RCVDBDI_MODE_ENABLE);
1483         err |= tg3_stop_block(tp, RCVDCC_MODE,   RCVDCC_MODE_ENABLE);
1484         err |= tg3_stop_block(tp, RCVCC_MODE,    RCVCC_MODE_ENABLE);
1485
1486         err |= tg3_stop_block(tp, SNDBDS_MODE,   SNDBDS_MODE_ENABLE);
1487         err |= tg3_stop_block(tp, SNDBDI_MODE,   SNDBDI_MODE_ENABLE);
1488         err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
1489         err |= tg3_stop_block(tp, RDMAC_MODE,    RDMAC_MODE_ENABLE);
1490         err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
1491         err |= tg3_stop_block(tp, SNDBDC_MODE,   SNDBDC_MODE_ENABLE);
1492         if (err)
1493                 goto out;
1494
1495         tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
1496         tw32_carefully(MAC_MODE, tp->mac_mode);
1497
1498         tp->tx_mode &= ~TX_MODE_ENABLE;
1499         tw32_carefully(MAC_TX_MODE, tp->tx_mode);
1500
1501         for (i = 0; i < MAX_WAIT_CNT; i++) {
1502                 udelay(100);
1503                 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
1504                         break;
1505         }
1506         if (i >= MAX_WAIT_CNT) {
1507                 printf("tg3_abort_hw timed out TX_MODE_ENABLE will not clear MAC_TX_MODE=%x\n",
1508                        (unsigned int) tr32(MAC_TX_MODE));
1509                 return -ENODEV;
1510         }
1511
1512         err  = tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE);
1513         err |= tg3_stop_block(tp, WDMAC_MODE,  WDMAC_MODE_ENABLE);
1514         err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE);
1515
1516         tw32(FTQ_RESET, 0xffffffff);
1517         tw32(FTQ_RESET, 0x00000000);
1518
1519         err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE);
1520         err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE);
1521         if (err)
1522                 goto out;
1523
1524         memset(tp->hw_status, 0, TG3_HW_STATUS_SIZE);
1525
1526 out:
1527         return err;
1528 }
1529
1530 static void tg3_chip_reset(struct tg3 *tp)
1531 {
1532         uint32_t val;
1533
1534         if (!(tp->tg3_flags2 & TG3_FLG2_SUN_5704)) {
1535                 /* Force NVRAM to settle.
1536                  * This deals with a chip bug which can result in EEPROM
1537                  * corruption.
1538                  */
1539                 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
1540                         int i;
1541         
1542                         tw32(NVRAM_SWARB, SWARB_REQ_SET1);
1543                         for (i = 0; i < 100000; i++) {
1544                                 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
1545                                         break;
1546                                 udelay(10);
1547                         }
1548                 }
1549         }
1550         /* In Etherboot we don't need to worry about the 5701
1551          * REG_WRITE_BUG because we do all register writes indirectly.
1552          */
1553
1554         // Alf: here patched
1555         /* do the reset */
1556         val = GRC_MISC_CFG_CORECLK_RESET;
1557         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
1558             || (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)) {
1559                 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
1560         }
1561
1562         // Alf : Please VALIDATE THIS.
1563         // It is necessary in my case (5751) to prevent a reboot, but
1564         // I have no idea about a side effect on any other version.
1565         // It appears to be what's done in tigon3.c from Broadcom
1566         if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
1567           tw32(GRC_MISC_CFG, 0x20000000) ;
1568           val |= 0x20000000 ;
1569         }
1570
1571         tw32(GRC_MISC_CFG, val);
1572
1573         /* Flush PCI posted writes.  The normal MMIO registers
1574          * are inaccessible at this time so this is the only
1575          * way to make this reliably.  I tried to use indirect
1576          * register read/write but this upset some 5701 variants.
1577          */
1578         pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
1579
1580         udelay(120);
1581
1582         /* Re-enable indirect register accesses. */
1583         pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
1584                                tp->misc_host_ctrl);
1585
1586         /* Set MAX PCI retry to zero. */
1587         val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
1588         if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
1589             (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
1590                 val |= PCISTATE_RETRY_SAME_DMA;
1591         pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
1592
1593         pci_restore_state(tp->pdev, tp->pci_cfg_state);
1594
1595         /* Make sure PCI-X relaxed ordering bit is clear. */
1596         pci_read_config_dword(tp->pdev, TG3PCI_X_CAPS, &val);
1597         val &= ~PCIX_CAPS_RELAXED_ORDERING;
1598         pci_write_config_dword(tp->pdev, TG3PCI_X_CAPS, val);
1599
1600         tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
1601
1602         if (((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0) &&
1603                 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) {
1604                 tp->pci_clock_ctrl |=
1605                         (CLOCK_CTRL_FORCE_CLKRUN | CLOCK_CTRL_CLKRUN_OENABLE);
1606                 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
1607         }
1608
1609         tw32(TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
1610 }
1611
1612 static void tg3_stop_fw(struct tg3 *tp)
1613 {
1614         if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
1615                 uint32_t val;
1616                 int i;
1617
1618                 tg3_write_mem(NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1619                 val = tr32(GRC_RX_CPU_EVENT);
1620                 val |= (1 << 14);
1621                 tw32(GRC_RX_CPU_EVENT, val);
1622
1623                 /* Wait for RX cpu to ACK the event.  */
1624                 for (i = 0; i < 100; i++) {
1625                         if (!(tr32(GRC_RX_CPU_EVENT) & (1 << 14)))
1626                                 break;
1627                         udelay(1);
1628                 }
1629         }
1630 }
1631
1632 static int tg3_restart_fw(struct tg3 *tp, uint32_t state)
1633 {
1634         uint32_t val;
1635         int i;
1636         
1637         tg3_write_mem(NIC_SRAM_FIRMWARE_MBOX, 
1638                 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
1639         /* Wait for firmware initialization to complete. */
1640         for (i = 0; i < 100000; i++) {
1641                 tg3_read_mem(NIC_SRAM_FIRMWARE_MBOX, &val);
1642                 if (val == (uint32_t) ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
1643                         break;
1644                 udelay(10);
1645         }
1646         if (i >= 100000 &&
1647                     !(tp->tg3_flags2 & TG3_FLG2_SUN_5704)) {
1648                 printf ( "Firmware will not restart magic=%#lx\n",
1649                         val );
1650                 return -ENODEV;
1651         }
1652         if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
1653           state = DRV_STATE_SUSPEND;
1654         }
1655
1656         if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
1657             (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)) {
1658           // Enable PCIE bug fix
1659           tg3_read_mem(0x7c00, &val);
1660           tg3_write_mem(0x7c00, val | 0x02000000);
1661         }
1662         tg3_write_mem(NIC_SRAM_FW_DRV_STATE_MBOX, state);
1663         return 0;
1664 }
1665
1666 static int tg3_halt(struct tg3 *tp)
1667 {
1668         tg3_stop_fw(tp);
1669         tg3_abort_hw(tp);
1670         tg3_chip_reset(tp);
1671         return tg3_restart_fw(tp, DRV_STATE_UNLOAD);
1672 }
1673
1674 static void __tg3_set_mac_addr(struct tg3 *tp)
1675 {
1676         uint32_t addr_high, addr_low;
1677         int i;
1678
1679         addr_high = ((tp->nic->node_addr[0] << 8) |
1680                      tp->nic->node_addr[1]);
1681         addr_low = ((tp->nic->node_addr[2] << 24) |
1682                     (tp->nic->node_addr[3] << 16) |
1683                     (tp->nic->node_addr[4] <<  8) |
1684                     (tp->nic->node_addr[5] <<  0));
1685         for (i = 0; i < 4; i++) {
1686                 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
1687                 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
1688         }
1689
1690         if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
1691                 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
1692                 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705)) {
1693                 for(i = 0; i < 12; i++) {
1694                         tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
1695                         tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
1696                 }
1697         }
1698         addr_high = (tp->nic->node_addr[0] +
1699                      tp->nic->node_addr[1] +
1700                      tp->nic->node_addr[2] +
1701                      tp->nic->node_addr[3] +
1702                      tp->nic->node_addr[4] +
1703                      tp->nic->node_addr[5]) &
1704                 TX_BACKOFF_SEED_MASK;
1705         tw32(MAC_TX_BACKOFF_SEED, addr_high);
1706 }
1707
1708 static void tg3_set_bdinfo(struct tg3 *tp, uint32_t bdinfo_addr,
1709                            dma_addr_t mapping, uint32_t maxlen_flags,
1710                            uint32_t nic_addr)
1711 {
1712         tg3_write_mem((bdinfo_addr +
1713                        TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
1714                       ((uint64_t) mapping >> 32));
1715         tg3_write_mem((bdinfo_addr +
1716                        TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
1717                       ((uint64_t) mapping & 0xffffffff));
1718         tg3_write_mem((bdinfo_addr +
1719                        TG3_BDINFO_MAXLEN_FLAGS),
1720                        maxlen_flags);
1721         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
1722                 tg3_write_mem((bdinfo_addr + TG3_BDINFO_NIC_ADDR), nic_addr);
1723         }
1724 }
1725
1726
1727 static void tg3_init_rings(struct tg3 *tp)
1728 {
1729         unsigned i;
1730
1731         /* Zero out the tg3 variables */
1732         memset(&tg3_bss, 0, sizeof(tg3_bss));
1733         tp->rx_std    = &tg3_bss.rx_std[0];
1734         tp->rx_rcb    = &tg3_bss.rx_rcb[0];
1735         tp->tx_ring   = &tg3_bss.tx_ring[0];
1736         tp->hw_status = &tg3_bss.hw_status;
1737         tp->hw_stats  = &tg3_bss.hw_stats;
1738         tp->mac_mode  = 0;
1739
1740
1741         /* Initialize tx/rx rings for packet processing.
1742          *
1743          * The chip has been shut down and the driver detached from
1744          * the networking, so no interrupts or new tx packets will
1745          * end up in the driver.
1746          */
1747
1748         /* Initialize invariants of the rings, we only set this
1749          * stuff once.  This works because the card does not
1750          * write into the rx buffer posting rings.
1751          */
1752         for (i = 0; i < TG3_RX_RING_SIZE; i++) {
1753                 struct tg3_rx_buffer_desc *rxd;
1754
1755                 rxd = &tp->rx_std[i];
1756                 rxd->idx_len = (RX_PKT_BUF_SZ - 2 - 64) << RXD_LEN_SHIFT;
1757                 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
1758                 rxd->opaque = (RXD_OPAQUE_RING_STD | (i << RXD_OPAQUE_INDEX_SHIFT));
1759
1760                 /* Note where the receive buffer for the ring is placed */
1761                 rxd->addr_hi = 0;
1762                 rxd->addr_lo = virt_to_bus(
1763                         &tg3_bss.rx_bufs[i%TG3_DEF_RX_RING_PENDING][2]);
1764         }
1765 }
1766
1767 #define TG3_WRITE_SETTINGS(TABLE) \
1768 do { \
1769         const uint32_t *_table, *_end; \
1770         _table = TABLE; \
1771         _end = _table + sizeof(TABLE)/sizeof(TABLE[0]);  \
1772         for(; _table < _end; _table += 2) { \
1773                 tw32(_table[0], _table[1]); \
1774         } \
1775 } while(0)
1776
1777
1778 /* initialize/reset the tg3 */
1779 static int tg3_setup_hw(struct tg3 *tp)
1780 {
1781         uint32_t val, rdmac_mode;
1782         int i, err, limit;
1783
1784         /* Simply don't support setups with extremly buggy firmware in etherboot */
1785         if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
1786                 printf("Error 5701_A0 firmware bug detected\n");
1787                 return -EINVAL;
1788         }
1789
1790         tg3_disable_ints(tp);
1791
1792         /* Originally this was all in tg3_init_hw */
1793
1794         /* Force the chip into D0. */
1795         tg3_set_power_state_0(tp);
1796
1797         tg3_switch_clocks(tp);
1798
1799         tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
1800
1801         // This should go somewhere else
1802 #define T3_PCIE_CAPABILITY_ID_REG           0xD0
1803 #define T3_PCIE_CAPABILITY_ID               0x10
1804 #define T3_PCIE_CAPABILITY_REG              0xD2
1805
1806         /* Originally this was all in tg3_reset_hw */
1807
1808         tg3_stop_fw(tp);
1809
1810         /* No need to call tg3_abort_hw here, it is called before tg3_setup_hw. */
1811
1812         tg3_chip_reset(tp);
1813
1814         tw32(GRC_MODE, tp->grc_mode);  /* Redundant? */
1815
1816         err = tg3_restart_fw(tp, DRV_STATE_START);
1817         if (err)
1818                 return err;
1819
1820         if (tp->phy_id == PHY_ID_SERDES) {
1821                 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
1822         }
1823         tw32_carefully(MAC_MODE, tp->mac_mode);
1824
1825
1826         /* This works around an issue with Athlon chipsets on
1827          * B3 tigon3 silicon.  This bit has no effect on any
1828          * other revision.
1829          * Alf: Except 5750 ! (which reboots)
1830          */
1831
1832         if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
1833           tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
1834         tw32_carefully(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
1835
1836         if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
1837             (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
1838                 val = tr32(TG3PCI_PCISTATE);
1839                 val |= PCISTATE_RETRY_SAME_DMA;
1840                 tw32(TG3PCI_PCISTATE, val);
1841         }
1842
1843         /* Descriptor ring init may make accesses to the
1844          * NIC SRAM area to setup the TX descriptors, so we
1845          * can only do this after the hardware has been
1846          * successfully reset.
1847          */
1848         tg3_init_rings(tp);
1849
1850         /* Clear statistics/status block in chip */
1851         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
1852                 for (i = NIC_SRAM_STATS_BLK;
1853                      i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
1854                      i += sizeof(uint32_t)) {
1855                         tg3_write_mem(i, 0);
1856                         udelay(40);
1857                 }
1858         }
1859
1860         /* This value is determined during the probe time DMA
1861          * engine test, tg3_setup_dma.
1862          */
1863         tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
1864
1865         tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
1866                           GRC_MODE_4X_NIC_SEND_RINGS |
1867                           GRC_MODE_NO_TX_PHDR_CSUM |
1868                           GRC_MODE_NO_RX_PHDR_CSUM);
1869         tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
1870         tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
1871         tp->grc_mode |= GRC_MODE_NO_RX_PHDR_CSUM;
1872
1873         tw32(GRC_MODE,
1874                 tp->grc_mode | 
1875                 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
1876
1877         /* Setup the timer prescalar register.  Clock is always 66Mhz. */
1878         tw32(GRC_MISC_CFG,
1879              (65 << GRC_MISC_CFG_PRESCALAR_SHIFT));
1880
1881         /* Initialize MBUF/DESC pool. */
1882         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
1883                 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
1884                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
1885                         tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
1886                 else
1887                         tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
1888                 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
1889                 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
1890         }
1891         if (!(tp->tg3_flags & TG3_FLAG_JUMBO_ENABLE)) {
1892                 tw32(BUFMGR_MB_RDMA_LOW_WATER,
1893                      tp->bufmgr_config.mbuf_read_dma_low_water);
1894                 tw32(BUFMGR_MB_MACRX_LOW_WATER,
1895                      tp->bufmgr_config.mbuf_mac_rx_low_water);
1896                 tw32(BUFMGR_MB_HIGH_WATER,
1897                      tp->bufmgr_config.mbuf_high_water);
1898         } else {
1899                 tw32(BUFMGR_MB_RDMA_LOW_WATER,
1900                      tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
1901                 tw32(BUFMGR_MB_MACRX_LOW_WATER,
1902                      tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
1903                 tw32(BUFMGR_MB_HIGH_WATER,
1904                      tp->bufmgr_config.mbuf_high_water_jumbo);
1905         }
1906         tw32(BUFMGR_DMA_LOW_WATER,
1907              tp->bufmgr_config.dma_low_water);
1908         tw32(BUFMGR_DMA_HIGH_WATER,
1909              tp->bufmgr_config.dma_high_water);
1910
1911         tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
1912         for (i = 0; i < 2000; i++) {
1913                 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
1914                         break;
1915                 udelay(10);
1916         }
1917         if (i >= 2000) {
1918                 printf("tg3_setup_hw cannot enable BUFMGR\n");
1919                 return -ENODEV;
1920         }
1921
1922         tw32(FTQ_RESET, 0xffffffff);
1923         tw32(FTQ_RESET, 0x00000000);
1924         for (i = 0; i < 2000; i++) {
1925                 if (tr32(FTQ_RESET) == 0x00000000)
1926                         break;
1927                 udelay(10);
1928         }
1929         if (i >= 2000) {
1930                 printf("tg3_setup_hw cannot reset FTQ\n");
1931                 return -ENODEV;
1932         }
1933
1934         /* Initialize TG3_BDINFO's at:
1935          *  RCVDBDI_STD_BD:     standard eth size rx ring
1936          *  RCVDBDI_JUMBO_BD:   jumbo frame rx ring
1937          *  RCVDBDI_MINI_BD:    small frame rx ring (??? does not work)
1938          *
1939          * like so:
1940          *  TG3_BDINFO_HOST_ADDR:       high/low parts of DMA address of ring
1941          *  TG3_BDINFO_MAXLEN_FLAGS:    (rx max buffer size << 16) |
1942          *                              ring attribute flags
1943          *  TG3_BDINFO_NIC_ADDR:        location of descriptors in nic SRAM
1944          *
1945          * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
1946          * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
1947          *
1948          * ??? No space allocated for mini receive ring? :(
1949          *
1950          * The size of each ring is fixed in the firmware, but the location is
1951          * configurable.
1952          */
1953         {
1954                 static const uint32_t table_all[] = {
1955                         /* Setup replenish thresholds. */
1956                         RCVBDI_STD_THRESH, TG3_DEF_RX_RING_PENDING / 8,
1957
1958                         /* Etherboot lives below 4GB */
1959                         RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH, 0,
1960                         RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR, NIC_SRAM_RX_BUFFER_DESC,
1961                 };
1962                 static const uint32_t table_not_5705[] = {
1963                         /* Buffer maximum length */
1964                         RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT,
1965                         
1966                         /* Disable the mini frame rx ring */
1967                         RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,      BDINFO_FLAGS_DISABLED,
1968                         
1969                         /* Disable the jumbo frame rx ring */
1970                         RCVBDI_JUMBO_THRESH, 0,
1971                         RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS, BDINFO_FLAGS_DISABLED,
1972                         
1973                         
1974                 };
1975                 TG3_WRITE_SETTINGS(table_all);
1976                 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW, 
1977                         virt_to_bus(tp->rx_std));
1978                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1979                         tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS,
1980                                 RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT);
1981                 } else {
1982                         TG3_WRITE_SETTINGS(table_not_5705);
1983                 }
1984         }
1985
1986         
1987         /* There is only one send ring on 5705, no need to explicitly
1988          * disable the others.
1989          */
1990         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
1991                 /* Clear out send RCB ring in SRAM. */
1992                 for (i = NIC_SRAM_SEND_RCB; i < NIC_SRAM_RCV_RET_RCB; i += TG3_BDINFO_SIZE)
1993                         tg3_write_mem(i + TG3_BDINFO_MAXLEN_FLAGS, BDINFO_FLAGS_DISABLED);
1994         }
1995
1996         tp->tx_prod = 0;
1997         tw32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
1998         tw32_mailbox2(MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
1999
2000         tg3_set_bdinfo(tp,
2001                 NIC_SRAM_SEND_RCB,
2002                 virt_to_bus(tp->tx_ring),
2003                 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
2004                 NIC_SRAM_TX_BUFFER_DESC);
2005
2006         /* There is only one receive return ring on 5705, no need to explicitly
2007          * disable the others.
2008          */
2009         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
2010                 for (i = NIC_SRAM_RCV_RET_RCB; i < NIC_SRAM_STATS_BLK; i += TG3_BDINFO_SIZE) {
2011                         tg3_write_mem(i + TG3_BDINFO_MAXLEN_FLAGS,
2012                                 BDINFO_FLAGS_DISABLED);
2013                 }
2014         }
2015
2016         tp->rx_rcb_ptr = 0;
2017         tw32_mailbox2(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, 0);
2018
2019         tg3_set_bdinfo(tp,
2020                 NIC_SRAM_RCV_RET_RCB,
2021                 virt_to_bus(tp->rx_rcb),
2022                 (TG3_RX_RCB_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
2023                 0);
2024
2025         tp->rx_std_ptr = TG3_DEF_RX_RING_PENDING;
2026         tw32_mailbox2(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
2027                      tp->rx_std_ptr);
2028
2029         tw32_mailbox2(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW, 0);
2030
2031         /* Initialize MAC address and backoff seed. */
2032         __tg3_set_mac_addr(tp);
2033
2034         /* Calculate RDMAC_MODE setting early, we need it to determine
2035          * the RCVLPC_STATE_ENABLE mask.
2036          */
2037         rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
2038                 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
2039                 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
2040                 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
2041                 RDMAC_MODE_LNGREAD_ENAB);
2042         if (tp->tg3_flags & TG3_FLAG_SPLIT_MODE)
2043                 rdmac_mode |= RDMAC_MODE_SPLIT_ENABLE;
2044         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2045                 if (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
2046                         if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
2047                                 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
2048                                 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
2049                         }
2050                 }
2051         }
2052
2053         /* Setup host coalescing engine. */
2054         tw32(HOSTCC_MODE, 0);
2055         for (i = 0; i < 2000; i++) {
2056                 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
2057                         break;
2058                 udelay(10);
2059         }
2060
2061         tp->mac_mode = MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
2062                 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
2063         tw32_carefully(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
2064
2065         tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
2066         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
2067                 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
2068                                        GRC_LCLCTRL_GPIO_OUTPUT1);
2069         tw32_carefully(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
2070
2071         tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0);
2072         tr32(MAILBOX_INTERRUPT_0);
2073
2074         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
2075                 tw32_carefully(DMAC_MODE, DMAC_MODE_ENABLE);
2076         }
2077
2078         val = ( WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
2079                 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
2080                 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
2081                 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
2082                 WDMAC_MODE_LNGREAD_ENAB);
2083         if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) &&
2084                 ((tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) != 0) &&
2085                 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
2086                 val |= WDMAC_MODE_RX_ACCEL;
2087         }
2088         tw32_carefully(WDMAC_MODE, val);
2089
2090         if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
2091                 val = tr32(TG3PCI_X_CAPS);
2092                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
2093                         val &= PCIX_CAPS_BURST_MASK;
2094                         val |= (PCIX_CAPS_MAX_BURST_CPIOB << PCIX_CAPS_BURST_SHIFT);
2095                 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2096                         val &= ~(PCIX_CAPS_SPLIT_MASK | PCIX_CAPS_BURST_MASK);
2097                         val |= (PCIX_CAPS_MAX_BURST_CPIOB << PCIX_CAPS_BURST_SHIFT);
2098                         if (tp->tg3_flags & TG3_FLAG_SPLIT_MODE)
2099                                 val |= (tp->split_mode_max_reqs <<
2100                                         PCIX_CAPS_SPLIT_SHIFT);
2101                 }
2102                 tw32(TG3PCI_X_CAPS, val);
2103         }
2104
2105         tw32_carefully(RDMAC_MODE, rdmac_mode);
2106         {
2107                 static const uint32_t table_all[] = {
2108                         /* MTU + ethernet header + FCS + optional VLAN tag */
2109                         MAC_RX_MTU_SIZE, ETH_MAX_MTU + ETH_HLEN + 8,
2110                         
2111                         /* The slot time is changed by tg3_setup_phy if we
2112                          * run at gigabit with half duplex.
2113                          */
2114                         MAC_TX_LENGTHS, 
2115                         (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2116                         (6 << TX_LENGTHS_IPG_SHIFT) |
2117                         (32 << TX_LENGTHS_SLOT_TIME_SHIFT),
2118                         
2119                         /* Receive rules. */
2120                         MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS,
2121                         RCVLPC_CONFIG, 0x0181,
2122                         
2123                         /* Receive/send statistics. */
2124                         RCVLPC_STATS_ENABLE, 0xffffff,
2125                         RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE,
2126                         SNDDATAI_STATSENAB, 0xffffff,
2127                         SNDDATAI_STATSCTRL, (SNDDATAI_SCTRL_ENABLE |SNDDATAI_SCTRL_FASTUPD),
2128                         
2129                         /* Host coalescing engine */
2130                         HOSTCC_RXCOL_TICKS, 0,
2131                         HOSTCC_TXCOL_TICKS, LOW_TXCOL_TICKS,
2132                         HOSTCC_RXMAX_FRAMES, 1,
2133                         HOSTCC_TXMAX_FRAMES, LOW_RXMAX_FRAMES,
2134                         HOSTCC_RXCOAL_MAXF_INT, 1,
2135                         HOSTCC_TXCOAL_MAXF_INT, 0,
2136                         
2137                         /* Status/statistics block address. */
2138                         /* Etherboot lives below 4GB, so HIGH == 0 */
2139                         HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH, 0,
2140
2141                         /* No need to enable 32byte coalesce mode. */
2142                         HOSTCC_MODE, HOSTCC_MODE_ENABLE | 0,
2143                         
2144                         RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE,
2145                         RCVLPC_MODE, RCVLPC_MODE_ENABLE,
2146                         
2147                         RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE,
2148
2149                         SNDDATAC_MODE, SNDDATAC_MODE_ENABLE,
2150                         SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE,
2151                         RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB,
2152                         RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ,
2153                         SNDDATAI_MODE, SNDDATAI_MODE_ENABLE,
2154                         SNDBDI_MODE, SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE,
2155                         SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE,
2156                         
2157                         /* Accept all multicast frames. */
2158                         MAC_HASH_REG_0, 0xffffffff,
2159                         MAC_HASH_REG_1, 0xffffffff,
2160                         MAC_HASH_REG_2, 0xffffffff,
2161                         MAC_HASH_REG_3, 0xffffffff,
2162                 };
2163                 static const uint32_t table_not_5705[] = {
2164                         /* Host coalescing engine */
2165                         HOSTCC_RXCOAL_TICK_INT, 0,
2166                         HOSTCC_TXCOAL_TICK_INT, 0,
2167
2168                         /* Status/statistics block address. */
2169                         /* Etherboot lives below 4GB, so HIGH == 0 */
2170                         HOSTCC_STAT_COAL_TICKS, DEFAULT_STAT_COAL_TICKS,
2171                         HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH, 0,
2172                         HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK,
2173                         HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK,
2174
2175                         RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE,
2176
2177                         MBFREE_MODE, MBFREE_MODE_ENABLE,
2178                 };
2179                 TG3_WRITE_SETTINGS(table_all);
2180                 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
2181                         virt_to_bus(tp->hw_stats));
2182                 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
2183                         virt_to_bus(tp->hw_status));
2184                 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
2185                         TG3_WRITE_SETTINGS(table_not_5705);
2186                 }
2187         }
2188
2189         tp->tx_mode = TX_MODE_ENABLE;
2190         tw32_carefully(MAC_TX_MODE, tp->tx_mode);
2191
2192         tp->rx_mode = RX_MODE_ENABLE;
2193         tw32_carefully(MAC_RX_MODE, tp->rx_mode);
2194
2195         tp->mi_mode = MAC_MI_MODE_BASE;
2196         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
2197
2198         tw32(MAC_LED_CTRL, 0);
2199         tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2200         if (tp->phy_id == PHY_ID_SERDES) {
2201                 tw32_carefully(MAC_RX_MODE, RX_MODE_RESET);
2202         }
2203         tp->rx_mode |= RX_MODE_KEEP_VLAN_TAG; /* drop tagged vlan packets */
2204         tw32_carefully(MAC_RX_MODE, tp->rx_mode);
2205
2206         if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
2207                 tw32(MAC_SERDES_CFG, 0x616000);
2208
2209         /* Prevent chip from dropping frames when flow control
2210          * is enabled.
2211          */
2212         tw32(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
2213         tr32(MAC_LOW_WMARK_MAX_RX_FRAME);
2214
2215         err = tg3_setup_phy(tp);
2216
2217         /* Ignore CRC stats */
2218
2219         /* Initialize receive rules. */
2220         tw32(MAC_RCV_RULE_0,  0xc2000000 & RCV_RULE_DISABLE_MASK);
2221         tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
2222         tw32(MAC_RCV_RULE_1,  0x86000004 & RCV_RULE_DISABLE_MASK);
2223         tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
2224
2225         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
2226             || (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750))
2227                 limit = 8;
2228         else
2229                 limit = 16;
2230         if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
2231                 limit -= 4;
2232         switch (limit) {
2233         case 16:        tw32(MAC_RCV_RULE_15,  0); tw32(MAC_RCV_VALUE_15,  0);
2234         case 15:        tw32(MAC_RCV_RULE_14,  0); tw32(MAC_RCV_VALUE_14,  0);
2235         case 14:        tw32(MAC_RCV_RULE_13,  0); tw32(MAC_RCV_VALUE_13,  0);
2236         case 13:        tw32(MAC_RCV_RULE_12,  0); tw32(MAC_RCV_VALUE_12,  0);
2237         case 12:        tw32(MAC_RCV_RULE_11,  0); tw32(MAC_RCV_VALUE_11,  0);
2238         case 11:        tw32(MAC_RCV_RULE_10,  0); tw32(MAC_RCV_VALUE_10,  0);
2239         case 10:        tw32(MAC_RCV_RULE_9,  0);  tw32(MAC_RCV_VALUE_9,  0);
2240         case 9:         tw32(MAC_RCV_RULE_8,  0);  tw32(MAC_RCV_VALUE_8,  0);
2241         case 8:         tw32(MAC_RCV_RULE_7,  0);  tw32(MAC_RCV_VALUE_7,  0);
2242         case 7:         tw32(MAC_RCV_RULE_6,  0);  tw32(MAC_RCV_VALUE_6,  0);
2243         case 6:         tw32(MAC_RCV_RULE_5,  0);  tw32(MAC_RCV_VALUE_5,  0);
2244         case 5:         tw32(MAC_RCV_RULE_4,  0);  tw32(MAC_RCV_VALUE_4,  0);
2245         case 4:         /* tw32(MAC_RCV_RULE_3,  0); tw32(MAC_RCV_VALUE_3,  0); */
2246         case 3:         /* tw32(MAC_RCV_RULE_2,  0); tw32(MAC_RCV_VALUE_2,  0); */
2247         case 2:
2248         case 1:
2249         default:
2250                 break;
2251         };
2252
2253         return err;
2254 }
2255
2256
2257
2258 /* Chips other than 5700/5701 use the NVRAM for fetching info. */
2259 static void tg3_nvram_init(struct tg3 *tp)
2260 {
2261         tw32(GRC_EEPROM_ADDR,
2262              (EEPROM_ADDR_FSM_RESET |
2263               (EEPROM_DEFAULT_CLOCK_PERIOD <<
2264                EEPROM_ADDR_CLKPERD_SHIFT)));
2265
2266         mdelay(1);
2267
2268         /* Enable seeprom accesses. */
2269         tw32_carefully(GRC_LOCAL_CTRL,
2270                 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
2271
2272         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2273             GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2274                 uint32_t nvcfg1 = tr32(NVRAM_CFG1);
2275
2276                 tp->tg3_flags |= TG3_FLAG_NVRAM;
2277                 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
2278                         if (nvcfg1 & NVRAM_CFG1_BUFFERED_MODE)
2279                                 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
2280                 } else {
2281                         nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
2282                         tw32(NVRAM_CFG1, nvcfg1);
2283                 }
2284
2285         } else {
2286                 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
2287         }
2288 }
2289
2290
2291 static int tg3_nvram_read_using_eeprom(
2292         struct tg3 *tp __unused, uint32_t offset, uint32_t *val)
2293 {
2294         uint32_t tmp;
2295         int i;
2296
2297         if (offset > EEPROM_ADDR_ADDR_MASK ||
2298                 (offset % 4) != 0) {
2299                 return -EINVAL;
2300         }
2301
2302         tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2303                                         EEPROM_ADDR_DEVID_MASK |
2304                                         EEPROM_ADDR_READ);
2305         tw32(GRC_EEPROM_ADDR,
2306              tmp |
2307              (0 << EEPROM_ADDR_DEVID_SHIFT) |
2308              ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2309               EEPROM_ADDR_ADDR_MASK) |
2310              EEPROM_ADDR_READ | EEPROM_ADDR_START);
2311
2312         for (i = 0; i < 10000; i++) {
2313                 tmp = tr32(GRC_EEPROM_ADDR);
2314
2315                 if (tmp & EEPROM_ADDR_COMPLETE)
2316                         break;
2317                 udelay(100);
2318         }
2319         if (!(tmp & EEPROM_ADDR_COMPLETE)) {
2320                 return -EBUSY;
2321         }
2322
2323         *val = tr32(GRC_EEPROM_DATA);
2324         return 0;
2325 }
2326
2327 static int tg3_nvram_read(struct tg3 *tp, uint32_t offset, uint32_t *val)
2328 {
2329         int i, saw_done_clear;
2330
2331         if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2332                 return tg3_nvram_read_using_eeprom(tp, offset, val);
2333
2334         if (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED)
2335                 offset = ((offset / NVRAM_BUFFERED_PAGE_SIZE) <<
2336                           NVRAM_BUFFERED_PAGE_POS) +
2337                         (offset % NVRAM_BUFFERED_PAGE_SIZE);
2338
2339         if (offset > NVRAM_ADDR_MSK)
2340                 return -EINVAL;
2341
2342         tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2343         for (i = 0; i < 1000; i++) {
2344                 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2345                         break;
2346                 udelay(20);
2347         }
2348
2349         tw32(NVRAM_ADDR, offset);
2350         tw32(NVRAM_CMD,
2351              NVRAM_CMD_RD | NVRAM_CMD_GO |
2352              NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2353
2354         /* Wait for done bit to clear then set again. */
2355         saw_done_clear = 0;
2356         for (i = 0; i < 1000; i++) {
2357                 udelay(10);
2358                 if (!saw_done_clear &&
2359                     !(tr32(NVRAM_CMD) & NVRAM_CMD_DONE))
2360                         saw_done_clear = 1;
2361                 else if (saw_done_clear &&
2362                          (tr32(NVRAM_CMD) & NVRAM_CMD_DONE))
2363                         break;
2364         }
2365         if (i >= 1000) {
2366                 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2367                 return -EBUSY;
2368         }
2369
2370         *val = bswap_32(tr32(NVRAM_RDDATA));
2371         tw32(NVRAM_SWARB, 0x20);
2372
2373         return 0;
2374 }
2375
2376 struct subsys_tbl_ent {
2377         uint16_t subsys_vendor, subsys_devid;
2378         uint32_t phy_id;
2379 };
2380
2381 static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
2382         /* Broadcom boards. */
2383         { 0x14e4, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
2384         { 0x14e4, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
2385         { 0x14e4, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
2386         { 0x14e4, 0x0003, PHY_ID_SERDES  }, /* BCM95700A9 */
2387         { 0x14e4, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
2388         { 0x14e4, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
2389         { 0x14e4, 0x0007, PHY_ID_SERDES  }, /* BCM95701A7 */
2390         { 0x14e4, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
2391         { 0x14e4, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
2392         { 0x14e4, 0x0009, PHY_ID_BCM5701 }, /* BCM95703Ax1 */
2393         { 0x14e4, 0x8009, PHY_ID_BCM5701 }, /* BCM95703Ax2 */
2394
2395         /* 3com boards. */
2396         { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
2397         { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
2398         /* { PCI_VENDOR_ID_3COM, 0x1002, PHY_ID_XXX },     3C996CT */
2399         /* { PCI_VENDOR_ID_3COM, 0x1003, PHY_ID_XXX },     3C997T */
2400         { PCI_VENDOR_ID_3COM, 0x1004, PHY_ID_SERDES  }, /* 3C996SX */
2401         /* { PCI_VENDOR_ID_3COM, 0x1005, PHY_ID_XXX },     3C997SZ */
2402         { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
2403         { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
2404
2405         /* DELL boards. */
2406         { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
2407         { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
2408         { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
2409         { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
2410         { PCI_VENDOR_ID_DELL, 0x0179, PHY_ID_BCM5751 }, /* EtherXpress */
2411         
2412         /* Fujitsu Siemens Computer */
2413         { PCI_VENDOR_ID_FSC, 0x105d, PHY_ID_BCM5751 }, /* Futro C200 */ 
2414
2415         /* Compaq boards. */
2416         { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
2417         { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
2418         { PCI_VENDOR_ID_COMPAQ, 0x007d, PHY_ID_SERDES  }, /* CHANGELING */
2419         { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
2420         { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }  /* NC7780_2 */
2421 };
2422
2423 static int tg3_phy_probe(struct tg3 *tp)
2424 {
2425         uint32_t eeprom_phy_id, hw_phy_id_1, hw_phy_id_2;
2426         uint32_t hw_phy_id, hw_phy_id_masked;
2427         enum phy_led_mode eeprom_led_mode;
2428         uint32_t val;
2429         unsigned i;
2430         int eeprom_signature_found, err;
2431
2432         tp->phy_id = PHY_ID_INVALID;
2433
2434         for (i = 0; i < sizeof(subsys_id_to_phy_id)/sizeof(subsys_id_to_phy_id[0]); i++) {
2435                 if ((subsys_id_to_phy_id[i].subsys_vendor == tp->subsystem_vendor) &&
2436                         (subsys_id_to_phy_id[i].subsys_devid == tp->subsystem_device)) {
2437                         tp->phy_id = subsys_id_to_phy_id[i].phy_id;
2438                         break;
2439                 }
2440         }
2441
2442         eeprom_phy_id = PHY_ID_INVALID;
2443         eeprom_led_mode = led_mode_auto;
2444         eeprom_signature_found = 0;
2445         tg3_read_mem(NIC_SRAM_DATA_SIG, &val);
2446         if (val == NIC_SRAM_DATA_SIG_MAGIC) {
2447                 uint32_t nic_cfg;
2448
2449                 tg3_read_mem(NIC_SRAM_DATA_CFG, &nic_cfg);
2450                 tp->nic_sram_data_cfg = nic_cfg;
2451
2452                 eeprom_signature_found = 1;
2453
2454                 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
2455                     NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER) {
2456                         eeprom_phy_id = PHY_ID_SERDES;
2457                 } else {
2458                         uint32_t nic_phy_id;
2459
2460                         tg3_read_mem(NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
2461                         if (nic_phy_id != 0) {
2462                                 uint32_t id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
2463                                 uint32_t id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
2464
2465                                 eeprom_phy_id  = (id1 >> 16) << 10;
2466                                 eeprom_phy_id |= (id2 & 0xfc00) << 16;
2467                                 eeprom_phy_id |= (id2 & 0x03ff) <<  0;
2468                         }
2469                 }
2470
2471                 switch (nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK) {
2472                 case NIC_SRAM_DATA_CFG_LED_TRIPLE_SPD:
2473                         eeprom_led_mode = led_mode_three_link;
2474                         break;
2475
2476                 case NIC_SRAM_DATA_CFG_LED_LINK_SPD:
2477                         eeprom_led_mode = led_mode_link10;
2478                         break;
2479
2480                 default:
2481                         eeprom_led_mode = led_mode_auto;
2482                         break;
2483                 };
2484                 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
2485                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
2486                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) &&
2487                         (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP)) {
2488                         tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
2489                 }
2490
2491                 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE)
2492                         tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
2493                 if (nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL)
2494                         tp->tg3_flags |= TG3_FLAG_SERDES_WOL_CAP;
2495         }
2496
2497         /* Now read the physical PHY_ID from the chip and verify
2498          * that it is sane.  If it doesn't look good, we fall back
2499          * to either the hard-coded table based PHY_ID and failing
2500          * that the value found in the eeprom area.
2501          */
2502         err  = tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
2503         err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
2504
2505         hw_phy_id  = (hw_phy_id_1 & 0xffff) << 10;
2506         hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
2507         hw_phy_id |= (hw_phy_id_2 & 0x03ff) <<  0;
2508
2509         hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
2510
2511         if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
2512                 tp->phy_id = hw_phy_id;
2513         } else {
2514                 /* phy_id currently holds the value found in the
2515                  * subsys_id_to_phy_id[] table or PHY_ID_INVALID
2516                  * if a match was not found there.
2517                  */
2518                 if (tp->phy_id == PHY_ID_INVALID) {
2519                         if (!eeprom_signature_found ||
2520                             !KNOWN_PHY_ID(eeprom_phy_id & PHY_ID_MASK))
2521                                 return -ENODEV;
2522                         tp->phy_id = eeprom_phy_id;
2523                 }
2524         }
2525
2526         err = tg3_phy_reset(tp);
2527         if (err)
2528                 return err;
2529
2530         if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2531             tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
2532                 uint32_t mii_tg3_ctrl;
2533                 
2534                 /* These chips, when reset, only advertise 10Mb
2535                  * capabilities.  Fix that.
2536                  */
2537                 err  = tg3_writephy(tp, MII_ADVERTISE,
2538                                     (ADVERTISE_CSMA |
2539                                      ADVERTISE_PAUSE_CAP |
2540                                      ADVERTISE_10HALF |
2541                                      ADVERTISE_10FULL |
2542                                      ADVERTISE_100HALF |
2543                                      ADVERTISE_100FULL));
2544                 mii_tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
2545                                 MII_TG3_CTRL_ADV_1000_FULL |
2546                                 MII_TG3_CTRL_AS_MASTER |
2547                                 MII_TG3_CTRL_ENABLE_AS_MASTER);
2548                 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2549                         mii_tg3_ctrl = 0;
2550
2551                 err |= tg3_writephy(tp, MII_TG3_CTRL, mii_tg3_ctrl);
2552                 err |= tg3_writephy(tp, MII_BMCR,
2553                                     (BMCR_ANRESTART | BMCR_ANENABLE));
2554         }
2555
2556         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
2557                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2558                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2559                 tg3_writedsp(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
2560         }
2561
2562         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2563                 tg3_writephy(tp, 0x1c, 0x8d68);
2564                 tg3_writephy(tp, 0x1c, 0x8d68);
2565         }
2566
2567         /* Enable Ethernet@WireSpeed */
2568         tg3_phy_set_wirespeed(tp);
2569
2570         if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
2571                 err = tg3_init_5401phy_dsp(tp);
2572         }
2573
2574         /* Determine the PHY led mode. 
2575          * Be careful if this gets set wrong it can result in an inability to 
2576          * establish a link.
2577          */
2578         if (tp->phy_id == PHY_ID_SERDES) {
2579                 tp->led_mode = led_mode_three_link;
2580         }
2581         else if (tp->subsystem_vendor == PCI_VENDOR_ID_DELL) {
2582                 tp->led_mode = led_mode_link10;
2583         } else {
2584                 tp->led_mode = led_mode_three_link;
2585                 if (eeprom_signature_found &&
2586                     eeprom_led_mode != led_mode_auto)
2587                         tp->led_mode = eeprom_led_mode;
2588         }
2589
2590         if (tp->phy_id == PHY_ID_SERDES)
2591                 tp->link_config.advertising =
2592                         (ADVERTISED_1000baseT_Half |
2593                          ADVERTISED_1000baseT_Full |
2594                          ADVERTISED_Autoneg |
2595                          ADVERTISED_FIBRE);
2596         if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2597                 tp->link_config.advertising &=
2598                         ~(ADVERTISED_1000baseT_Half |
2599                           ADVERTISED_1000baseT_Full);
2600
2601         return err;
2602 }
2603
2604 #if SUPPORT_PARTNO_STR
2605 static void tg3_read_partno(struct tg3 *tp)
2606 {
2607         unsigned char vpd_data[256];
2608         int i;
2609
2610         for (i = 0; i < 256; i += 4) {
2611                 uint32_t tmp;
2612
2613                 if (tg3_nvram_read(tp, 0x100 + i, &tmp))
2614                         goto out_not_found;
2615
2616                 vpd_data[i + 0] = ((tmp >>  0) & 0xff);
2617                 vpd_data[i + 1] = ((tmp >>  8) & 0xff);
2618                 vpd_data[i + 2] = ((tmp >> 16) & 0xff);
2619                 vpd_data[i + 3] = ((tmp >> 24) & 0xff);
2620         }
2621
2622         /* Now parse and find the part number. */
2623         for (i = 0; i < 256; ) {
2624                 unsigned char val = vpd_data[i];
2625                 int block_end;
2626
2627                 if (val == 0x82 || val == 0x91) {
2628                         i = (i + 3 +
2629                              (vpd_data[i + 1] +
2630                               (vpd_data[i + 2] << 8)));
2631                         continue;
2632                 }
2633
2634                 if (val != 0x90)
2635                         goto out_not_found;
2636
2637                 block_end = (i + 3 +
2638                              (vpd_data[i + 1] +
2639                               (vpd_data[i + 2] << 8)));
2640                 i += 3;
2641                 while (i < block_end) {
2642                         if (vpd_data[i + 0] == 'P' &&
2643                             vpd_data[i + 1] == 'N') {
2644                                 int partno_len = vpd_data[i + 2];
2645
2646                                 if (partno_len > 24)
2647                                         goto out_not_found;
2648
2649                                 memcpy(tp->board_part_number,
2650                                        &vpd_data[i + 3],
2651                                        partno_len);
2652
2653                                 /* Success. */
2654                                 return;
2655                         }
2656                 }
2657
2658                 /* Part number not found. */
2659                 goto out_not_found;
2660         }
2661
2662 out_not_found:
2663         memcpy(tp->board_part_number, "none", sizeof("none"));
2664 }
2665 #else
2666 #define tg3_read_partno(TP) ((TP)->board_part_number[0] = '\0')
2667 #endif
2668
2669 static int tg3_get_invariants(struct tg3 *tp)
2670 {
2671         uint32_t misc_ctrl_reg;
2672         uint32_t pci_state_reg, grc_misc_cfg;
2673         uint16_t pci_cmd;
2674         uint8_t  pci_latency;
2675         uint32_t val ;
2676         int err;
2677
2678         /* Read the subsystem vendor and device ids */
2679         pci_read_config_word(tp->pdev, PCI_SUBSYSTEM_VENDOR_ID, &tp->subsystem_vendor);
2680         pci_read_config_word(tp->pdev, PCI_SUBSYSTEM_ID, &tp->subsystem_device);
2681
2682         /* The sun_5704 code needs infrastructure etherboot does have
2683          * ignore it for now.
2684          */
2685
2686         /* If we have an AMD 762 or Intel ICH/ICH0 chipset, write
2687          * reordering to the mailbox registers done by the host
2688          * controller can cause major troubles.  We read back from
2689          * every mailbox register write to force the writes to be
2690          * posted to the chip in order.
2691          *
2692          * TG3_FLAG_MBOX_WRITE_REORDER has been forced on.
2693          */
2694
2695         /* Force memory write invalidate off.  If we leave it on,
2696          * then on 5700_BX chips we have to enable a workaround.
2697          * The workaround is to set the TG3PCI_DMA_RW_CTRL boundry
2698          * to match the cacheline size.  The Broadcom driver have this
2699          * workaround but turns MWI off all the times so never uses
2700          * it.  This seems to suggest that the workaround is insufficient.
2701          */
2702         pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
2703         pci_cmd &= ~PCI_COMMAND_INVALIDATE;
2704         /* Also, force SERR#/PERR# in PCI command. */
2705         pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
2706         pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
2707
2708         /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
2709          * has the register indirect write enable bit set before
2710          * we try to access any of the MMIO registers.  It is also
2711          * critical that the PCI-X hw workaround situation is decided
2712          * before that as well.
2713          */
2714         pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL, &misc_ctrl_reg);
2715
2716         tp->pci_chip_rev_id = (misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT);
2717
2718         /* Initialize misc host control in PCI block. */
2719         tp->misc_host_ctrl |= (misc_ctrl_reg &
2720                                MISC_HOST_CTRL_CHIPREV);
2721         pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
2722                                tp->misc_host_ctrl);
2723
2724         pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER, &pci_latency);
2725         if (pci_latency < 64) {
2726                 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER, 64);
2727         }
2728
2729         pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &pci_state_reg);
2730
2731         /* If this is a 5700 BX chipset, and we are in PCI-X
2732          * mode, enable register write workaround.
2733          *
2734          * The workaround is to use indirect register accesses
2735          * for all chip writes not to mailbox registers.
2736          *
2737          * In etherboot to simplify things we just always use this work around.
2738          */
2739         if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0) {
2740                 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
2741         }
2742         /* Back to back register writes can cause problems on the 5701,
2743          * the workaround is to read back all reg writes except those to
2744          * mailbox regs.
2745          * In etherboot we always use indirect register accesses so
2746          * we don't see this.
2747          */
2748
2749         if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
2750                 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
2751         if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
2752                 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
2753
2754         /* Chip-specific fixup from Broadcom driver */
2755         if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
2756             (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
2757                 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
2758                 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
2759         }
2760
2761         /* determine if it is PCIE system */
2762         // Alf : I have no idea what this is about...
2763         // But it's definitely usefull
2764         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
2765           val = tr32(TG3PCI_MSI_CAP_ID) ;
2766           if (((val >> 8) & 0xff) == T3_PCIE_CAPABILITY_ID_REG) {
2767             val = tr32(T3_PCIE_CAPABILITY_ID_REG) ;
2768             if ((val & 0xff) == T3_PCIE_CAPABILITY_ID) {
2769               tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS ;
2770             }
2771           }
2772         }
2773
2774         /* Force the chip into D0. */
2775         tg3_set_power_state_0(tp);
2776
2777         /* Etherboot does not ask the tg3 to do checksums */
2778         /* Etherboot does not ask the tg3 to do jumbo frames */
2779         /* Ehterboot does not ask the tg3 to use WakeOnLan. */
2780
2781         /* A few boards don't want Ethernet@WireSpeed phy feature */
2782         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
2783             (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
2784                 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
2785                         (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
2786                         (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1))) {
2787                 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
2788         }
2789
2790         /* Avoid tagged irq status etherboot does not use irqs */
2791
2792         /* Only 5701 and later support tagged irq status mode.
2793          * Also, 5788 chips cannot use tagged irq status.
2794          *
2795          * However, since etherboot does not use irqs avoid tagged irqs
2796          * status  because the interrupt condition is more difficult to
2797          * fully clear in that mode.
2798          */
2799         
2800         /* Since some 5700_AX && 5700_BX have problems with 32BYTE
2801          * coalesce_mode, and the rest work fine anything set.
2802          * Don't enable HOST_CC_MODE_32BYTE in etherboot.
2803          */
2804
2805         /* Initialize MAC MI mode, polling disabled. */
2806         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
2807
2808         /* Initialize data/descriptor byte/word swapping. */
2809         tw32(GRC_MODE, tp->grc_mode);
2810
2811         tg3_switch_clocks(tp);
2812
2813         /* Clear this out for sanity. */
2814         tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
2815
2816         /* Etherboot does not need to check if the PCIX_TARGET_HWBUG
2817          * is needed.  It always uses it.
2818          */
2819         
2820         udelay(50);
2821         tg3_nvram_init(tp);
2822
2823         /* The TX descriptors will reside in main memory.
2824          */
2825
2826         /* See which board we are using.
2827          */
2828         grc_misc_cfg = tr32(GRC_MISC_CFG);
2829         grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
2830
2831         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
2832             grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5704CIOBE) {
2833                 tp->tg3_flags |= TG3_FLAG_SPLIT_MODE;
2834                 tp->split_mode_max_reqs = SPLIT_MODE_5704_MAX_REQ;
2835         }
2836
2837         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
2838             (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
2839              grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
2840                 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
2841
2842 #define PCI_DEVICE_ID_TIGON3_5901       0x170d
2843 #define PCI_DEVICE_ID_TIGON3_5901_2     0x170e
2844
2845         /* these are limited to 10/100 only */
2846         if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) &&
2847                     ((grc_misc_cfg == 0x8000) || (grc_misc_cfg == 0x4000))) ||
2848                 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
2849                         (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM) &&
2850                         ((tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901) ||
2851                                 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2)))) {
2852                 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
2853         }
2854
2855         err = tg3_phy_probe(tp);
2856         if (err) {
2857                 printf("phy probe failed, err %d\n", err);
2858         }
2859
2860         tg3_read_partno(tp);
2861
2862
2863         /* 5700 BX chips need to have their TX producer index mailboxes
2864          * written twice to workaround a bug.
2865          * In etherboot we do this unconditionally to simplify things.
2866          */
2867
2868         /* 5700 chips can get confused if TX buffers straddle the
2869          * 4GB address boundary in some cases.
2870          * 
2871          * In etherboot we can ignore the problem as etherboot lives below 4GB.
2872          */
2873
2874         /* In etherboot wake-on-lan is unconditionally disabled */
2875         return err;
2876 }
2877
2878 static int  tg3_get_device_address(struct tg3 *tp)
2879 {
2880         struct nic *nic = tp->nic;
2881         uint32_t hi, lo, mac_offset;
2882
2883         if (PCI_FUNC(tp->pdev->devfn) == 0)
2884                 mac_offset = 0x7c;
2885         else
2886                 mac_offset = 0xcc;
2887
2888         /* First try to get it from MAC address mailbox. */
2889         tg3_read_mem(NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
2890         if ((hi >> 16) == 0x484b) {
2891                 nic->node_addr[0] = (hi >>  8) & 0xff;
2892                 nic->node_addr[1] = (hi >>  0) & 0xff;
2893
2894                 tg3_read_mem(NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
2895                 nic->node_addr[2] = (lo >> 24) & 0xff;
2896                 nic->node_addr[3] = (lo >> 16) & 0xff;
2897                 nic->node_addr[4] = (lo >>  8) & 0xff;
2898                 nic->node_addr[5] = (lo >>  0) & 0xff;
2899         }
2900         /* Next, try NVRAM. */
2901         else if (!tg3_nvram_read(tp, mac_offset + 0, &hi) &&
2902                  !tg3_nvram_read(tp, mac_offset + 4, &lo)) {
2903                 nic->node_addr[0] = ((hi >> 16) & 0xff);
2904                 nic->node_addr[1] = ((hi >> 24) & 0xff);
2905                 nic->node_addr[2] = ((lo >>  0) & 0xff);
2906                 nic->node_addr[3] = ((lo >>  8) & 0xff);
2907                 nic->node_addr[4] = ((lo >> 16) & 0xff);
2908                 nic->node_addr[5] = ((lo >> 24) & 0xff);
2909         }
2910         /* Finally just fetch it out of the MAC control regs. */
2911         else {
2912                 hi = tr32(MAC_ADDR_0_HIGH);
2913                 lo = tr32(MAC_ADDR_0_LOW);
2914
2915                 nic->node_addr[5] = lo & 0xff;
2916                 nic->node_addr[4] = (lo >> 8) & 0xff;
2917                 nic->node_addr[3] = (lo >> 16) & 0xff;
2918                 nic->node_addr[2] = (lo >> 24) & 0xff;
2919                 nic->node_addr[1] = hi & 0xff;
2920                 nic->node_addr[0] = (hi >> 8) & 0xff;
2921         }
2922
2923         return 0;
2924 }
2925
2926
2927 static int tg3_setup_dma(struct tg3 *tp)
2928 {
2929         tw32(TG3PCI_CLOCK_CTRL, 0);
2930
2931         if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) == 0) {
2932                 tp->dma_rwctrl =
2933                         (0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
2934                         (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT) |
2935                         (0x7 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
2936                         (0x7 << DMA_RWCTRL_READ_WATER_SHIFT) |
2937                         (0x0f << DMA_RWCTRL_MIN_DMA_SHIFT);
2938                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2939                         tp->dma_rwctrl &= ~(DMA_RWCTRL_MIN_DMA << DMA_RWCTRL_MIN_DMA_SHIFT);
2940                 }
2941         } else {
2942                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
2943                         tp->dma_rwctrl =
2944                                 (0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
2945                                 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT) |
2946                                 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
2947                                 (0x7 << DMA_RWCTRL_READ_WATER_SHIFT) |
2948                                 (0x00 << DMA_RWCTRL_MIN_DMA_SHIFT);
2949                 else
2950                         tp->dma_rwctrl =
2951                                 (0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
2952                                 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT) |
2953                                 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
2954                                 (0x3 << DMA_RWCTRL_READ_WATER_SHIFT) |
2955                                 (0x0f << DMA_RWCTRL_MIN_DMA_SHIFT);
2956
2957                 /* Wheee, some more chip bugs... */
2958                 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
2959                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)) {
2960                         uint32_t ccval = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
2961
2962                         if ((ccval == 0x6) || (ccval == 0x7)) {
2963                                 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
2964                         }
2965                 }
2966         }
2967
2968         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
2969                 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)) {
2970                 tp->dma_rwctrl &= ~(DMA_RWCTRL_MIN_DMA << DMA_RWCTRL_MIN_DMA_SHIFT);
2971         }
2972
2973         /*
2974           Alf : Tried that, but it does not work. Should be this way though :-(
2975         if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
2976           tp->dma_rwctrl |= 0x001f0000;
2977         }
2978         */
2979         tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
2980
2981         tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
2982
2983         return 0;
2984 }
2985
2986 static void tg3_init_link_config(struct tg3 *tp)
2987 {
2988         tp->link_config.advertising =
2989                 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2990                  ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2991                  ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
2992                  ADVERTISED_Autoneg | ADVERTISED_MII);
2993         tp->carrier_ok = 0;
2994         tp->link_config.active_speed = SPEED_INVALID;
2995         tp->link_config.active_duplex = DUPLEX_INVALID;
2996 }
2997
2998
2999 #if SUPPORT_PHY_STR
3000 static const char * tg3_phy_string(struct tg3 *tp)
3001 {
3002         switch (tp->phy_id & PHY_ID_MASK) {
3003         case PHY_ID_BCM5400:    return "5400";
3004         case PHY_ID_BCM5401:    return "5401";
3005         case PHY_ID_BCM5411:    return "5411";
3006         case PHY_ID_BCM5701:    return "5701";
3007         case PHY_ID_BCM5703:    return "5703";
3008         case PHY_ID_BCM5704:    return "5704";
3009         case PHY_ID_BCM5705:    return "5705";
3010         case PHY_ID_BCM5750:    return "5750";
3011         case PHY_ID_BCM5751:    return "5751"; 
3012         case PHY_ID_BCM8002:    return "8002/serdes";
3013         case PHY_ID_SERDES:     return "serdes";
3014         default:                return "unknown";
3015         };
3016 }
3017 #else
3018 #define tg3_phy_string(TP) "?"
3019 #endif
3020
3021
3022 static void tg3_poll_link(struct tg3 *tp)
3023 {
3024         uint32_t mac_stat;
3025
3026         mac_stat = tr32(MAC_STATUS);
3027         if (tp->phy_id == PHY_ID_SERDES) {
3028                 if (tp->carrier_ok?
3029                         (mac_stat & MAC_STATUS_LNKSTATE_CHANGED):
3030                         (mac_stat & MAC_STATUS_PCS_SYNCED)) {
3031                         tw32_carefully(MAC_MODE, tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK);
3032                         tw32_carefully(MAC_MODE, tp->mac_mode);
3033
3034                         tg3_setup_phy(tp);
3035                 }
3036         }
3037         else {
3038                 if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED) {
3039                         tg3_setup_phy(tp);
3040                 }
3041         }
3042 }
3043
3044 /**************************************************************************
3045 POLL - Wait for a frame
3046 ***************************************************************************/
3047 static void tg3_ack_irqs(struct tg3 *tp)
3048 {
3049         if (tp->hw_status->status & SD_STATUS_UPDATED) {
3050                 /*
3051                  * writing any value to intr-mbox-0 clears PCI INTA# and
3052                  * chip-internal interrupt pending events.
3053                  * writing non-zero to intr-mbox-0 additional tells the
3054                  * NIC to stop sending us irqs, engaging "in-intr-handler"
3055                  * event coalescing.
3056                  */
3057                 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 
3058                         0x00000001);
3059                 /*
3060                  * Flush PCI write.  This also guarantees that our
3061                  * status block has been flushed to host memory.
3062                  */
3063                 tr32(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW);
3064                 tp->hw_status->status &= ~SD_STATUS_UPDATED;
3065         }
3066 }
3067
3068 static int tg3_poll(struct nic *nic, int retrieve)
3069 {
3070         /* return true if there's an ethernet packet ready to read */
3071         /* nic->packet should contain data on return */
3072         /* nic->packetlen should contain length of data */
3073
3074         struct tg3 *tp = &tg3;
3075         int result;
3076
3077         result = 0;
3078
3079         if ( (tp->hw_status->idx[0].rx_producer != tp->rx_rcb_ptr) && !retrieve ) 
3080           return 1;
3081
3082         tg3_ack_irqs(tp);
3083
3084         if (tp->hw_status->idx[0].rx_producer != tp->rx_rcb_ptr) {
3085                 struct tg3_rx_buffer_desc *desc;
3086                 unsigned int len;
3087                 desc = &tp->rx_rcb[tp->rx_rcb_ptr];
3088                 if ((desc->opaque & RXD_OPAQUE_RING_MASK) == RXD_OPAQUE_RING_STD) {
3089                         len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4; /* omit crc */
3090                         
3091                         nic->packetlen = len;
3092                         memcpy(nic->packet, bus_to_virt(desc->addr_lo), len);
3093                         result = 1;
3094                 }
3095                 tp->rx_rcb_ptr = (tp->rx_rcb_ptr + 1) % TG3_RX_RCB_RING_SIZE;
3096                 
3097                 /* ACK the status ring */
3098                 tw32_mailbox2(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, tp->rx_rcb_ptr);
3099
3100                 /* Refill RX ring. */
3101                 if (result) {
3102                         tp->rx_std_ptr = (tp->rx_std_ptr + 1) % TG3_RX_RING_SIZE;
3103                         tw32_mailbox2(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW, tp->rx_std_ptr);
3104                 }
3105         }
3106         tg3_poll_link(tp);
3107         return result;
3108 }
3109
3110 /**************************************************************************
3111 TRANSMIT - Transmit a frame
3112 ***************************************************************************/
3113 #if 0
3114 static void tg3_set_txd(struct tg3 *tp, int entry,
3115         dma_addr_t mapping, int len, uint32_t flags,
3116         uint32_t mss_and_is_end)
3117 {
3118         struct tg3_tx_buffer_desc *txd =  &tp->tx_ring[entry];
3119         int is_end = (mss_and_is_end & 0x1);
3120         if (is_end) {
3121                 flags |= TXD_FLAG_END;
3122         }
3123
3124         txd->addr_hi   = 0;
3125         txd->addr_lo   = mapping & 0xffffffff;
3126         txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
3127         txd->vlan_tag  = 0 << TXD_VLAN_TAG_SHIFT;
3128 }
3129 #endif
3130
3131 static void tg3_transmit(struct nic *nic, const char *dst_addr,
3132         unsigned int type, unsigned int size, const char *packet)
3133 {
3134         static int frame_idx;
3135         struct eth_frame *frame;
3136         
3137         /* send the packet to destination */
3138         struct tg3_tx_buffer_desc *txd;
3139         struct tg3 *tp;
3140         uint32_t entry;
3141         int i;
3142
3143         /* Wait until there is a free packet frame */
3144         tp = &tg3;
3145         i = 0;
3146         entry = tp->tx_prod;
3147         while((tp->hw_status->idx[0].tx_consumer != entry) &&
3148                 (tp->hw_status->idx[0].tx_consumer != PREV_TX(entry))) {
3149                 mdelay(10);     /* give the nick a chance */
3150                 if (++i > 500) { /* timeout 5s for transmit */
3151                         printf("transmit timed out\n");
3152                         tg3_halt(tp);
3153                         tg3_setup_hw(tp);
3154                         return;
3155                 }
3156         }
3157         if (i != 0) {
3158                 printf("#");
3159         }
3160         
3161         /* Copy the packet to the our local buffer */
3162         frame = &tg3_bss.tx_frame[frame_idx];
3163         memcpy(frame->dst_addr, dst_addr, ETH_ALEN);
3164         memcpy(frame->src_addr, nic->node_addr, ETH_ALEN);
3165         frame->type = htons(type);
3166         memset(frame->data, 0, sizeof(frame->data));
3167         memcpy(frame->data, packet, size);
3168
3169         /* Setup the ring buffer entry to transmit */
3170         txd            = &tp->tx_ring[entry];
3171         txd->addr_hi   = 0; /* Etherboot runs under 4GB */
3172         txd->addr_lo   = virt_to_bus(frame);
3173         txd->len_flags = ((size + ETH_HLEN) << TXD_LEN_SHIFT) | TXD_FLAG_END;
3174         txd->vlan_tag  = 0 << TXD_VLAN_TAG_SHIFT;
3175
3176         /* Advance to the next entry */
3177         entry = NEXT_TX(entry);
3178         frame_idx ^= 1;
3179
3180         /* Packets are ready, update Tx producer idx local and on card */
3181         tw32_mailbox((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
3182         tw32_mailbox2((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
3183         tp->tx_prod = entry;
3184 }
3185
3186 /**************************************************************************
3187 DISABLE - Turn off ethernet interface
3188 ***************************************************************************/
3189 static void tg3_disable ( struct nic *nic __unused ) {
3190         struct tg3 *tp = &tg3;
3191         /* put the card in its initial state */
3192         /* This function serves 3 purposes.
3193          * This disables DMA and interrupts so we don't receive
3194          *  unexpected packets or interrupts from the card after
3195          *  etherboot has finished. 
3196          * This frees resources so etherboot may use
3197          *  this driver on another interface
3198          * This allows etherboot to reinitialize the interface
3199          *  if something is something goes wrong.
3200          */
3201         tg3_halt(tp);
3202         tp->tg3_flags &= ~(TG3_FLAG_INIT_COMPLETE|TG3_FLAG_GOT_SERDES_FLOWCTL);
3203         tp->carrier_ok = 0;
3204         iounmap((void *)tp->regs);
3205 }
3206
3207 /**************************************************************************
3208 IRQ - Enable, Disable, or Force interrupts
3209 ***************************************************************************/
3210 static void tg3_irq(struct nic *nic __unused, irq_action_t action __unused)
3211 {
3212   switch ( action ) {
3213   case DISABLE :
3214     break;
3215   case ENABLE :
3216     break;
3217   case FORCE :
3218     break;
3219   }
3220 }
3221
3222 static struct nic_operations tg3_operations = {
3223         .connect        = dummy_connect,
3224         .poll           = tg3_poll,
3225         .transmit       = tg3_transmit,
3226         .irq            = tg3_irq,
3227
3228 };
3229
3230 /**************************************************************************
3231 PROBE - Look for an adapter, this routine's visible to the outside
3232 You should omit the last argument struct pci_device * for a non-PCI NIC
3233 ***************************************************************************/
3234 static int tg3_probe ( struct nic *nic, struct pci_device *pdev ) {
3235
3236         struct tg3 *tp = &tg3;
3237         unsigned long tg3reg_base, tg3reg_len;
3238         int i, err, pm_cap;
3239
3240         memset(tp, 0, sizeof(*tp));
3241
3242         adjust_pci_device(pdev);
3243
3244         pci_fill_nic ( nic, pdev );
3245         nic->irqno  = 0;
3246
3247         /* Find power-management capability. */
3248         pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
3249         if (pm_cap == 0) {
3250                 printf("Cannot find PowerManagement capability, aborting.\n");
3251                 return 0;
3252         }
3253         tg3reg_base = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
3254         if (tg3reg_base == -1UL) {
3255                 printf("Unuseable bar\n");
3256                 return 0;
3257         }
3258         tg3reg_len  = pci_bar_size(pdev,  PCI_BASE_ADDRESS_0);
3259
3260         tp->pdev       = pdev;
3261         tp->nic        = nic;
3262         tp->pm_cap     = pm_cap;
3263         tp->rx_mode    = 0;
3264         tp->tx_mode    = 0;
3265         tp->mi_mode    = MAC_MI_MODE_BASE;
3266         tp->tg3_flags  = 0 & ~TG3_FLAG_INIT_COMPLETE; 
3267         
3268         /* The word/byte swap controls here control register access byte
3269          * swapping.  DMA data byte swapping is controlled in the GRC_MODE
3270          * setting below.
3271          */
3272         tp->misc_host_ctrl =
3273                 MISC_HOST_CTRL_MASK_PCI_INT |
3274                 MISC_HOST_CTRL_WORD_SWAP |
3275                 MISC_HOST_CTRL_INDIR_ACCESS |
3276                 MISC_HOST_CTRL_PCISTATE_RW;
3277
3278         /* The NONFRM (non-frame) byte/word swap controls take effect
3279          * on descriptor entries, anything which isn't packet data.
3280          *
3281          * The StrongARM chips on the board (one for tx, one for rx)
3282          * are running in big-endian mode.
3283          */
3284         tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
3285                         GRC_MODE_WSWAP_NONFRM_DATA);
3286 #if __BYTE_ORDER == __BIG_ENDIAN
3287         tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
3288 #endif
3289         tp->regs = (unsigned long) ioremap(tg3reg_base, tg3reg_len);
3290         if (tp->regs == 0UL) {
3291                 printf("Cannot map device registers, aborting\n");
3292                 return 0;
3293         }
3294
3295         tg3_init_link_config(tp);
3296
3297         err = tg3_get_invariants(tp);
3298         if (err) {
3299                 printf("Problem fetching invariants of chip, aborting.\n");
3300                 goto err_out_iounmap;
3301         }
3302
3303         err = tg3_get_device_address(tp);
3304         if (err) {
3305                 printf("Could not obtain valid ethernet address, aborting.\n");
3306                 goto err_out_iounmap;
3307         }
3308
3309         DBG ( "Ethernet addr: %s\n", eth_ntoa ( nic->node_addr ) );
3310
3311         tg3_setup_dma(tp);
3312
3313         /* Now that we have fully setup the chip, save away a snapshot
3314          * of the PCI config space.  We need to restore this after
3315          * GRC_MISC_CFG core clock resets and some resume events.
3316          */
3317         pci_save_state(tp->pdev, tp->pci_cfg_state);
3318
3319         printf("Tigon3 [partno(%s) rev %hx PHY(%s)] (PCI%s:%s:%s)\n",
3320                 tp->board_part_number,
3321                 tp->pci_chip_rev_id,
3322                 tg3_phy_string(tp),
3323                 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ? "X" : ""),
3324                 ((tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED) ?
3325                         ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ? "133MHz" : "66MHz") :
3326                         ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ? "100MHz" : "33MHz")),
3327                 ((tp->tg3_flags & TG3_FLAG_PCI_32BIT) ? "32-bit" : "64-bit"));
3328
3329
3330         err = tg3_setup_hw(tp); 
3331         if (err) {
3332                 goto err_out_disable;
3333         } 
3334         tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
3335
3336         /* Wait for a reasonable time for the link to come up */
3337         tg3_poll_link(tp);
3338         for(i = 0; !tp->carrier_ok && (i < VALID_LINK_TIMEOUT*100); i++) {
3339                 mdelay(1);
3340                 tg3_poll_link(tp);
3341         }
3342         if (!tp->carrier_ok){
3343                 printf("Valid link not established\n");
3344                 goto err_out_disable;
3345         }
3346
3347         nic->nic_op     = &tg3_operations;
3348         return 1;
3349
3350  err_out_iounmap:
3351         iounmap((void *)tp->regs);
3352         return 0;
3353  err_out_disable:
3354         tg3_disable(nic);
3355         return 0;
3356 }
3357
3358
3359 static struct pci_device_id tg3_nics[] = {
3360 PCI_ROM(0x14e4, 0x1644, "tg3-5700",        "Broadcom Tigon 3 5700"),
3361 PCI_ROM(0x14e4, 0x1645, "tg3-5701",        "Broadcom Tigon 3 5701"),
3362 PCI_ROM(0x14e4, 0x1646, "tg3-5702",        "Broadcom Tigon 3 5702"),
3363 PCI_ROM(0x14e4, 0x1647, "tg3-5703",        "Broadcom Tigon 3 5703"),
3364 PCI_ROM(0x14e4, 0x1648, "tg3-5704",        "Broadcom Tigon 3 5704"),
3365 PCI_ROM(0x14e4, 0x164d, "tg3-5702FE",      "Broadcom Tigon 3 5702FE"),
3366 PCI_ROM(0x14e4, 0x1653, "tg3-5705",        "Broadcom Tigon 3 5705"),
3367 PCI_ROM(0x14e4, 0x1654, "tg3-5705_2",      "Broadcom Tigon 3 5705_2"),
3368 PCI_ROM(0x14e4, 0x165d, "tg3-5705M",       "Broadcom Tigon 3 5705M"),
3369 PCI_ROM(0x14e4, 0x165e, "tg3-5705M_2",     "Broadcom Tigon 3 5705M_2"),
3370 PCI_ROM(0x14e4, 0x1677, "tg3-5751",        "Broadcom Tigon 3 5751"),
3371 PCI_ROM(0x14e4, 0x1696, "tg3-5782",        "Broadcom Tigon 3 5782"),
3372 PCI_ROM(0x14e4, 0x169c, "tg3-5788",        "Broadcom Tigon 3 5788"),
3373 PCI_ROM(0x14e4, 0x16a6, "tg3-5702X",       "Broadcom Tigon 3 5702X"),
3374 PCI_ROM(0x14e4, 0x16a7, "tg3-5703X",       "Broadcom Tigon 3 5703X"),
3375 PCI_ROM(0x14e4, 0x16a8, "tg3-5704S",       "Broadcom Tigon 3 5704S"),
3376 PCI_ROM(0x14e4, 0x16c6, "tg3-5702A3",      "Broadcom Tigon 3 5702A3"),
3377 PCI_ROM(0x14e4, 0x16c7, "tg3-5703A3",      "Broadcom Tigon 3 5703A3"),
3378 PCI_ROM(0x14e4, 0x170d, "tg3-5901",        "Broadcom Tigon 3 5901"),
3379 PCI_ROM(0x14e4, 0x170e, "tg3-5901_2",      "Broadcom Tigon 3 5901_2"),
3380 PCI_ROM(0x1148, 0x4400, "tg3-9DXX",        "Syskonnect 9DXX"),
3381 PCI_ROM(0x1148, 0x4500, "tg3-9MXX",        "Syskonnect 9MXX"),
3382 PCI_ROM(0x173b, 0x03e8, "tg3-ac1000",      "Altima AC1000"),
3383 PCI_ROM(0x173b, 0x03e9, "tg3-ac1001",      "Altima AC1001"),
3384 PCI_ROM(0x173b, 0x03ea, "tg3-ac9100",      "Altima AC9100"),
3385 PCI_ROM(0x173b, 0x03eb, "tg3-ac1003",      "Altima AC1003"),
3386 };
3387
3388 PCI_DRIVER ( tg3_driver, tg3_nics, PCI_NO_CLASS );
3389
3390 DRIVER ( "TG3", nic_driver, pci_driver, tg3_driver,
3391          tg3_probe, tg3_disable );