[tg3] Add support for tg3-5721
[people/dverkamp/gpxe.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 <errno.h>
18 #include <gpxe/pci.h>
19 #include <gpxe/ethernet.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                 (tp->pci_chip_rev_id != CHIPREV_ID_5721)) {
1884                 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
1885                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
1886                         tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
1887                 else
1888                         tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
1889                 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
1890                 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
1891         }
1892         if (!(tp->tg3_flags & TG3_FLAG_JUMBO_ENABLE)) {
1893                 tw32(BUFMGR_MB_RDMA_LOW_WATER,
1894                      tp->bufmgr_config.mbuf_read_dma_low_water);
1895                 tw32(BUFMGR_MB_MACRX_LOW_WATER,
1896                      tp->bufmgr_config.mbuf_mac_rx_low_water);
1897                 tw32(BUFMGR_MB_HIGH_WATER,
1898                      tp->bufmgr_config.mbuf_high_water);
1899         } else {
1900                 tw32(BUFMGR_MB_RDMA_LOW_WATER,
1901                      tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
1902                 tw32(BUFMGR_MB_MACRX_LOW_WATER,
1903                      tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
1904                 tw32(BUFMGR_MB_HIGH_WATER,
1905                      tp->bufmgr_config.mbuf_high_water_jumbo);
1906         }
1907         tw32(BUFMGR_DMA_LOW_WATER,
1908              tp->bufmgr_config.dma_low_water);
1909         tw32(BUFMGR_DMA_HIGH_WATER,
1910              tp->bufmgr_config.dma_high_water);
1911
1912         tw32(BUFMGR_MODE, BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE);
1913         for (i = 0; i < 2000; i++) {
1914                 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
1915                         break;
1916                 udelay(10);
1917         }
1918         if (i >= 2000) {
1919                 printf("tg3_setup_hw cannot enable BUFMGR\n");
1920                 return -ENODEV;
1921         }
1922
1923         tw32(FTQ_RESET, 0xffffffff);
1924         tw32(FTQ_RESET, 0x00000000);
1925         for (i = 0; i < 2000; i++) {
1926                 if (tr32(FTQ_RESET) == 0x00000000)
1927                         break;
1928                 udelay(10);
1929         }
1930         if (i >= 2000) {
1931                 printf("tg3_setup_hw cannot reset FTQ\n");
1932                 return -ENODEV;
1933         }
1934
1935         /* Initialize TG3_BDINFO's at:
1936          *  RCVDBDI_STD_BD:     standard eth size rx ring
1937          *  RCVDBDI_JUMBO_BD:   jumbo frame rx ring
1938          *  RCVDBDI_MINI_BD:    small frame rx ring (??? does not work)
1939          *
1940          * like so:
1941          *  TG3_BDINFO_HOST_ADDR:       high/low parts of DMA address of ring
1942          *  TG3_BDINFO_MAXLEN_FLAGS:    (rx max buffer size << 16) |
1943          *                              ring attribute flags
1944          *  TG3_BDINFO_NIC_ADDR:        location of descriptors in nic SRAM
1945          *
1946          * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
1947          * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
1948          *
1949          * ??? No space allocated for mini receive ring? :(
1950          *
1951          * The size of each ring is fixed in the firmware, but the location is
1952          * configurable.
1953          */
1954         {
1955                 static const uint32_t table_all[] = {
1956                         /* Setup replenish thresholds. */
1957                         RCVBDI_STD_THRESH, TG3_DEF_RX_RING_PENDING / 8,
1958
1959                         /* Etherboot lives below 4GB */
1960                         RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH, 0,
1961                         RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR, NIC_SRAM_RX_BUFFER_DESC,
1962                 };
1963                 static const uint32_t table_not_5705[] = {
1964                         /* Buffer maximum length */
1965                         RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, RX_STD_MAX_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT,
1966                         
1967                         /* Disable the mini frame rx ring */
1968                         RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,      BDINFO_FLAGS_DISABLED,
1969                         
1970                         /* Disable the jumbo frame rx ring */
1971                         RCVBDI_JUMBO_THRESH, 0,
1972                         RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS, BDINFO_FLAGS_DISABLED,
1973                         
1974                         
1975                 };
1976                 TG3_WRITE_SETTINGS(table_all);
1977                 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW, 
1978                         virt_to_bus(tp->rx_std));
1979                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
1980                         tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS,
1981                                 RX_STD_MAX_SIZE_5705 << BDINFO_FLAGS_MAXLEN_SHIFT);
1982                 } else {
1983                         TG3_WRITE_SETTINGS(table_not_5705);
1984                 }
1985         }
1986
1987         
1988         /* There is only one send ring on 5705, no need to explicitly
1989          * disable the others.
1990          */
1991         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
1992                 /* Clear out send RCB ring in SRAM. */
1993                 for (i = NIC_SRAM_SEND_RCB; i < NIC_SRAM_RCV_RET_RCB; i += TG3_BDINFO_SIZE)
1994                         tg3_write_mem(i + TG3_BDINFO_MAXLEN_FLAGS, BDINFO_FLAGS_DISABLED);
1995         }
1996
1997         tp->tx_prod = 0;
1998         tw32_mailbox(MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
1999         tw32_mailbox2(MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW, 0);
2000
2001         tg3_set_bdinfo(tp,
2002                 NIC_SRAM_SEND_RCB,
2003                 virt_to_bus(tp->tx_ring),
2004                 (TG3_TX_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
2005                 NIC_SRAM_TX_BUFFER_DESC);
2006
2007         /* There is only one receive return ring on 5705, no need to explicitly
2008          * disable the others.
2009          */
2010         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
2011                 for (i = NIC_SRAM_RCV_RET_RCB; i < NIC_SRAM_STATS_BLK; i += TG3_BDINFO_SIZE) {
2012                         tg3_write_mem(i + TG3_BDINFO_MAXLEN_FLAGS,
2013                                 BDINFO_FLAGS_DISABLED);
2014                 }
2015         }
2016
2017         tp->rx_rcb_ptr = 0;
2018         tw32_mailbox2(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, 0);
2019
2020         tg3_set_bdinfo(tp,
2021                 NIC_SRAM_RCV_RET_RCB,
2022                 virt_to_bus(tp->rx_rcb),
2023                 (TG3_RX_RCB_RING_SIZE << BDINFO_FLAGS_MAXLEN_SHIFT),
2024                 0);
2025
2026         tp->rx_std_ptr = TG3_DEF_RX_RING_PENDING;
2027         tw32_mailbox2(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW,
2028                      tp->rx_std_ptr);
2029
2030         tw32_mailbox2(MAILBOX_RCV_JUMBO_PROD_IDX + TG3_64BIT_REG_LOW, 0);
2031
2032         /* Initialize MAC address and backoff seed. */
2033         __tg3_set_mac_addr(tp);
2034
2035         /* Calculate RDMAC_MODE setting early, we need it to determine
2036          * the RCVLPC_STATE_ENABLE mask.
2037          */
2038         rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
2039                 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
2040                 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
2041                 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
2042                 RDMAC_MODE_LNGREAD_ENAB);
2043         if (tp->tg3_flags & TG3_FLAG_SPLIT_MODE)
2044                 rdmac_mode |= RDMAC_MODE_SPLIT_ENABLE;
2045         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2046                 if (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
2047                         if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
2048                                 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
2049                                 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
2050                         }
2051                 }
2052         }
2053
2054         /* Setup host coalescing engine. */
2055         tw32(HOSTCC_MODE, 0);
2056         for (i = 0; i < 2000; i++) {
2057                 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
2058                         break;
2059                 udelay(10);
2060         }
2061
2062         tp->mac_mode = MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
2063                 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
2064         tw32_carefully(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
2065
2066         tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
2067         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
2068                 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
2069                                        GRC_LCLCTRL_GPIO_OUTPUT1);
2070         tw32_carefully(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
2071
2072         tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0);
2073         tr32(MAILBOX_INTERRUPT_0);
2074
2075         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
2076                 tw32_carefully(DMAC_MODE, DMAC_MODE_ENABLE);
2077         }
2078
2079         val = ( WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
2080                 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
2081                 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
2082                 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
2083                 WDMAC_MODE_LNGREAD_ENAB);
2084         if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) &&
2085                 ((tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) != 0) &&
2086                 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
2087                 val |= WDMAC_MODE_RX_ACCEL;
2088         }
2089         tw32_carefully(WDMAC_MODE, val);
2090
2091         if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
2092                 val = tr32(TG3PCI_X_CAPS);
2093                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
2094                         val &= PCIX_CAPS_BURST_MASK;
2095                         val |= (PCIX_CAPS_MAX_BURST_CPIOB << PCIX_CAPS_BURST_SHIFT);
2096                 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2097                         val &= ~(PCIX_CAPS_SPLIT_MASK | PCIX_CAPS_BURST_MASK);
2098                         val |= (PCIX_CAPS_MAX_BURST_CPIOB << PCIX_CAPS_BURST_SHIFT);
2099                         if (tp->tg3_flags & TG3_FLAG_SPLIT_MODE)
2100                                 val |= (tp->split_mode_max_reqs <<
2101                                         PCIX_CAPS_SPLIT_SHIFT);
2102                 }
2103                 tw32(TG3PCI_X_CAPS, val);
2104         }
2105
2106         tw32_carefully(RDMAC_MODE, rdmac_mode);
2107         {
2108                 static const uint32_t table_all[] = {
2109                         /* MTU + ethernet header + FCS + optional VLAN tag */
2110                         MAC_RX_MTU_SIZE, ETH_MAX_MTU + ETH_HLEN + 8,
2111                         
2112                         /* The slot time is changed by tg3_setup_phy if we
2113                          * run at gigabit with half duplex.
2114                          */
2115                         MAC_TX_LENGTHS, 
2116                         (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
2117                         (6 << TX_LENGTHS_IPG_SHIFT) |
2118                         (32 << TX_LENGTHS_SLOT_TIME_SHIFT),
2119                         
2120                         /* Receive rules. */
2121                         MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS,
2122                         RCVLPC_CONFIG, 0x0181,
2123                         
2124                         /* Receive/send statistics. */
2125                         RCVLPC_STATS_ENABLE, 0xffffff,
2126                         RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE,
2127                         SNDDATAI_STATSENAB, 0xffffff,
2128                         SNDDATAI_STATSCTRL, (SNDDATAI_SCTRL_ENABLE |SNDDATAI_SCTRL_FASTUPD),
2129                         
2130                         /* Host coalescing engine */
2131                         HOSTCC_RXCOL_TICKS, 0,
2132                         HOSTCC_TXCOL_TICKS, LOW_TXCOL_TICKS,
2133                         HOSTCC_RXMAX_FRAMES, 1,
2134                         HOSTCC_TXMAX_FRAMES, LOW_RXMAX_FRAMES,
2135                         HOSTCC_RXCOAL_MAXF_INT, 1,
2136                         HOSTCC_TXCOAL_MAXF_INT, 0,
2137                         
2138                         /* Status/statistics block address. */
2139                         /* Etherboot lives below 4GB, so HIGH == 0 */
2140                         HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH, 0,
2141
2142                         /* No need to enable 32byte coalesce mode. */
2143                         HOSTCC_MODE, HOSTCC_MODE_ENABLE | 0,
2144                         
2145                         RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE,
2146                         RCVLPC_MODE, RCVLPC_MODE_ENABLE,
2147                         
2148                         RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE,
2149
2150                         SNDDATAC_MODE, SNDDATAC_MODE_ENABLE,
2151                         SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE,
2152                         RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB,
2153                         RCVDBDI_MODE, RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ,
2154                         SNDDATAI_MODE, SNDDATAI_MODE_ENABLE,
2155                         SNDBDI_MODE, SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE,
2156                         SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE,
2157                         
2158                         /* Accept all multicast frames. */
2159                         MAC_HASH_REG_0, 0xffffffff,
2160                         MAC_HASH_REG_1, 0xffffffff,
2161                         MAC_HASH_REG_2, 0xffffffff,
2162                         MAC_HASH_REG_3, 0xffffffff,
2163                 };
2164                 static const uint32_t table_not_5705[] = {
2165                         /* Host coalescing engine */
2166                         HOSTCC_RXCOAL_TICK_INT, 0,
2167                         HOSTCC_TXCOAL_TICK_INT, 0,
2168
2169                         /* Status/statistics block address. */
2170                         /* Etherboot lives below 4GB, so HIGH == 0 */
2171                         HOSTCC_STAT_COAL_TICKS, DEFAULT_STAT_COAL_TICKS,
2172                         HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH, 0,
2173                         HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK,
2174                         HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK,
2175
2176                         RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE,
2177
2178                         MBFREE_MODE, MBFREE_MODE_ENABLE,
2179                 };
2180                 TG3_WRITE_SETTINGS(table_all);
2181                 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
2182                         virt_to_bus(tp->hw_stats));
2183                 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
2184                         virt_to_bus(tp->hw_status));
2185                 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
2186                         TG3_WRITE_SETTINGS(table_not_5705);
2187                 }
2188         }
2189
2190         tp->tx_mode = TX_MODE_ENABLE;
2191         tw32_carefully(MAC_TX_MODE, tp->tx_mode);
2192
2193         tp->rx_mode = RX_MODE_ENABLE;
2194         tw32_carefully(MAC_RX_MODE, tp->rx_mode);
2195
2196         tp->mi_mode = MAC_MI_MODE_BASE;
2197         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
2198
2199         tw32(MAC_LED_CTRL, 0);
2200         tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
2201         if (tp->phy_id == PHY_ID_SERDES) {
2202                 tw32_carefully(MAC_RX_MODE, RX_MODE_RESET);
2203         }
2204         tp->rx_mode |= RX_MODE_KEEP_VLAN_TAG; /* drop tagged vlan packets */
2205         tw32_carefully(MAC_RX_MODE, tp->rx_mode);
2206
2207         if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
2208                 tw32(MAC_SERDES_CFG, 0x616000);
2209
2210         /* Prevent chip from dropping frames when flow control
2211          * is enabled.
2212          */
2213         tw32(MAC_LOW_WMARK_MAX_RX_FRAME, 2);
2214         tr32(MAC_LOW_WMARK_MAX_RX_FRAME);
2215
2216         err = tg3_setup_phy(tp);
2217
2218         /* Ignore CRC stats */
2219
2220         /* Initialize receive rules. */
2221         tw32(MAC_RCV_RULE_0,  0xc2000000 & RCV_RULE_DISABLE_MASK);
2222         tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
2223         tw32(MAC_RCV_RULE_1,  0x86000004 & RCV_RULE_DISABLE_MASK);
2224         tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
2225
2226         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
2227             || (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750))
2228                 limit = 8;
2229         else
2230                 limit = 16;
2231         if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
2232                 limit -= 4;
2233         switch (limit) {
2234         case 16:        tw32(MAC_RCV_RULE_15,  0); tw32(MAC_RCV_VALUE_15,  0);
2235         case 15:        tw32(MAC_RCV_RULE_14,  0); tw32(MAC_RCV_VALUE_14,  0);
2236         case 14:        tw32(MAC_RCV_RULE_13,  0); tw32(MAC_RCV_VALUE_13,  0);
2237         case 13:        tw32(MAC_RCV_RULE_12,  0); tw32(MAC_RCV_VALUE_12,  0);
2238         case 12:        tw32(MAC_RCV_RULE_11,  0); tw32(MAC_RCV_VALUE_11,  0);
2239         case 11:        tw32(MAC_RCV_RULE_10,  0); tw32(MAC_RCV_VALUE_10,  0);
2240         case 10:        tw32(MAC_RCV_RULE_9,  0);  tw32(MAC_RCV_VALUE_9,  0);
2241         case 9:         tw32(MAC_RCV_RULE_8,  0);  tw32(MAC_RCV_VALUE_8,  0);
2242         case 8:         tw32(MAC_RCV_RULE_7,  0);  tw32(MAC_RCV_VALUE_7,  0);
2243         case 7:         tw32(MAC_RCV_RULE_6,  0);  tw32(MAC_RCV_VALUE_6,  0);
2244         case 6:         tw32(MAC_RCV_RULE_5,  0);  tw32(MAC_RCV_VALUE_5,  0);
2245         case 5:         tw32(MAC_RCV_RULE_4,  0);  tw32(MAC_RCV_VALUE_4,  0);
2246         case 4:         /* tw32(MAC_RCV_RULE_3,  0); tw32(MAC_RCV_VALUE_3,  0); */
2247         case 3:         /* tw32(MAC_RCV_RULE_2,  0); tw32(MAC_RCV_VALUE_2,  0); */
2248         case 2:
2249         case 1:
2250         default:
2251                 break;
2252         };
2253
2254         return err;
2255 }
2256
2257
2258
2259 /* Chips other than 5700/5701 use the NVRAM for fetching info. */
2260 static void tg3_nvram_init(struct tg3 *tp)
2261 {
2262         tw32(GRC_EEPROM_ADDR,
2263              (EEPROM_ADDR_FSM_RESET |
2264               (EEPROM_DEFAULT_CLOCK_PERIOD <<
2265                EEPROM_ADDR_CLKPERD_SHIFT)));
2266
2267         mdelay(1);
2268
2269         /* Enable seeprom accesses. */
2270         tw32_carefully(GRC_LOCAL_CTRL,
2271                 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
2272
2273         if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2274             GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
2275                 uint32_t nvcfg1 = tr32(NVRAM_CFG1);
2276
2277                 tp->tg3_flags |= TG3_FLAG_NVRAM;
2278                 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
2279                         if (nvcfg1 & NVRAM_CFG1_BUFFERED_MODE)
2280                                 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
2281                 } else {
2282                         nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
2283                         tw32(NVRAM_CFG1, nvcfg1);
2284                 }
2285
2286         } else {
2287                 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
2288         }
2289 }
2290
2291
2292 static int tg3_nvram_read_using_eeprom(
2293         struct tg3 *tp __unused, uint32_t offset, uint32_t *val)
2294 {
2295         uint32_t tmp;
2296         int i;
2297
2298         if (offset > EEPROM_ADDR_ADDR_MASK ||
2299                 (offset % 4) != 0) {
2300                 return -EINVAL;
2301         }
2302
2303         tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2304                                         EEPROM_ADDR_DEVID_MASK |
2305                                         EEPROM_ADDR_READ);
2306         tw32(GRC_EEPROM_ADDR,
2307              tmp |
2308              (0 << EEPROM_ADDR_DEVID_SHIFT) |
2309              ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2310               EEPROM_ADDR_ADDR_MASK) |
2311              EEPROM_ADDR_READ | EEPROM_ADDR_START);
2312
2313         for (i = 0; i < 10000; i++) {
2314                 tmp = tr32(GRC_EEPROM_ADDR);
2315
2316                 if (tmp & EEPROM_ADDR_COMPLETE)
2317                         break;
2318                 udelay(100);
2319         }
2320         if (!(tmp & EEPROM_ADDR_COMPLETE)) {
2321                 return -EBUSY;
2322         }
2323
2324         *val = tr32(GRC_EEPROM_DATA);
2325         return 0;
2326 }
2327
2328 static int tg3_nvram_read(struct tg3 *tp, uint32_t offset, uint32_t *val)
2329 {
2330         int i, saw_done_clear;
2331
2332         if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2333                 return tg3_nvram_read_using_eeprom(tp, offset, val);
2334
2335         if (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED)
2336                 offset = ((offset / NVRAM_BUFFERED_PAGE_SIZE) <<
2337                           NVRAM_BUFFERED_PAGE_POS) +
2338                         (offset % NVRAM_BUFFERED_PAGE_SIZE);
2339
2340         if (offset > NVRAM_ADDR_MSK)
2341                 return -EINVAL;
2342
2343         tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2344         for (i = 0; i < 1000; i++) {
2345                 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2346                         break;
2347                 udelay(20);
2348         }
2349
2350         tw32(NVRAM_ADDR, offset);
2351         tw32(NVRAM_CMD,
2352              NVRAM_CMD_RD | NVRAM_CMD_GO |
2353              NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2354
2355         /* Wait for done bit to clear then set again. */
2356         saw_done_clear = 0;
2357         for (i = 0; i < 1000; i++) {
2358                 udelay(10);
2359                 if (!saw_done_clear &&
2360                     !(tr32(NVRAM_CMD) & NVRAM_CMD_DONE))
2361                         saw_done_clear = 1;
2362                 else if (saw_done_clear &&
2363                          (tr32(NVRAM_CMD) & NVRAM_CMD_DONE))
2364                         break;
2365         }
2366         if (i >= 1000) {
2367                 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2368                 return -EBUSY;
2369         }
2370
2371         *val = bswap_32(tr32(NVRAM_RDDATA));
2372         tw32(NVRAM_SWARB, 0x20);
2373
2374         return 0;
2375 }
2376
2377 struct subsys_tbl_ent {
2378         uint16_t subsys_vendor, subsys_devid;
2379         uint32_t phy_id;
2380 };
2381
2382 static struct subsys_tbl_ent subsys_id_to_phy_id[] = {
2383         /* Broadcom boards. */
2384         { 0x14e4, 0x1644, PHY_ID_BCM5401 }, /* BCM95700A6 */
2385         { 0x14e4, 0x0001, PHY_ID_BCM5701 }, /* BCM95701A5 */
2386         { 0x14e4, 0x0002, PHY_ID_BCM8002 }, /* BCM95700T6 */
2387         { 0x14e4, 0x0003, PHY_ID_SERDES  }, /* BCM95700A9 */
2388         { 0x14e4, 0x0005, PHY_ID_BCM5701 }, /* BCM95701T1 */
2389         { 0x14e4, 0x0006, PHY_ID_BCM5701 }, /* BCM95701T8 */
2390         { 0x14e4, 0x0007, PHY_ID_SERDES  }, /* BCM95701A7 */
2391         { 0x14e4, 0x0008, PHY_ID_BCM5701 }, /* BCM95701A10 */
2392         { 0x14e4, 0x8008, PHY_ID_BCM5701 }, /* BCM95701A12 */
2393         { 0x14e4, 0x0009, PHY_ID_BCM5701 }, /* BCM95703Ax1 */
2394         { 0x14e4, 0x8009, PHY_ID_BCM5701 }, /* BCM95703Ax2 */
2395
2396         /* 3com boards. */
2397         { PCI_VENDOR_ID_3COM, 0x1000, PHY_ID_BCM5401 }, /* 3C996T */
2398         { PCI_VENDOR_ID_3COM, 0x1006, PHY_ID_BCM5701 }, /* 3C996BT */
2399         /* { PCI_VENDOR_ID_3COM, 0x1002, PHY_ID_XXX },     3C996CT */
2400         /* { PCI_VENDOR_ID_3COM, 0x1003, PHY_ID_XXX },     3C997T */
2401         { PCI_VENDOR_ID_3COM, 0x1004, PHY_ID_SERDES  }, /* 3C996SX */
2402         /* { PCI_VENDOR_ID_3COM, 0x1005, PHY_ID_XXX },     3C997SZ */
2403         { PCI_VENDOR_ID_3COM, 0x1007, PHY_ID_BCM5701 }, /* 3C1000T */
2404         { PCI_VENDOR_ID_3COM, 0x1008, PHY_ID_BCM5701 }, /* 3C940BR01 */
2405
2406         /* DELL boards. */
2407         { PCI_VENDOR_ID_DELL, 0x00d1, PHY_ID_BCM5401 }, /* VIPER */
2408         { PCI_VENDOR_ID_DELL, 0x0106, PHY_ID_BCM5401 }, /* JAGUAR */
2409         { PCI_VENDOR_ID_DELL, 0x0109, PHY_ID_BCM5411 }, /* MERLOT */
2410         { PCI_VENDOR_ID_DELL, 0x010a, PHY_ID_BCM5411 }, /* SLIM_MERLOT */
2411         { PCI_VENDOR_ID_DELL, 0x0179, PHY_ID_BCM5751 }, /* EtherXpress */
2412         
2413         /* Fujitsu Siemens Computer */
2414         { PCI_VENDOR_ID_FSC, 0x105d, PHY_ID_BCM5751 }, /* Futro C200 */ 
2415
2416         /* Compaq boards. */
2417         { PCI_VENDOR_ID_COMPAQ, 0x007c, PHY_ID_BCM5701 }, /* BANSHEE */
2418         { PCI_VENDOR_ID_COMPAQ, 0x009a, PHY_ID_BCM5701 }, /* BANSHEE_2 */
2419         { PCI_VENDOR_ID_COMPAQ, 0x007d, PHY_ID_SERDES  }, /* CHANGELING */
2420         { PCI_VENDOR_ID_COMPAQ, 0x0085, PHY_ID_BCM5701 }, /* NC7780 */
2421         { PCI_VENDOR_ID_COMPAQ, 0x0099, PHY_ID_BCM5701 }  /* NC7780_2 */
2422 };
2423
2424 static int tg3_phy_probe(struct tg3 *tp)
2425 {
2426         uint32_t eeprom_phy_id, hw_phy_id_1, hw_phy_id_2;
2427         uint32_t hw_phy_id, hw_phy_id_masked;
2428         enum phy_led_mode eeprom_led_mode;
2429         uint32_t val;
2430         unsigned i;
2431         int eeprom_signature_found, err;
2432
2433         tp->phy_id = PHY_ID_INVALID;
2434
2435         for (i = 0; i < sizeof(subsys_id_to_phy_id)/sizeof(subsys_id_to_phy_id[0]); i++) {
2436                 if ((subsys_id_to_phy_id[i].subsys_vendor == tp->subsystem_vendor) &&
2437                         (subsys_id_to_phy_id[i].subsys_devid == tp->subsystem_device)) {
2438                         tp->phy_id = subsys_id_to_phy_id[i].phy_id;
2439                         break;
2440                 }
2441         }
2442
2443         eeprom_phy_id = PHY_ID_INVALID;
2444         eeprom_led_mode = led_mode_auto;
2445         eeprom_signature_found = 0;
2446         tg3_read_mem(NIC_SRAM_DATA_SIG, &val);
2447         if (val == NIC_SRAM_DATA_SIG_MAGIC) {
2448                 uint32_t nic_cfg;
2449
2450                 tg3_read_mem(NIC_SRAM_DATA_CFG, &nic_cfg);
2451                 tp->nic_sram_data_cfg = nic_cfg;
2452
2453                 eeprom_signature_found = 1;
2454
2455                 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
2456                     NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER) {
2457                         eeprom_phy_id = PHY_ID_SERDES;
2458                 } else {
2459                         uint32_t nic_phy_id;
2460
2461                         tg3_read_mem(NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
2462                         if (nic_phy_id != 0) {
2463                                 uint32_t id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
2464                                 uint32_t id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
2465
2466                                 eeprom_phy_id  = (id1 >> 16) << 10;
2467                                 eeprom_phy_id |= (id2 & 0xfc00) << 16;
2468                                 eeprom_phy_id |= (id2 & 0x03ff) <<  0;
2469                         }
2470                 }
2471
2472                 switch (nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK) {
2473                 case NIC_SRAM_DATA_CFG_LED_TRIPLE_SPD:
2474                         eeprom_led_mode = led_mode_three_link;
2475                         break;
2476
2477                 case NIC_SRAM_DATA_CFG_LED_LINK_SPD:
2478                         eeprom_led_mode = led_mode_link10;
2479                         break;
2480
2481                 default:
2482                         eeprom_led_mode = led_mode_auto;
2483                         break;
2484                 };
2485                 if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
2486                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
2487                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)) &&
2488                         (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP)) {
2489                         tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
2490                 }
2491
2492                 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE)
2493                         tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
2494                 if (nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL)
2495                         tp->tg3_flags |= TG3_FLAG_SERDES_WOL_CAP;
2496         }
2497
2498         /* Now read the physical PHY_ID from the chip and verify
2499          * that it is sane.  If it doesn't look good, we fall back
2500          * to either the hard-coded table based PHY_ID and failing
2501          * that the value found in the eeprom area.
2502          */
2503         err  = tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
2504         err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
2505
2506         hw_phy_id  = (hw_phy_id_1 & 0xffff) << 10;
2507         hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
2508         hw_phy_id |= (hw_phy_id_2 & 0x03ff) <<  0;
2509
2510         hw_phy_id_masked = hw_phy_id & PHY_ID_MASK;
2511
2512         if (!err && KNOWN_PHY_ID(hw_phy_id_masked)) {
2513                 tp->phy_id = hw_phy_id;
2514         } else {
2515                 /* phy_id currently holds the value found in the
2516                  * subsys_id_to_phy_id[] table or PHY_ID_INVALID
2517                  * if a match was not found there.
2518                  */
2519                 if (tp->phy_id == PHY_ID_INVALID) {
2520                         if (!eeprom_signature_found ||
2521                             !KNOWN_PHY_ID(eeprom_phy_id & PHY_ID_MASK))
2522                                 return -ENODEV;
2523                         tp->phy_id = eeprom_phy_id;
2524                 }
2525         }
2526
2527         err = tg3_phy_reset(tp);
2528         if (err)
2529                 return err;
2530
2531         if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2532             tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
2533                 uint32_t mii_tg3_ctrl;
2534                 
2535                 /* These chips, when reset, only advertise 10Mb
2536                  * capabilities.  Fix that.
2537                  */
2538                 err  = tg3_writephy(tp, MII_ADVERTISE,
2539                                     (ADVERTISE_CSMA |
2540                                      ADVERTISE_PAUSE_CAP |
2541                                      ADVERTISE_10HALF |
2542                                      ADVERTISE_10FULL |
2543                                      ADVERTISE_100HALF |
2544                                      ADVERTISE_100FULL));
2545                 mii_tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
2546                                 MII_TG3_CTRL_ADV_1000_FULL |
2547                                 MII_TG3_CTRL_AS_MASTER |
2548                                 MII_TG3_CTRL_ENABLE_AS_MASTER);
2549                 if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2550                         mii_tg3_ctrl = 0;
2551
2552                 err |= tg3_writephy(tp, MII_TG3_CTRL, mii_tg3_ctrl);
2553                 err |= tg3_writephy(tp, MII_BMCR,
2554                                     (BMCR_ANRESTART | BMCR_ANENABLE));
2555         }
2556
2557         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
2558                 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2559                 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x201f);
2560                 tg3_writedsp(tp, MII_TG3_DSP_RW_PORT, 0x2aaa);
2561         }
2562
2563         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2564                 tg3_writephy(tp, 0x1c, 0x8d68);
2565                 tg3_writephy(tp, 0x1c, 0x8d68);
2566         }
2567
2568         /* Enable Ethernet@WireSpeed */
2569         tg3_phy_set_wirespeed(tp);
2570
2571         if (!err && ((tp->phy_id & PHY_ID_MASK) == PHY_ID_BCM5401)) {
2572                 err = tg3_init_5401phy_dsp(tp);
2573         }
2574
2575         /* Determine the PHY led mode. 
2576          * Be careful if this gets set wrong it can result in an inability to 
2577          * establish a link.
2578          */
2579         if (tp->phy_id == PHY_ID_SERDES) {
2580                 tp->led_mode = led_mode_three_link;
2581         }
2582         else if (tp->subsystem_vendor == PCI_VENDOR_ID_DELL) {
2583                 tp->led_mode = led_mode_link10;
2584         } else {
2585                 tp->led_mode = led_mode_three_link;
2586                 if (eeprom_signature_found &&
2587                     eeprom_led_mode != led_mode_auto)
2588                         tp->led_mode = eeprom_led_mode;
2589         }
2590
2591         if (tp->phy_id == PHY_ID_SERDES)
2592                 tp->link_config.advertising =
2593                         (ADVERTISED_1000baseT_Half |
2594                          ADVERTISED_1000baseT_Full |
2595                          ADVERTISED_Autoneg |
2596                          ADVERTISED_FIBRE);
2597         if (tp->tg3_flags & TG3_FLAG_10_100_ONLY)
2598                 tp->link_config.advertising &=
2599                         ~(ADVERTISED_1000baseT_Half |
2600                           ADVERTISED_1000baseT_Full);
2601
2602         return err;
2603 }
2604
2605 #if SUPPORT_PARTNO_STR
2606 static void tg3_read_partno(struct tg3 *tp)
2607 {
2608         unsigned char vpd_data[256];
2609         int i;
2610
2611         for (i = 0; i < 256; i += 4) {
2612                 uint32_t tmp;
2613
2614                 if (tg3_nvram_read(tp, 0x100 + i, &tmp))
2615                         goto out_not_found;
2616
2617                 vpd_data[i + 0] = ((tmp >>  0) & 0xff);
2618                 vpd_data[i + 1] = ((tmp >>  8) & 0xff);
2619                 vpd_data[i + 2] = ((tmp >> 16) & 0xff);
2620                 vpd_data[i + 3] = ((tmp >> 24) & 0xff);
2621         }
2622
2623         /* Now parse and find the part number. */
2624         for (i = 0; i < 256; ) {
2625                 unsigned char val = vpd_data[i];
2626                 int block_end;
2627
2628                 if (val == 0x82 || val == 0x91) {
2629                         i = (i + 3 +
2630                              (vpd_data[i + 1] +
2631                               (vpd_data[i + 2] << 8)));
2632                         continue;
2633                 }
2634
2635                 if (val != 0x90)
2636                         goto out_not_found;
2637
2638                 block_end = (i + 3 +
2639                              (vpd_data[i + 1] +
2640                               (vpd_data[i + 2] << 8)));
2641                 i += 3;
2642                 while (i < block_end) {
2643                         if (vpd_data[i + 0] == 'P' &&
2644                             vpd_data[i + 1] == 'N') {
2645                                 int partno_len = vpd_data[i + 2];
2646
2647                                 if (partno_len > 24)
2648                                         goto out_not_found;
2649
2650                                 memcpy(tp->board_part_number,
2651                                        &vpd_data[i + 3],
2652                                        partno_len);
2653
2654                                 /* Success. */
2655                                 return;
2656                         }
2657                 }
2658
2659                 /* Part number not found. */
2660                 goto out_not_found;
2661         }
2662
2663 out_not_found:
2664         memcpy(tp->board_part_number, "none", sizeof("none"));
2665 }
2666 #else
2667 #define tg3_read_partno(TP) ((TP)->board_part_number[0] = '\0')
2668 #endif
2669
2670 static int tg3_get_invariants(struct tg3 *tp)
2671 {
2672         uint32_t misc_ctrl_reg;
2673         uint32_t pci_state_reg, grc_misc_cfg;
2674         uint16_t pci_cmd;
2675         uint8_t  pci_latency;
2676         uint32_t val ;
2677         int err;
2678
2679         /* Read the subsystem vendor and device ids */
2680         pci_read_config_word(tp->pdev, PCI_SUBSYSTEM_VENDOR_ID, &tp->subsystem_vendor);
2681         pci_read_config_word(tp->pdev, PCI_SUBSYSTEM_ID, &tp->subsystem_device);
2682
2683         /* The sun_5704 code needs infrastructure etherboot does have
2684          * ignore it for now.
2685          */
2686
2687         /* If we have an AMD 762 or Intel ICH/ICH0 chipset, write
2688          * reordering to the mailbox registers done by the host
2689          * controller can cause major troubles.  We read back from
2690          * every mailbox register write to force the writes to be
2691          * posted to the chip in order.
2692          *
2693          * TG3_FLAG_MBOX_WRITE_REORDER has been forced on.
2694          */
2695
2696         /* Force memory write invalidate off.  If we leave it on,
2697          * then on 5700_BX chips we have to enable a workaround.
2698          * The workaround is to set the TG3PCI_DMA_RW_CTRL boundry
2699          * to match the cacheline size.  The Broadcom driver have this
2700          * workaround but turns MWI off all the times so never uses
2701          * it.  This seems to suggest that the workaround is insufficient.
2702          */
2703         pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
2704         pci_cmd &= ~PCI_COMMAND_INVALIDATE;
2705         /* Also, force SERR#/PERR# in PCI command. */
2706         pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
2707         pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
2708
2709         /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
2710          * has the register indirect write enable bit set before
2711          * we try to access any of the MMIO registers.  It is also
2712          * critical that the PCI-X hw workaround situation is decided
2713          * before that as well.
2714          */
2715         pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL, &misc_ctrl_reg);
2716
2717         tp->pci_chip_rev_id = (misc_ctrl_reg >> MISC_HOST_CTRL_CHIPREV_SHIFT);
2718
2719         /* Initialize misc host control in PCI block. */
2720         tp->misc_host_ctrl |= (misc_ctrl_reg &
2721                                MISC_HOST_CTRL_CHIPREV);
2722         pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
2723                                tp->misc_host_ctrl);
2724
2725         pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER, &pci_latency);
2726         if (pci_latency < 64) {
2727                 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER, 64);
2728         }
2729
2730         pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE, &pci_state_reg);
2731
2732         /* If this is a 5700 BX chipset, and we are in PCI-X
2733          * mode, enable register write workaround.
2734          *
2735          * The workaround is to use indirect register accesses
2736          * for all chip writes not to mailbox registers.
2737          *
2738          * In etherboot to simplify things we just always use this work around.
2739          */
2740         if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0) {
2741                 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
2742         }
2743         /* Back to back register writes can cause problems on the 5701,
2744          * the workaround is to read back all reg writes except those to
2745          * mailbox regs.
2746          * In etherboot we always use indirect register accesses so
2747          * we don't see this.
2748          */
2749
2750         if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
2751                 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
2752         if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
2753                 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
2754
2755         /* Chip-specific fixup from Broadcom driver */
2756         if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
2757             (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
2758                 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
2759                 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
2760         }
2761
2762         /* determine if it is PCIE system */
2763         // Alf : I have no idea what this is about...
2764         // But it's definitely usefull
2765         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) {
2766           val = tr32(TG3PCI_MSI_CAP_ID) ;
2767           if (((val >> 8) & 0xff) == T3_PCIE_CAPABILITY_ID_REG) {
2768             val = tr32(T3_PCIE_CAPABILITY_ID_REG) ;
2769             if ((val & 0xff) == T3_PCIE_CAPABILITY_ID) {
2770               tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS ;
2771             }
2772           }
2773         }
2774
2775         /* Force the chip into D0. */
2776         tg3_set_power_state_0(tp);
2777
2778         /* Etherboot does not ask the tg3 to do checksums */
2779         /* Etherboot does not ask the tg3 to do jumbo frames */
2780         /* Ehterboot does not ask the tg3 to use WakeOnLan. */
2781
2782         /* A few boards don't want Ethernet@WireSpeed phy feature */
2783         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
2784             (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
2785                 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
2786                         (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
2787                         (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1))) {
2788                 tp->tg3_flags2 |= TG3_FLG2_NO_ETH_WIRE_SPEED;
2789         }
2790
2791         /* Avoid tagged irq status etherboot does not use irqs */
2792
2793         /* Only 5701 and later support tagged irq status mode.
2794          * Also, 5788 chips cannot use tagged irq status.
2795          *
2796          * However, since etherboot does not use irqs avoid tagged irqs
2797          * status  because the interrupt condition is more difficult to
2798          * fully clear in that mode.
2799          */
2800         
2801         /* Since some 5700_AX && 5700_BX have problems with 32BYTE
2802          * coalesce_mode, and the rest work fine anything set.
2803          * Don't enable HOST_CC_MODE_32BYTE in etherboot.
2804          */
2805
2806         /* Initialize MAC MI mode, polling disabled. */
2807         tw32_carefully(MAC_MI_MODE, tp->mi_mode);
2808
2809         /* Initialize data/descriptor byte/word swapping. */
2810         tw32(GRC_MODE, tp->grc_mode);
2811
2812         tg3_switch_clocks(tp);
2813
2814         /* Clear this out for sanity. */
2815         tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
2816
2817         /* Etherboot does not need to check if the PCIX_TARGET_HWBUG
2818          * is needed.  It always uses it.
2819          */
2820         
2821         udelay(50);
2822         tg3_nvram_init(tp);
2823
2824         /* The TX descriptors will reside in main memory.
2825          */
2826
2827         /* See which board we are using.
2828          */
2829         grc_misc_cfg = tr32(GRC_MISC_CFG);
2830         grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
2831
2832         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
2833             grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5704CIOBE) {
2834                 tp->tg3_flags |= TG3_FLAG_SPLIT_MODE;
2835                 tp->split_mode_max_reqs = SPLIT_MODE_5704_MAX_REQ;
2836         }
2837
2838         if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
2839             (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
2840              grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
2841                 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
2842
2843 #define PCI_DEVICE_ID_TIGON3_5901       0x170d
2844 #define PCI_DEVICE_ID_TIGON3_5901_2     0x170e
2845
2846         /* these are limited to 10/100 only */
2847         if (((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) &&
2848                     ((grc_misc_cfg == 0x8000) || (grc_misc_cfg == 0x4000))) ||
2849                 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
2850                         (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM) &&
2851                         ((tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901) ||
2852                                 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2)))) {
2853                 tp->tg3_flags |= TG3_FLAG_10_100_ONLY;
2854         }
2855
2856         err = tg3_phy_probe(tp);
2857         if (err) {
2858                 printf("phy probe failed, err %d\n", err);
2859         }
2860
2861         tg3_read_partno(tp);
2862
2863
2864         /* 5700 BX chips need to have their TX producer index mailboxes
2865          * written twice to workaround a bug.
2866          * In etherboot we do this unconditionally to simplify things.
2867          */
2868
2869         /* 5700 chips can get confused if TX buffers straddle the
2870          * 4GB address boundary in some cases.
2871          * 
2872          * In etherboot we can ignore the problem as etherboot lives below 4GB.
2873          */
2874
2875         /* In etherboot wake-on-lan is unconditionally disabled */
2876         return err;
2877 }
2878
2879 static int  tg3_get_device_address(struct tg3 *tp)
2880 {
2881         struct nic *nic = tp->nic;
2882         uint32_t hi, lo, mac_offset;
2883
2884         if (PCI_FUNC(tp->pdev->devfn) == 0)
2885                 mac_offset = 0x7c;
2886         else
2887                 mac_offset = 0xcc;
2888
2889         /* First try to get it from MAC address mailbox. */
2890         tg3_read_mem(NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
2891         if ((hi >> 16) == 0x484b) {
2892                 nic->node_addr[0] = (hi >>  8) & 0xff;
2893                 nic->node_addr[1] = (hi >>  0) & 0xff;
2894
2895                 tg3_read_mem(NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
2896                 nic->node_addr[2] = (lo >> 24) & 0xff;
2897                 nic->node_addr[3] = (lo >> 16) & 0xff;
2898                 nic->node_addr[4] = (lo >>  8) & 0xff;
2899                 nic->node_addr[5] = (lo >>  0) & 0xff;
2900         }
2901         /* Next, try NVRAM. */
2902         else if (!tg3_nvram_read(tp, mac_offset + 0, &hi) &&
2903                  !tg3_nvram_read(tp, mac_offset + 4, &lo)) {
2904                 nic->node_addr[0] = ((hi >> 16) & 0xff);
2905                 nic->node_addr[1] = ((hi >> 24) & 0xff);
2906                 nic->node_addr[2] = ((lo >>  0) & 0xff);
2907                 nic->node_addr[3] = ((lo >>  8) & 0xff);
2908                 nic->node_addr[4] = ((lo >> 16) & 0xff);
2909                 nic->node_addr[5] = ((lo >> 24) & 0xff);
2910         }
2911         /* Finally just fetch it out of the MAC control regs. */
2912         else {
2913                 hi = tr32(MAC_ADDR_0_HIGH);
2914                 lo = tr32(MAC_ADDR_0_LOW);
2915
2916                 nic->node_addr[5] = lo & 0xff;
2917                 nic->node_addr[4] = (lo >> 8) & 0xff;
2918                 nic->node_addr[3] = (lo >> 16) & 0xff;
2919                 nic->node_addr[2] = (lo >> 24) & 0xff;
2920                 nic->node_addr[1] = hi & 0xff;
2921                 nic->node_addr[0] = (hi >> 8) & 0xff;
2922         }
2923
2924         return 0;
2925 }
2926
2927
2928 static int tg3_setup_dma(struct tg3 *tp)
2929 {
2930         tw32(TG3PCI_CLOCK_CTRL, 0);
2931
2932         if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) == 0) {
2933                 tp->dma_rwctrl =
2934                         (0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
2935                         (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT) |
2936                         (0x7 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
2937                         (0x7 << DMA_RWCTRL_READ_WATER_SHIFT) |
2938                         (0x0f << DMA_RWCTRL_MIN_DMA_SHIFT);
2939                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2940                         tp->dma_rwctrl &= ~(DMA_RWCTRL_MIN_DMA << DMA_RWCTRL_MIN_DMA_SHIFT);
2941                 }
2942         } else {
2943                 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
2944                         tp->dma_rwctrl =
2945                                 (0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
2946                                 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT) |
2947                                 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
2948                                 (0x7 << DMA_RWCTRL_READ_WATER_SHIFT) |
2949                                 (0x00 << DMA_RWCTRL_MIN_DMA_SHIFT);
2950                 else
2951                         tp->dma_rwctrl =
2952                                 (0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
2953                                 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT) |
2954                                 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
2955                                 (0x3 << DMA_RWCTRL_READ_WATER_SHIFT) |
2956                                 (0x0f << DMA_RWCTRL_MIN_DMA_SHIFT);
2957
2958                 /* Wheee, some more chip bugs... */
2959                 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
2960                         (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)) {
2961                         uint32_t ccval = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
2962
2963                         if ((ccval == 0x6) || (ccval == 0x7)) {
2964                                 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
2965                         }
2966                 }
2967         }
2968
2969         if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) ||
2970                 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)) {
2971                 tp->dma_rwctrl &= ~(DMA_RWCTRL_MIN_DMA << DMA_RWCTRL_MIN_DMA_SHIFT);
2972         }
2973
2974         /*
2975           Alf : Tried that, but it does not work. Should be this way though :-(
2976         if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
2977           tp->dma_rwctrl |= 0x001f0000;
2978         }
2979         */
2980         tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
2981
2982         tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
2983
2984         return 0;
2985 }
2986
2987 static void tg3_init_link_config(struct tg3 *tp)
2988 {
2989         tp->link_config.advertising =
2990                 (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2991                  ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2992                  ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full |
2993                  ADVERTISED_Autoneg | ADVERTISED_MII);
2994         tp->carrier_ok = 0;
2995         tp->link_config.active_speed = SPEED_INVALID;
2996         tp->link_config.active_duplex = DUPLEX_INVALID;
2997 }
2998
2999
3000 #if SUPPORT_PHY_STR
3001 static const char * tg3_phy_string(struct tg3 *tp)
3002 {
3003         switch (tp->phy_id & PHY_ID_MASK) {
3004         case PHY_ID_BCM5400:    return "5400";
3005         case PHY_ID_BCM5401:    return "5401";
3006         case PHY_ID_BCM5411:    return "5411";
3007         case PHY_ID_BCM5701:    return "5701";
3008         case PHY_ID_BCM5703:    return "5703";
3009         case PHY_ID_BCM5704:    return "5704";
3010         case PHY_ID_BCM5705:    return "5705";
3011         case PHY_ID_BCM5750:    return "5750";
3012         case PHY_ID_BCM5751:    return "5751"; 
3013         case PHY_ID_BCM8002:    return "8002/serdes";
3014         case PHY_ID_SERDES:     return "serdes";
3015         default:                return "unknown";
3016         };
3017 }
3018 #else
3019 #define tg3_phy_string(TP) "?"
3020 #endif
3021
3022
3023 static void tg3_poll_link(struct tg3 *tp)
3024 {
3025         uint32_t mac_stat;
3026
3027         mac_stat = tr32(MAC_STATUS);
3028         if (tp->phy_id == PHY_ID_SERDES) {
3029                 if (tp->carrier_ok?
3030                         (mac_stat & MAC_STATUS_LNKSTATE_CHANGED):
3031                         (mac_stat & MAC_STATUS_PCS_SYNCED)) {
3032                         tw32_carefully(MAC_MODE, tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK);
3033                         tw32_carefully(MAC_MODE, tp->mac_mode);
3034
3035                         tg3_setup_phy(tp);
3036                 }
3037         }
3038         else {
3039                 if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED) {
3040                         tg3_setup_phy(tp);
3041                 }
3042         }
3043 }
3044
3045 /**************************************************************************
3046 POLL - Wait for a frame
3047 ***************************************************************************/
3048 static void tg3_ack_irqs(struct tg3 *tp)
3049 {
3050         if (tp->hw_status->status & SD_STATUS_UPDATED) {
3051                 /*
3052                  * writing any value to intr-mbox-0 clears PCI INTA# and
3053                  * chip-internal interrupt pending events.
3054                  * writing non-zero to intr-mbox-0 additional tells the
3055                  * NIC to stop sending us irqs, engaging "in-intr-handler"
3056                  * event coalescing.
3057                  */
3058                 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 
3059                         0x00000001);
3060                 /*
3061                  * Flush PCI write.  This also guarantees that our
3062                  * status block has been flushed to host memory.
3063                  */
3064                 tr32(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW);
3065                 tp->hw_status->status &= ~SD_STATUS_UPDATED;
3066         }
3067 }
3068
3069 static int tg3_poll(struct nic *nic, int retrieve)
3070 {
3071         /* return true if there's an ethernet packet ready to read */
3072         /* nic->packet should contain data on return */
3073         /* nic->packetlen should contain length of data */
3074
3075         struct tg3 *tp = &tg3;
3076         int result;
3077
3078         result = 0;
3079
3080         if ( (tp->hw_status->idx[0].rx_producer != tp->rx_rcb_ptr) && !retrieve ) 
3081           return 1;
3082
3083         tg3_ack_irqs(tp);
3084
3085         if (tp->hw_status->idx[0].rx_producer != tp->rx_rcb_ptr) {
3086                 struct tg3_rx_buffer_desc *desc;
3087                 unsigned int len;
3088                 desc = &tp->rx_rcb[tp->rx_rcb_ptr];
3089                 if ((desc->opaque & RXD_OPAQUE_RING_MASK) == RXD_OPAQUE_RING_STD) {
3090                         len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4; /* omit crc */
3091                         
3092                         nic->packetlen = len;
3093                         memcpy(nic->packet, bus_to_virt(desc->addr_lo), len);
3094                         result = 1;
3095                 }
3096                 tp->rx_rcb_ptr = (tp->rx_rcb_ptr + 1) % TG3_RX_RCB_RING_SIZE;
3097                 
3098                 /* ACK the status ring */
3099                 tw32_mailbox2(MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW, tp->rx_rcb_ptr);
3100
3101                 /* Refill RX ring. */
3102                 if (result) {
3103                         tp->rx_std_ptr = (tp->rx_std_ptr + 1) % TG3_RX_RING_SIZE;
3104                         tw32_mailbox2(MAILBOX_RCV_STD_PROD_IDX + TG3_64BIT_REG_LOW, tp->rx_std_ptr);
3105                 }
3106         }
3107         tg3_poll_link(tp);
3108         return result;
3109 }
3110
3111 /**************************************************************************
3112 TRANSMIT - Transmit a frame
3113 ***************************************************************************/
3114 #if 0
3115 static void tg3_set_txd(struct tg3 *tp, int entry,
3116         dma_addr_t mapping, int len, uint32_t flags,
3117         uint32_t mss_and_is_end)
3118 {
3119         struct tg3_tx_buffer_desc *txd =  &tp->tx_ring[entry];
3120         int is_end = (mss_and_is_end & 0x1);
3121         if (is_end) {
3122                 flags |= TXD_FLAG_END;
3123         }
3124
3125         txd->addr_hi   = 0;
3126         txd->addr_lo   = mapping & 0xffffffff;
3127         txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
3128         txd->vlan_tag  = 0 << TXD_VLAN_TAG_SHIFT;
3129 }
3130 #endif
3131
3132 static void tg3_transmit(struct nic *nic, const char *dst_addr,
3133         unsigned int type, unsigned int size, const char *packet)
3134 {
3135         static int frame_idx;
3136         struct eth_frame *frame;
3137         
3138         /* send the packet to destination */
3139         struct tg3_tx_buffer_desc *txd;
3140         struct tg3 *tp;
3141         uint32_t entry;
3142         int i;
3143
3144         /* Wait until there is a free packet frame */
3145         tp = &tg3;
3146         i = 0;
3147         entry = tp->tx_prod;
3148         while((tp->hw_status->idx[0].tx_consumer != entry) &&
3149                 (tp->hw_status->idx[0].tx_consumer != PREV_TX(entry))) {
3150                 mdelay(10);     /* give the nick a chance */
3151                 if (++i > 500) { /* timeout 5s for transmit */
3152                         printf("transmit timed out\n");
3153                         tg3_halt(tp);
3154                         tg3_setup_hw(tp);
3155                         return;
3156                 }
3157         }
3158         if (i != 0) {
3159                 printf("#");
3160         }
3161         
3162         /* Copy the packet to the our local buffer */
3163         frame = &tg3_bss.tx_frame[frame_idx];
3164         memcpy(frame->dst_addr, dst_addr, ETH_ALEN);
3165         memcpy(frame->src_addr, nic->node_addr, ETH_ALEN);
3166         frame->type = htons(type);
3167         memset(frame->data, 0, sizeof(frame->data));
3168         memcpy(frame->data, packet, size);
3169
3170         /* Setup the ring buffer entry to transmit */
3171         txd            = &tp->tx_ring[entry];
3172         txd->addr_hi   = 0; /* Etherboot runs under 4GB */
3173         txd->addr_lo   = virt_to_bus(frame);
3174         txd->len_flags = ((size + ETH_HLEN) << TXD_LEN_SHIFT) | TXD_FLAG_END;
3175         txd->vlan_tag  = 0 << TXD_VLAN_TAG_SHIFT;
3176
3177         /* Advance to the next entry */
3178         entry = NEXT_TX(entry);
3179         frame_idx ^= 1;
3180
3181         /* Packets are ready, update Tx producer idx local and on card */
3182         tw32_mailbox((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
3183         tw32_mailbox2((MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW), entry);
3184         tp->tx_prod = entry;
3185 }
3186
3187 /**************************************************************************
3188 DISABLE - Turn off ethernet interface
3189 ***************************************************************************/
3190 static void tg3_disable ( struct nic *nic __unused ) {
3191         struct tg3 *tp = &tg3;
3192         /* put the card in its initial state */
3193         /* This function serves 3 purposes.
3194          * This disables DMA and interrupts so we don't receive
3195          *  unexpected packets or interrupts from the card after
3196          *  etherboot has finished. 
3197          * This frees resources so etherboot may use
3198          *  this driver on another interface
3199          * This allows etherboot to reinitialize the interface
3200          *  if something is something goes wrong.
3201          */
3202         tg3_halt(tp);
3203         tp->tg3_flags &= ~(TG3_FLAG_INIT_COMPLETE|TG3_FLAG_GOT_SERDES_FLOWCTL);
3204         tp->carrier_ok = 0;
3205         iounmap((void *)tp->regs);
3206 }
3207
3208 /**************************************************************************
3209 IRQ - Enable, Disable, or Force interrupts
3210 ***************************************************************************/
3211 static void tg3_irq(struct nic *nic __unused, irq_action_t action __unused)
3212 {
3213   switch ( action ) {
3214   case DISABLE :
3215     break;
3216   case ENABLE :
3217     break;
3218   case FORCE :
3219     break;
3220   }
3221 }
3222
3223 static struct nic_operations tg3_operations = {
3224         .connect        = dummy_connect,
3225         .poll           = tg3_poll,
3226         .transmit       = tg3_transmit,
3227         .irq            = tg3_irq,
3228
3229 };
3230
3231 /**************************************************************************
3232 PROBE - Look for an adapter, this routine's visible to the outside
3233 You should omit the last argument struct pci_device * for a non-PCI NIC
3234 ***************************************************************************/
3235 static int tg3_probe ( struct nic *nic, struct pci_device *pdev ) {
3236
3237         struct tg3 *tp = &tg3;
3238         unsigned long tg3reg_base, tg3reg_len;
3239         int i, err, pm_cap;
3240
3241         memset(tp, 0, sizeof(*tp));
3242
3243         adjust_pci_device(pdev);
3244
3245         nic->irqno  = 0;
3246         nic->ioaddr = pdev->ioaddr;
3247
3248         /* Find power-management capability. */
3249         pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
3250         if (pm_cap == 0) {
3251                 printf("Cannot find PowerManagement capability, aborting.\n");
3252                 return 0;
3253         }
3254         tg3reg_base = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
3255         if (tg3reg_base == -1UL) {
3256                 printf("Unuseable bar\n");
3257                 return 0;
3258         }
3259         tg3reg_len  = pci_bar_size(pdev,  PCI_BASE_ADDRESS_0);
3260
3261         tp->pdev       = pdev;
3262         tp->nic        = nic;
3263         tp->pm_cap     = pm_cap;
3264         tp->rx_mode    = 0;
3265         tp->tx_mode    = 0;
3266         tp->mi_mode    = MAC_MI_MODE_BASE;
3267         tp->tg3_flags  = 0 & ~TG3_FLAG_INIT_COMPLETE; 
3268         
3269         /* The word/byte swap controls here control register access byte
3270          * swapping.  DMA data byte swapping is controlled in the GRC_MODE
3271          * setting below.
3272          */
3273         tp->misc_host_ctrl =
3274                 MISC_HOST_CTRL_MASK_PCI_INT |
3275                 MISC_HOST_CTRL_WORD_SWAP |
3276                 MISC_HOST_CTRL_INDIR_ACCESS |
3277                 MISC_HOST_CTRL_PCISTATE_RW;
3278
3279         /* The NONFRM (non-frame) byte/word swap controls take effect
3280          * on descriptor entries, anything which isn't packet data.
3281          *
3282          * The StrongARM chips on the board (one for tx, one for rx)
3283          * are running in big-endian mode.
3284          */
3285         tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
3286                         GRC_MODE_WSWAP_NONFRM_DATA);
3287 #if __BYTE_ORDER == __BIG_ENDIAN
3288         tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
3289 #endif
3290         tp->regs = (unsigned long) ioremap(tg3reg_base, tg3reg_len);
3291         if (tp->regs == 0UL) {
3292                 printf("Cannot map device registers, aborting\n");
3293                 return 0;
3294         }
3295
3296         tg3_init_link_config(tp);
3297
3298         err = tg3_get_invariants(tp);
3299         if (err) {
3300                 printf("Problem fetching invariants of chip, aborting.\n");
3301                 goto err_out_iounmap;
3302         }
3303
3304         err = tg3_get_device_address(tp);
3305         if (err) {
3306                 printf("Could not obtain valid ethernet address, aborting.\n");
3307                 goto err_out_iounmap;
3308         }
3309
3310         DBG ( "Ethernet addr: %s\n", eth_ntoa ( nic->node_addr ) );
3311
3312         tg3_setup_dma(tp);
3313
3314         /* Now that we have fully setup the chip, save away a snapshot
3315          * of the PCI config space.  We need to restore this after
3316          * GRC_MISC_CFG core clock resets and some resume events.
3317          */
3318         pci_save_state(tp->pdev, tp->pci_cfg_state);
3319
3320         printf("Tigon3 [partno(%s) rev %hx PHY(%s)] (PCI%s:%s:%s)\n",
3321                 tp->board_part_number,
3322                 tp->pci_chip_rev_id,
3323                 tg3_phy_string(tp),
3324                 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ? "X" : ""),
3325                 ((tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED) ?
3326                         ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ? "133MHz" : "66MHz") :
3327                         ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ? "100MHz" : "33MHz")),
3328                 ((tp->tg3_flags & TG3_FLAG_PCI_32BIT) ? "32-bit" : "64-bit"));
3329
3330
3331         err = tg3_setup_hw(tp); 
3332         if (err) {
3333                 goto err_out_disable;
3334         } 
3335         tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
3336
3337         /* Wait for a reasonable time for the link to come up */
3338         tg3_poll_link(tp);
3339         for(i = 0; !tp->carrier_ok && (i < VALID_LINK_TIMEOUT*100); i++) {
3340                 mdelay(1);
3341                 tg3_poll_link(tp);
3342         }
3343         if (!tp->carrier_ok){
3344                 printf("Valid link not established\n");
3345                 goto err_out_disable;
3346         }
3347
3348         nic->nic_op     = &tg3_operations;
3349         return 1;
3350
3351  err_out_iounmap:
3352         iounmap((void *)tp->regs);
3353         return 0;
3354  err_out_disable:
3355         tg3_disable(nic);
3356         return 0;
3357 }
3358
3359
3360 static struct pci_device_id tg3_nics[] = {
3361 PCI_ROM(0x14e4, 0x1644, "tg3-5700",        "Broadcom Tigon 3 5700"),
3362 PCI_ROM(0x14e4, 0x1645, "tg3-5701",        "Broadcom Tigon 3 5701"),
3363 PCI_ROM(0x14e4, 0x1646, "tg3-5702",        "Broadcom Tigon 3 5702"),
3364 PCI_ROM(0x14e4, 0x1647, "tg3-5703",        "Broadcom Tigon 3 5703"),
3365 PCI_ROM(0x14e4, 0x1648, "tg3-5704",        "Broadcom Tigon 3 5704"),
3366 PCI_ROM(0x14e4, 0x164d, "tg3-5702FE",      "Broadcom Tigon 3 5702FE"),
3367 PCI_ROM(0x14e4, 0x1653, "tg3-5705",        "Broadcom Tigon 3 5705"),
3368 PCI_ROM(0x14e4, 0x1654, "tg3-5705_2",      "Broadcom Tigon 3 5705_2"),
3369 PCI_ROM(0x14e4, 0x1659, "tg3-5721",        "Broadcom Tigon 3 5721"),
3370 PCI_ROM(0x14e4, 0x165d, "tg3-5705M",       "Broadcom Tigon 3 5705M"),
3371 PCI_ROM(0x14e4, 0x165e, "tg3-5705M_2",     "Broadcom Tigon 3 5705M_2"),
3372 PCI_ROM(0x14e4, 0x1677, "tg3-5751",        "Broadcom Tigon 3 5751"),
3373 PCI_ROM(0x14e4, 0x1696, "tg3-5782",        "Broadcom Tigon 3 5782"),
3374 PCI_ROM(0x14e4, 0x169c, "tg3-5788",        "Broadcom Tigon 3 5788"),
3375 PCI_ROM(0x14e4, 0x16a6, "tg3-5702X",       "Broadcom Tigon 3 5702X"),
3376 PCI_ROM(0x14e4, 0x16a7, "tg3-5703X",       "Broadcom Tigon 3 5703X"),
3377 PCI_ROM(0x14e4, 0x16a8, "tg3-5704S",       "Broadcom Tigon 3 5704S"),
3378 PCI_ROM(0x14e4, 0x16c6, "tg3-5702A3",      "Broadcom Tigon 3 5702A3"),
3379 PCI_ROM(0x14e4, 0x16c7, "tg3-5703A3",      "Broadcom Tigon 3 5703A3"),
3380 PCI_ROM(0x14e4, 0x170d, "tg3-5901",        "Broadcom Tigon 3 5901"),
3381 PCI_ROM(0x14e4, 0x170e, "tg3-5901_2",      "Broadcom Tigon 3 5901_2"),
3382 PCI_ROM(0x1148, 0x4400, "tg3-9DXX",        "Syskonnect 9DXX"),
3383 PCI_ROM(0x1148, 0x4500, "tg3-9MXX",        "Syskonnect 9MXX"),
3384 PCI_ROM(0x173b, 0x03e8, "tg3-ac1000",      "Altima AC1000"),
3385 PCI_ROM(0x173b, 0x03e9, "tg3-ac1001",      "Altima AC1001"),
3386 PCI_ROM(0x173b, 0x03ea, "tg3-ac9100",      "Altima AC9100"),
3387 PCI_ROM(0x173b, 0x03eb, "tg3-ac1003",      "Altima AC1003"),
3388 PCI_ROM(0x0e11, 0x00ca, "tg3-hp",          "HP Tigon 3"),
3389 };
3390
3391 PCI_DRIVER ( tg3_driver, tg3_nics, PCI_NO_CLASS );
3392
3393 DRIVER ( "TG3", nic_driver, pci_driver, tg3_driver,
3394          tg3_probe, tg3_disable );
3395
3396 /*
3397  * Local variables:
3398  *  c-basic-offset: 8
3399  *  c-indent-level: 8
3400  *  tab-width: 8
3401  * End:
3402  */