1 // SPDX-License-Identifier: GPL-2.0
2 /* sunhme.c: Sparc HME/BigMac 10/100baseT half/full duplex auto switching,
3 * auto carrier detecting ethernet driver. Also known as the
4 * "Happy Meal Ethernet" found on SunSwift SBUS cards.
6 * Copyright (C) 1996, 1998, 1999, 2002, 2003,
10 * 2000/11/11 Willy Tarreau <willy AT meta-x.org>
11 * - port to non-sparc architectures. Tested only on x86 and
12 * only currently works with QFE PCI cards.
13 * - ability to specify the MAC address at module load time by passing this
14 * argument : macaddr=0x00,0x10,0x20,0x30,0x40,0x50
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/fcntl.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioport.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/ethtool.h>
29 #include <linux/mii.h>
30 #include <linux/crc32.h>
31 #include <linux/random.h>
32 #include <linux/errno.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
37 #include <linux/bitops.h>
38 #include <linux/dma-mapping.h>
42 #include <asm/byteorder.h>
46 #include <linux/of_device.h>
47 #include <asm/idprom.h>
48 #include <asm/openprom.h>
49 #include <asm/oplib.h>
51 #include <asm/auxio.h>
53 #include <linux/uaccess.h>
58 #include <linux/pci.h>
63 #define DRV_NAME "sunhme"
64 #define DRV_VERSION "3.10"
65 #define DRV_RELDATE "August 26, 2008"
68 static char version[] =
69 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
71 MODULE_VERSION(DRV_VERSION);
72 MODULE_AUTHOR(DRV_AUTHOR);
73 MODULE_DESCRIPTION("Sun HappyMealEthernet(HME) 10/100baseT ethernet driver");
74 MODULE_LICENSE("GPL");
76 static int macaddr[6];
78 /* accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
79 module_param_array(macaddr, int, NULL, 0);
80 MODULE_PARM_DESC(macaddr, "Happy Meal MAC address to set");
83 static struct quattro *qfe_sbus_list;
87 static struct quattro *qfe_pci_list;
97 struct hme_tx_logent {
101 #define TXLOG_ACTION_IRQ 0x01
102 #define TXLOG_ACTION_TXMIT 0x02
103 #define TXLOG_ACTION_TBUSY 0x04
104 #define TXLOG_ACTION_NBUFS 0x08
107 #define TX_LOG_LEN 128
108 static struct hme_tx_logent tx_log[TX_LOG_LEN];
109 static int txlog_cur_entry;
110 static __inline__ void tx_add_log(struct happy_meal *hp, unsigned int a, unsigned int s)
112 struct hme_tx_logent *tlp;
115 local_irq_save(flags);
116 tlp = &tx_log[txlog_cur_entry];
117 tlp->tstamp = (unsigned int)jiffies;
118 tlp->tx_new = hp->tx_new;
119 tlp->tx_old = hp->tx_old;
122 txlog_cur_entry = (txlog_cur_entry + 1) & (TX_LOG_LEN - 1);
123 local_irq_restore(flags);
125 static __inline__ void tx_dump_log(void)
129 this = txlog_cur_entry;
130 for (i = 0; i < TX_LOG_LEN; i++) {
131 printk("TXLOG[%d]: j[%08x] tx[N(%d)O(%d)] action[%08x] stat[%08x]\n", i,
133 tx_log[this].tx_new, tx_log[this].tx_old,
134 tx_log[this].action, tx_log[this].status);
135 this = (this + 1) & (TX_LOG_LEN - 1);
138 static __inline__ void tx_dump_ring(struct happy_meal *hp)
140 struct hmeal_init_block *hb = hp->happy_block;
141 struct happy_meal_txd *tp = &hb->happy_meal_txd[0];
144 for (i = 0; i < TX_RING_SIZE; i+=4) {
145 printk("TXD[%d..%d]: [%08x:%08x] [%08x:%08x] [%08x:%08x] [%08x:%08x]\n",
147 le32_to_cpu(tp[i].tx_flags), le32_to_cpu(tp[i].tx_addr),
148 le32_to_cpu(tp[i + 1].tx_flags), le32_to_cpu(tp[i + 1].tx_addr),
149 le32_to_cpu(tp[i + 2].tx_flags), le32_to_cpu(tp[i + 2].tx_addr),
150 le32_to_cpu(tp[i + 3].tx_flags), le32_to_cpu(tp[i + 3].tx_addr));
154 #define tx_add_log(hp, a, s) do { } while(0)
155 #define tx_dump_log() do { } while(0)
156 #define tx_dump_ring(hp) do { } while(0)
160 #define HMD(x) printk x
165 /* #define AUTO_SWITCH_DEBUG */
167 #ifdef AUTO_SWITCH_DEBUG
168 #define ASD(x) printk x
173 #define DEFAULT_IPG0 16 /* For lance-mode only */
174 #define DEFAULT_IPG1 8 /* For all modes */
175 #define DEFAULT_IPG2 4 /* For all modes */
176 #define DEFAULT_JAMSIZE 4 /* Toe jam */
178 /* NOTE: In the descriptor writes one _must_ write the address
179 * member _first_. The card must not be allowed to see
180 * the updated descriptor flags until the address is
181 * correct. I've added a write memory barrier between
182 * the two stores so that I can sleep well at night... -DaveM
185 #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
186 static void sbus_hme_write32(void __iomem *reg, u32 val)
188 sbus_writel(val, reg);
191 static u32 sbus_hme_read32(void __iomem *reg)
193 return sbus_readl(reg);
196 static void sbus_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
198 rxd->rx_addr = (__force hme32)addr;
200 rxd->rx_flags = (__force hme32)flags;
203 static void sbus_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
205 txd->tx_addr = (__force hme32)addr;
207 txd->tx_flags = (__force hme32)flags;
210 static u32 sbus_hme_read_desc32(hme32 *p)
212 return (__force u32)*p;
215 static void pci_hme_write32(void __iomem *reg, u32 val)
220 static u32 pci_hme_read32(void __iomem *reg)
225 static void pci_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
227 rxd->rx_addr = (__force hme32)cpu_to_le32(addr);
229 rxd->rx_flags = (__force hme32)cpu_to_le32(flags);
232 static void pci_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
234 txd->tx_addr = (__force hme32)cpu_to_le32(addr);
236 txd->tx_flags = (__force hme32)cpu_to_le32(flags);
239 static u32 pci_hme_read_desc32(hme32 *p)
241 return le32_to_cpup((__le32 *)p);
244 #define hme_write32(__hp, __reg, __val) \
245 ((__hp)->write32((__reg), (__val)))
246 #define hme_read32(__hp, __reg) \
247 ((__hp)->read32(__reg))
248 #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
249 ((__hp)->write_rxd((__rxd), (__flags), (__addr)))
250 #define hme_write_txd(__hp, __txd, __flags, __addr) \
251 ((__hp)->write_txd((__txd), (__flags), (__addr)))
252 #define hme_read_desc32(__hp, __p) \
253 ((__hp)->read_desc32(__p))
256 /* SBUS only compilation */
257 #define hme_write32(__hp, __reg, __val) \
258 sbus_writel((__val), (__reg))
259 #define hme_read32(__hp, __reg) \
261 #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
262 do { (__rxd)->rx_addr = (__force hme32)(u32)(__addr); \
264 (__rxd)->rx_flags = (__force hme32)(u32)(__flags); \
266 #define hme_write_txd(__hp, __txd, __flags, __addr) \
267 do { (__txd)->tx_addr = (__force hme32)(u32)(__addr); \
269 (__txd)->tx_flags = (__force hme32)(u32)(__flags); \
271 #define hme_read_desc32(__hp, __p) ((__force u32)(hme32)*(__p))
273 /* PCI only compilation */
274 #define hme_write32(__hp, __reg, __val) \
275 writel((__val), (__reg))
276 #define hme_read32(__hp, __reg) \
278 #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
279 do { (__rxd)->rx_addr = (__force hme32)cpu_to_le32(__addr); \
281 (__rxd)->rx_flags = (__force hme32)cpu_to_le32(__flags); \
283 #define hme_write_txd(__hp, __txd, __flags, __addr) \
284 do { (__txd)->tx_addr = (__force hme32)cpu_to_le32(__addr); \
286 (__txd)->tx_flags = (__force hme32)cpu_to_le32(__flags); \
288 static inline u32 hme_read_desc32(struct happy_meal *hp, hme32 *p)
290 return le32_to_cpup((__le32 *)p);
296 /* Oh yes, the MIF BitBang is mighty fun to program. BitBucket is more like it. */
297 static void BB_PUT_BIT(struct happy_meal *hp, void __iomem *tregs, int bit)
299 hme_write32(hp, tregs + TCVR_BBDATA, bit);
300 hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
301 hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
305 static u32 BB_GET_BIT(struct happy_meal *hp, void __iomem *tregs, int internal)
309 hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
310 hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
311 ret = hme_read32(hp, tregs + TCVR_CFG);
313 ret &= TCV_CFG_MDIO0;
315 ret &= TCV_CFG_MDIO1;
321 static u32 BB_GET_BIT2(struct happy_meal *hp, void __iomem *tregs, int internal)
325 hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
327 retval = hme_read32(hp, tregs + TCVR_CFG);
329 retval &= TCV_CFG_MDIO0;
331 retval &= TCV_CFG_MDIO1;
332 hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
337 #define TCVR_FAILURE 0x80000000 /* Impossible MIF read value */
339 static int happy_meal_bb_read(struct happy_meal *hp,
340 void __iomem *tregs, int reg)
346 ASD(("happy_meal_bb_read: reg=%d ", reg));
348 /* Enable the MIF BitBang outputs. */
349 hme_write32(hp, tregs + TCVR_BBOENAB, 1);
351 /* Force BitBang into the idle state. */
352 for (i = 0; i < 32; i++)
353 BB_PUT_BIT(hp, tregs, 1);
355 /* Give it the read sequence. */
356 BB_PUT_BIT(hp, tregs, 0);
357 BB_PUT_BIT(hp, tregs, 1);
358 BB_PUT_BIT(hp, tregs, 1);
359 BB_PUT_BIT(hp, tregs, 0);
361 /* Give it the PHY address. */
362 tmp = hp->paddr & 0xff;
363 for (i = 4; i >= 0; i--)
364 BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
366 /* Tell it what register we want to read. */
368 for (i = 4; i >= 0; i--)
369 BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
371 /* Close down the MIF BitBang outputs. */
372 hme_write32(hp, tregs + TCVR_BBOENAB, 0);
374 /* Now read in the value. */
375 (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
376 for (i = 15; i >= 0; i--)
377 retval |= BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
378 (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
379 (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
380 (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
381 ASD(("value=%x\n", retval));
385 static void happy_meal_bb_write(struct happy_meal *hp,
386 void __iomem *tregs, int reg,
387 unsigned short value)
392 ASD(("happy_meal_bb_write: reg=%d value=%x\n", reg, value));
394 /* Enable the MIF BitBang outputs. */
395 hme_write32(hp, tregs + TCVR_BBOENAB, 1);
397 /* Force BitBang into the idle state. */
398 for (i = 0; i < 32; i++)
399 BB_PUT_BIT(hp, tregs, 1);
401 /* Give it write sequence. */
402 BB_PUT_BIT(hp, tregs, 0);
403 BB_PUT_BIT(hp, tregs, 1);
404 BB_PUT_BIT(hp, tregs, 0);
405 BB_PUT_BIT(hp, tregs, 1);
407 /* Give it the PHY address. */
408 tmp = (hp->paddr & 0xff);
409 for (i = 4; i >= 0; i--)
410 BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
412 /* Tell it what register we will be writing. */
414 for (i = 4; i >= 0; i--)
415 BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
417 /* Tell it to become ready for the bits. */
418 BB_PUT_BIT(hp, tregs, 1);
419 BB_PUT_BIT(hp, tregs, 0);
421 for (i = 15; i >= 0; i--)
422 BB_PUT_BIT(hp, tregs, ((value >> i) & 1));
424 /* Close down the MIF BitBang outputs. */
425 hme_write32(hp, tregs + TCVR_BBOENAB, 0);
428 #define TCVR_READ_TRIES 16
430 static int happy_meal_tcvr_read(struct happy_meal *hp,
431 void __iomem *tregs, int reg)
433 int tries = TCVR_READ_TRIES;
436 ASD(("happy_meal_tcvr_read: reg=0x%02x ", reg));
437 if (hp->tcvr_type == none) {
438 ASD(("no transceiver, value=TCVR_FAILURE\n"));
442 if (!(hp->happy_flags & HFLAG_FENABLE)) {
443 ASD(("doing bit bang\n"));
444 return happy_meal_bb_read(hp, tregs, reg);
447 hme_write32(hp, tregs + TCVR_FRAME,
448 (FRAME_READ | (hp->paddr << 23) | ((reg & 0xff) << 18)));
449 while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
452 printk(KERN_ERR "happy meal: Aieee, transceiver MIF read bolixed\n");
455 retval = hme_read32(hp, tregs + TCVR_FRAME) & 0xffff;
456 ASD(("value=%04x\n", retval));
460 #define TCVR_WRITE_TRIES 16
462 static void happy_meal_tcvr_write(struct happy_meal *hp,
463 void __iomem *tregs, int reg,
464 unsigned short value)
466 int tries = TCVR_WRITE_TRIES;
468 ASD(("happy_meal_tcvr_write: reg=0x%02x value=%04x\n", reg, value));
470 /* Welcome to Sun Microsystems, can I take your order please? */
471 if (!(hp->happy_flags & HFLAG_FENABLE)) {
472 happy_meal_bb_write(hp, tregs, reg, value);
476 /* Would you like fries with that? */
477 hme_write32(hp, tregs + TCVR_FRAME,
478 (FRAME_WRITE | (hp->paddr << 23) |
479 ((reg & 0xff) << 18) | (value & 0xffff)));
480 while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
485 printk(KERN_ERR "happy meal: Aieee, transceiver MIF write bolixed\n");
487 /* Fifty-two cents is your change, have a nice day. */
490 /* Auto negotiation. The scheme is very simple. We have a timer routine
491 * that keeps watching the auto negotiation process as it progresses.
492 * The DP83840 is first told to start doing it's thing, we set up the time
493 * and place the timer state machine in it's initial state.
495 * Here the timer peeks at the DP83840 status registers at each click to see
496 * if the auto negotiation has completed, we assume here that the DP83840 PHY
497 * will time out at some point and just tell us what (didn't) happen. For
498 * complete coverage we only allow so many of the ticks at this level to run,
499 * when this has expired we print a warning message and try another strategy.
500 * This "other" strategy is to force the interface into various speed/duplex
501 * configurations and we stop when we see a link-up condition before the
502 * maximum number of "peek" ticks have occurred.
504 * Once a valid link status has been detected we configure the BigMAC and
505 * the rest of the Happy Meal to speak the most efficient protocol we could
506 * get a clean link for. The priority for link configurations, highest first
508 * 100 Base-T Full Duplex
509 * 100 Base-T Half Duplex
510 * 10 Base-T Full Duplex
511 * 10 Base-T Half Duplex
513 * We start a new timer now, after a successful auto negotiation status has
514 * been detected. This timer just waits for the link-up bit to get set in
515 * the BMCR of the DP83840. When this occurs we print a kernel log message
516 * describing the link type in use and the fact that it is up.
518 * If a fatal error of some sort is signalled and detected in the interrupt
519 * service routine, and the chip is reset, or the link is ifconfig'd down
520 * and then back up, this entire process repeats itself all over again.
522 static int try_next_permutation(struct happy_meal *hp, void __iomem *tregs)
524 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
526 /* Downgrade from full to half duplex. Only possible
529 if (hp->sw_bmcr & BMCR_FULLDPLX) {
530 hp->sw_bmcr &= ~(BMCR_FULLDPLX);
531 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
535 /* Downgrade from 100 to 10. */
536 if (hp->sw_bmcr & BMCR_SPEED100) {
537 hp->sw_bmcr &= ~(BMCR_SPEED100);
538 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
542 /* We've tried everything. */
546 static void display_link_mode(struct happy_meal *hp, void __iomem *tregs)
548 printk(KERN_INFO "%s: Link is up using ", hp->dev->name);
549 if (hp->tcvr_type == external)
553 printk("transceiver at ");
554 hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
555 if (hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) {
556 if (hp->sw_lpa & LPA_100FULL)
557 printk("100Mb/s, Full Duplex.\n");
559 printk("100Mb/s, Half Duplex.\n");
561 if (hp->sw_lpa & LPA_10FULL)
562 printk("10Mb/s, Full Duplex.\n");
564 printk("10Mb/s, Half Duplex.\n");
568 static void display_forced_link_mode(struct happy_meal *hp, void __iomem *tregs)
570 printk(KERN_INFO "%s: Link has been forced up using ", hp->dev->name);
571 if (hp->tcvr_type == external)
575 printk("transceiver at ");
576 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
577 if (hp->sw_bmcr & BMCR_SPEED100)
581 if (hp->sw_bmcr & BMCR_FULLDPLX)
582 printk("Full Duplex.\n");
584 printk("Half Duplex.\n");
587 static int set_happy_link_modes(struct happy_meal *hp, void __iomem *tregs)
591 /* All we care about is making sure the bigmac tx_cfg has a
592 * proper duplex setting.
594 if (hp->timer_state == arbwait) {
595 hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
596 if (!(hp->sw_lpa & (LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL)))
598 if (hp->sw_lpa & LPA_100FULL)
600 else if (hp->sw_lpa & LPA_100HALF)
602 else if (hp->sw_lpa & LPA_10FULL)
607 /* Forcing a link mode. */
608 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
609 if (hp->sw_bmcr & BMCR_FULLDPLX)
615 /* Before changing other bits in the tx_cfg register, and in
616 * general any of other the TX config registers too, you
619 * 2) Poll with reads until that bit reads back as zero
620 * 3) Make TX configuration changes
621 * 4) Set Enable once more
623 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
624 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
625 ~(BIGMAC_TXCFG_ENABLE));
626 while (hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) & BIGMAC_TXCFG_ENABLE)
629 hp->happy_flags |= HFLAG_FULL;
630 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
631 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
632 BIGMAC_TXCFG_FULLDPLX);
634 hp->happy_flags &= ~(HFLAG_FULL);
635 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
636 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
637 ~(BIGMAC_TXCFG_FULLDPLX));
639 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
640 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
641 BIGMAC_TXCFG_ENABLE);
647 static int happy_meal_init(struct happy_meal *hp);
649 static int is_lucent_phy(struct happy_meal *hp)
651 void __iomem *tregs = hp->tcvregs;
652 unsigned short mr2, mr3;
655 mr2 = happy_meal_tcvr_read(hp, tregs, 2);
656 mr3 = happy_meal_tcvr_read(hp, tregs, 3);
657 if ((mr2 & 0xffff) == 0x0180 &&
658 ((mr3 & 0xffff) >> 10) == 0x1d)
664 static void happy_meal_timer(struct timer_list *t)
666 struct happy_meal *hp = from_timer(hp, t, happy_timer);
667 void __iomem *tregs = hp->tcvregs;
668 int restart_timer = 0;
670 spin_lock_irq(&hp->happy_lock);
673 switch(hp->timer_state) {
675 /* Only allow for 5 ticks, thats 10 seconds and much too
676 * long to wait for arbitration to complete.
678 if (hp->timer_ticks >= 10) {
679 /* Enter force mode. */
681 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
682 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful, trying force link mode\n",
684 hp->sw_bmcr = BMCR_SPEED100;
685 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
687 if (!is_lucent_phy(hp)) {
688 /* OK, seems we need do disable the transceiver for the first
689 * tick to make sure we get an accurate link state at the
692 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
693 hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
694 happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG, hp->sw_csconfig);
696 hp->timer_state = ltrywait;
700 /* Anything interesting happen? */
701 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
702 if (hp->sw_bmsr & BMSR_ANEGCOMPLETE) {
705 /* Just what we've been waiting for... */
706 ret = set_happy_link_modes(hp, tregs);
708 /* Ooops, something bad happened, go to force
711 * XXX Broken hubs which don't support 802.3u
712 * XXX auto-negotiation make this happen as well.
717 /* Success, at least so far, advance our state engine. */
718 hp->timer_state = lupwait;
727 /* Auto negotiation was successful and we are awaiting a
728 * link up status. I have decided to let this timer run
729 * forever until some sort of error is signalled, reporting
730 * a message to the user at 10 second intervals.
732 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
733 if (hp->sw_bmsr & BMSR_LSTATUS) {
734 /* Wheee, it's up, display the link mode in use and put
735 * the timer to sleep.
737 display_link_mode(hp, tregs);
738 hp->timer_state = asleep;
741 if (hp->timer_ticks >= 10) {
742 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
743 "not completely up.\n", hp->dev->name);
753 /* Making the timeout here too long can make it take
754 * annoyingly long to attempt all of the link mode
755 * permutations, but then again this is essentially
756 * error recovery code for the most part.
758 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
759 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
760 if (hp->timer_ticks == 1) {
761 if (!is_lucent_phy(hp)) {
762 /* Re-enable transceiver, we'll re-enable the transceiver next
763 * tick, then check link state on the following tick.
765 hp->sw_csconfig |= CSCONFIG_TCVDISAB;
766 happy_meal_tcvr_write(hp, tregs,
767 DP83840_CSCONFIG, hp->sw_csconfig);
772 if (hp->timer_ticks == 2) {
773 if (!is_lucent_phy(hp)) {
774 hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
775 happy_meal_tcvr_write(hp, tregs,
776 DP83840_CSCONFIG, hp->sw_csconfig);
781 if (hp->sw_bmsr & BMSR_LSTATUS) {
782 /* Force mode selection success. */
783 display_forced_link_mode(hp, tregs);
784 set_happy_link_modes(hp, tregs); /* XXX error? then what? */
785 hp->timer_state = asleep;
788 if (hp->timer_ticks >= 4) { /* 6 seconds or so... */
791 ret = try_next_permutation(hp, tregs);
793 /* Aieee, tried them all, reset the
794 * chip and try all over again.
797 /* Let the user know... */
798 printk(KERN_NOTICE "%s: Link down, cable problem?\n",
801 ret = happy_meal_init(hp);
804 printk(KERN_ERR "%s: Error, cannot re-init the "
805 "Happy Meal.\n", hp->dev->name);
809 if (!is_lucent_phy(hp)) {
810 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
812 hp->sw_csconfig |= CSCONFIG_TCVDISAB;
813 happy_meal_tcvr_write(hp, tregs,
814 DP83840_CSCONFIG, hp->sw_csconfig);
826 /* Can't happens.... */
827 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
831 hp->timer_state = asleep; /* foo on you */
836 hp->happy_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
837 add_timer(&hp->happy_timer);
841 spin_unlock_irq(&hp->happy_lock);
844 #define TX_RESET_TRIES 32
845 #define RX_RESET_TRIES 32
847 /* hp->happy_lock must be held */
848 static void happy_meal_tx_reset(struct happy_meal *hp, void __iomem *bregs)
850 int tries = TX_RESET_TRIES;
852 HMD(("happy_meal_tx_reset: reset, "));
854 /* Would you like to try our SMCC Delux? */
855 hme_write32(hp, bregs + BMAC_TXSWRESET, 0);
856 while ((hme_read32(hp, bregs + BMAC_TXSWRESET) & 1) && --tries)
859 /* Lettuce, tomato, buggy hardware (no extra charge)? */
861 printk(KERN_ERR "happy meal: Transceiver BigMac ATTACK!");
867 /* hp->happy_lock must be held */
868 static void happy_meal_rx_reset(struct happy_meal *hp, void __iomem *bregs)
870 int tries = RX_RESET_TRIES;
872 HMD(("happy_meal_rx_reset: reset, "));
874 /* We have a special on GNU/Viking hardware bugs today. */
875 hme_write32(hp, bregs + BMAC_RXSWRESET, 0);
876 while ((hme_read32(hp, bregs + BMAC_RXSWRESET) & 1) && --tries)
879 /* Will that be all? */
881 printk(KERN_ERR "happy meal: Receiver BigMac ATTACK!");
883 /* Don't forget your vik_1137125_wa. Have a nice day. */
887 #define STOP_TRIES 16
889 /* hp->happy_lock must be held */
890 static void happy_meal_stop(struct happy_meal *hp, void __iomem *gregs)
892 int tries = STOP_TRIES;
894 HMD(("happy_meal_stop: reset, "));
896 /* We're consolidating our STB products, it's your lucky day. */
897 hme_write32(hp, gregs + GREG_SWRESET, GREG_RESET_ALL);
898 while (hme_read32(hp, gregs + GREG_SWRESET) && --tries)
901 /* Come back next week when we are "Sun Microelectronics". */
903 printk(KERN_ERR "happy meal: Fry guys.");
905 /* Remember: "Different name, same old buggy as shit hardware." */
909 /* hp->happy_lock must be held */
910 static void happy_meal_get_counters(struct happy_meal *hp, void __iomem *bregs)
912 struct net_device_stats *stats = &hp->dev->stats;
914 stats->rx_crc_errors += hme_read32(hp, bregs + BMAC_RCRCECTR);
915 hme_write32(hp, bregs + BMAC_RCRCECTR, 0);
917 stats->rx_frame_errors += hme_read32(hp, bregs + BMAC_UNALECTR);
918 hme_write32(hp, bregs + BMAC_UNALECTR, 0);
920 stats->rx_length_errors += hme_read32(hp, bregs + BMAC_GLECTR);
921 hme_write32(hp, bregs + BMAC_GLECTR, 0);
923 stats->tx_aborted_errors += hme_read32(hp, bregs + BMAC_EXCTR);
926 (hme_read32(hp, bregs + BMAC_EXCTR) +
927 hme_read32(hp, bregs + BMAC_LTCTR));
928 hme_write32(hp, bregs + BMAC_EXCTR, 0);
929 hme_write32(hp, bregs + BMAC_LTCTR, 0);
932 /* hp->happy_lock must be held */
933 static void happy_meal_poll_stop(struct happy_meal *hp, void __iomem *tregs)
935 ASD(("happy_meal_poll_stop: "));
937 /* If polling disabled or not polling already, nothing to do. */
938 if ((hp->happy_flags & (HFLAG_POLLENABLE | HFLAG_POLL)) !=
939 (HFLAG_POLLENABLE | HFLAG_POLL)) {
940 HMD(("not polling, return\n"));
944 /* Shut up the MIF. */
945 ASD(("were polling, mif ints off, "));
946 hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
948 /* Turn off polling. */
949 ASD(("polling off, "));
950 hme_write32(hp, tregs + TCVR_CFG,
951 hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_PENABLE));
953 /* We are no longer polling. */
954 hp->happy_flags &= ~(HFLAG_POLL);
956 /* Let the bits set. */
961 /* Only Sun can take such nice parts and fuck up the programming interface
962 * like this. Good job guys...
964 #define TCVR_RESET_TRIES 16 /* It should reset quickly */
965 #define TCVR_UNISOLATE_TRIES 32 /* Dis-isolation can take longer. */
967 /* hp->happy_lock must be held */
968 static int happy_meal_tcvr_reset(struct happy_meal *hp, void __iomem *tregs)
971 int result, tries = TCVR_RESET_TRIES;
973 tconfig = hme_read32(hp, tregs + TCVR_CFG);
974 ASD(("happy_meal_tcvr_reset: tcfg<%08lx> ", tconfig));
975 if (hp->tcvr_type == external) {
977 hme_write32(hp, tregs + TCVR_CFG, tconfig & ~(TCV_CFG_PSELECT));
978 hp->tcvr_type = internal;
979 hp->paddr = TCV_PADDR_ITX;
981 happy_meal_tcvr_write(hp, tregs, MII_BMCR,
982 (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
983 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
984 if (result == TCVR_FAILURE) {
985 ASD(("phyread_fail>\n"));
988 ASD(("phyread_ok,PSELECT>"));
989 hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
990 hp->tcvr_type = external;
991 hp->paddr = TCV_PADDR_ETX;
993 if (tconfig & TCV_CFG_MDIO1) {
994 ASD(("internal<PSELECT,"));
995 hme_write32(hp, tregs + TCVR_CFG, (tconfig | TCV_CFG_PSELECT));
997 happy_meal_tcvr_write(hp, tregs, MII_BMCR,
998 (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
999 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1000 if (result == TCVR_FAILURE) {
1001 ASD(("phyread_fail>\n"));
1004 ASD(("phyread_ok,~PSELECT>"));
1005 hme_write32(hp, tregs + TCVR_CFG, (tconfig & ~(TCV_CFG_PSELECT)));
1006 hp->tcvr_type = internal;
1007 hp->paddr = TCV_PADDR_ITX;
1011 ASD(("BMCR_RESET "));
1012 happy_meal_tcvr_write(hp, tregs, MII_BMCR, BMCR_RESET);
1015 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1016 if (result == TCVR_FAILURE)
1018 hp->sw_bmcr = result;
1019 if (!(result & BMCR_RESET))
1024 ASD(("BMCR RESET FAILED!\n"));
1027 ASD(("RESET_OK\n"));
1029 /* Get fresh copies of the PHY registers. */
1030 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1031 hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
1032 hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
1033 hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1036 hp->sw_bmcr &= ~(BMCR_ISOLATE);
1037 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1039 tries = TCVR_UNISOLATE_TRIES;
1041 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1042 if (result == TCVR_FAILURE)
1044 if (!(result & BMCR_ISOLATE))
1049 ASD((" FAILED!\n"));
1052 ASD((" SUCCESS and CSCONFIG_DFBYPASS\n"));
1053 if (!is_lucent_phy(hp)) {
1054 result = happy_meal_tcvr_read(hp, tregs,
1056 happy_meal_tcvr_write(hp, tregs,
1057 DP83840_CSCONFIG, (result | CSCONFIG_DFBYPASS));
1062 /* Figure out whether we have an internal or external transceiver.
1064 * hp->happy_lock must be held
1066 static void happy_meal_transceiver_check(struct happy_meal *hp, void __iomem *tregs)
1068 unsigned long tconfig = hme_read32(hp, tregs + TCVR_CFG);
1070 ASD(("happy_meal_transceiver_check: tcfg=%08lx ", tconfig));
1071 if (hp->happy_flags & HFLAG_POLL) {
1072 /* If we are polling, we must stop to get the transceiver type. */
1073 ASD(("<polling> "));
1074 if (hp->tcvr_type == internal) {
1075 if (tconfig & TCV_CFG_MDIO1) {
1076 ASD(("<internal> <poll stop> "));
1077 happy_meal_poll_stop(hp, tregs);
1078 hp->paddr = TCV_PADDR_ETX;
1079 hp->tcvr_type = external;
1080 ASD(("<external>\n"));
1081 tconfig &= ~(TCV_CFG_PENABLE);
1082 tconfig |= TCV_CFG_PSELECT;
1083 hme_write32(hp, tregs + TCVR_CFG, tconfig);
1086 if (hp->tcvr_type == external) {
1087 ASD(("<external> "));
1088 if (!(hme_read32(hp, tregs + TCVR_STATUS) >> 16)) {
1089 ASD(("<poll stop> "));
1090 happy_meal_poll_stop(hp, tregs);
1091 hp->paddr = TCV_PADDR_ITX;
1092 hp->tcvr_type = internal;
1093 ASD(("<internal>\n"));
1094 hme_write32(hp, tregs + TCVR_CFG,
1095 hme_read32(hp, tregs + TCVR_CFG) &
1096 ~(TCV_CFG_PSELECT));
1104 u32 reread = hme_read32(hp, tregs + TCVR_CFG);
1106 /* Else we can just work off of the MDIO bits. */
1107 ASD(("<not polling> "));
1108 if (reread & TCV_CFG_MDIO1) {
1109 hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
1110 hp->paddr = TCV_PADDR_ETX;
1111 hp->tcvr_type = external;
1112 ASD(("<external>\n"));
1114 if (reread & TCV_CFG_MDIO0) {
1115 hme_write32(hp, tregs + TCVR_CFG,
1116 tconfig & ~(TCV_CFG_PSELECT));
1117 hp->paddr = TCV_PADDR_ITX;
1118 hp->tcvr_type = internal;
1119 ASD(("<internal>\n"));
1121 printk(KERN_ERR "happy meal: Transceiver and a coke please.");
1122 hp->tcvr_type = none; /* Grrr... */
1129 /* The receive ring buffers are a bit tricky to get right. Here goes...
1131 * The buffers we dma into must be 64 byte aligned. So we use a special
1132 * alloc_skb() routine for the happy meal to allocate 64 bytes more than
1135 * We use skb_reserve() to align the data block we get in the skb. We
1136 * also program the etxregs->cfg register to use an offset of 2. This
1137 * imperical constant plus the ethernet header size will always leave
1138 * us with a nicely aligned ip header once we pass things up to the
1141 * The numbers work out to:
1143 * Max ethernet frame size 1518
1144 * Ethernet header size 14
1145 * Happy Meal base offset 2
1147 * Say a skb data area is at 0xf001b010, and its size alloced is
1148 * (ETH_FRAME_LEN + 64 + 2) = (1514 + 64 + 2) = 1580 bytes.
1150 * First our alloc_skb() routine aligns the data base to a 64 byte
1151 * boundary. We now have 0xf001b040 as our skb data address. We
1152 * plug this into the receive descriptor address.
1154 * Next, we skb_reserve() 2 bytes to account for the Happy Meal offset.
1155 * So now the data we will end up looking at starts at 0xf001b042. When
1156 * the packet arrives, we will check out the size received and subtract
1157 * this from the skb->length. Then we just pass the packet up to the
1158 * protocols as is, and allocate a new skb to replace this slot we have
1159 * just received from.
1161 * The ethernet layer will strip the ether header from the front of the
1162 * skb we just sent to it, this leaves us with the ip header sitting
1163 * nicely aligned at 0xf001b050. Also, for tcp and udp packets the
1164 * Happy Meal has even checksummed the tcp/udp data for us. The 16
1165 * bit checksum is obtained from the low bits of the receive descriptor
1168 * skb->csum = rxd->rx_flags & 0xffff;
1169 * skb->ip_summed = CHECKSUM_COMPLETE;
1171 * before sending off the skb to the protocols, and we are good as gold.
1173 static void happy_meal_clean_rings(struct happy_meal *hp)
1177 for (i = 0; i < RX_RING_SIZE; i++) {
1178 if (hp->rx_skbs[i] != NULL) {
1179 struct sk_buff *skb = hp->rx_skbs[i];
1180 struct happy_meal_rxd *rxd;
1183 rxd = &hp->happy_block->happy_meal_rxd[i];
1184 dma_addr = hme_read_desc32(hp, &rxd->rx_addr);
1185 dma_unmap_single(hp->dma_dev, dma_addr,
1186 RX_BUF_ALLOC_SIZE, DMA_FROM_DEVICE);
1187 dev_kfree_skb_any(skb);
1188 hp->rx_skbs[i] = NULL;
1192 for (i = 0; i < TX_RING_SIZE; i++) {
1193 if (hp->tx_skbs[i] != NULL) {
1194 struct sk_buff *skb = hp->tx_skbs[i];
1195 struct happy_meal_txd *txd;
1199 hp->tx_skbs[i] = NULL;
1201 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1202 txd = &hp->happy_block->happy_meal_txd[i];
1203 dma_addr = hme_read_desc32(hp, &txd->tx_addr);
1205 dma_unmap_single(hp->dma_dev, dma_addr,
1206 (hme_read_desc32(hp, &txd->tx_flags)
1210 dma_unmap_page(hp->dma_dev, dma_addr,
1211 (hme_read_desc32(hp, &txd->tx_flags)
1215 if (frag != skb_shinfo(skb)->nr_frags)
1219 dev_kfree_skb_any(skb);
1224 /* hp->happy_lock must be held */
1225 static void happy_meal_init_rings(struct happy_meal *hp)
1227 struct hmeal_init_block *hb = hp->happy_block;
1230 HMD(("happy_meal_init_rings: counters to zero, "));
1231 hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;
1233 /* Free any skippy bufs left around in the rings. */
1235 happy_meal_clean_rings(hp);
1237 /* Now get new skippy bufs for the receive ring. */
1238 HMD(("init rxring, "));
1239 for (i = 0; i < RX_RING_SIZE; i++) {
1240 struct sk_buff *skb;
1243 skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
1245 hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
1248 hp->rx_skbs[i] = skb;
1250 /* Because we reserve afterwards. */
1251 skb_put(skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
1252 mapping = dma_map_single(hp->dma_dev, skb->data, RX_BUF_ALLOC_SIZE,
1254 if (dma_mapping_error(hp->dma_dev, mapping)) {
1255 dev_kfree_skb_any(skb);
1256 hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
1259 hme_write_rxd(hp, &hb->happy_meal_rxd[i],
1260 (RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16)),
1262 skb_reserve(skb, RX_OFFSET);
1265 HMD(("init txring, "));
1266 for (i = 0; i < TX_RING_SIZE; i++)
1267 hme_write_txd(hp, &hb->happy_meal_txd[i], 0, 0);
1272 /* hp->happy_lock must be held */
1274 happy_meal_begin_auto_negotiation(struct happy_meal *hp,
1275 void __iomem *tregs,
1276 const struct ethtool_link_ksettings *ep)
1280 /* Read all of the registers we are interested in now. */
1281 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1282 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1283 hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
1284 hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
1286 /* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */
1288 hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1289 if (!ep || ep->base.autoneg == AUTONEG_ENABLE) {
1290 /* Advertise everything we can support. */
1291 if (hp->sw_bmsr & BMSR_10HALF)
1292 hp->sw_advertise |= (ADVERTISE_10HALF);
1294 hp->sw_advertise &= ~(ADVERTISE_10HALF);
1296 if (hp->sw_bmsr & BMSR_10FULL)
1297 hp->sw_advertise |= (ADVERTISE_10FULL);
1299 hp->sw_advertise &= ~(ADVERTISE_10FULL);
1300 if (hp->sw_bmsr & BMSR_100HALF)
1301 hp->sw_advertise |= (ADVERTISE_100HALF);
1303 hp->sw_advertise &= ~(ADVERTISE_100HALF);
1304 if (hp->sw_bmsr & BMSR_100FULL)
1305 hp->sw_advertise |= (ADVERTISE_100FULL);
1307 hp->sw_advertise &= ~(ADVERTISE_100FULL);
1308 happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
1310 /* XXX Currently no Happy Meal cards I know off support 100BaseT4,
1311 * XXX and this is because the DP83840 does not support it, changes
1312 * XXX would need to be made to the tx/rx logic in the driver as well
1313 * XXX so I completely skip checking for it in the BMSR for now.
1316 #ifdef AUTO_SWITCH_DEBUG
1317 ASD(("%s: Advertising [ ", hp->dev->name));
1318 if (hp->sw_advertise & ADVERTISE_10HALF)
1320 if (hp->sw_advertise & ADVERTISE_10FULL)
1322 if (hp->sw_advertise & ADVERTISE_100HALF)
1324 if (hp->sw_advertise & ADVERTISE_100FULL)
1328 /* Enable Auto-Negotiation, this is usually on already... */
1329 hp->sw_bmcr |= BMCR_ANENABLE;
1330 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1332 /* Restart it to make sure it is going. */
1333 hp->sw_bmcr |= BMCR_ANRESTART;
1334 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1336 /* BMCR_ANRESTART self clears when the process has begun. */
1338 timeout = 64; /* More than enough. */
1340 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1341 if (!(hp->sw_bmcr & BMCR_ANRESTART))
1342 break; /* got it. */
1346 printk(KERN_ERR "%s: Happy Meal would not start auto negotiation "
1347 "BMCR=0x%04x\n", hp->dev->name, hp->sw_bmcr);
1348 printk(KERN_NOTICE "%s: Performing force link detection.\n",
1352 hp->timer_state = arbwait;
1356 /* Force the link up, trying first a particular mode.
1357 * Either we are here at the request of ethtool or
1358 * because the Happy Meal would not start to autoneg.
1361 /* Disable auto-negotiation in BMCR, enable the duplex and
1362 * speed setting, init the timer state machine, and fire it off.
1364 if (!ep || ep->base.autoneg == AUTONEG_ENABLE) {
1365 hp->sw_bmcr = BMCR_SPEED100;
1367 if (ep->base.speed == SPEED_100)
1368 hp->sw_bmcr = BMCR_SPEED100;
1371 if (ep->base.duplex == DUPLEX_FULL)
1372 hp->sw_bmcr |= BMCR_FULLDPLX;
1374 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1376 if (!is_lucent_phy(hp)) {
1377 /* OK, seems we need do disable the transceiver for the first
1378 * tick to make sure we get an accurate link state at the
1381 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
1383 hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
1384 happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
1387 hp->timer_state = ltrywait;
1390 hp->timer_ticks = 0;
1391 hp->happy_timer.expires = jiffies + (12 * HZ)/10; /* 1.2 sec. */
1392 add_timer(&hp->happy_timer);
1395 /* hp->happy_lock must be held */
1396 static int happy_meal_init(struct happy_meal *hp)
1398 const unsigned char *e = &hp->dev->dev_addr[0];
1399 void __iomem *gregs = hp->gregs;
1400 void __iomem *etxregs = hp->etxregs;
1401 void __iomem *erxregs = hp->erxregs;
1402 void __iomem *bregs = hp->bigmacregs;
1403 void __iomem *tregs = hp->tcvregs;
1406 /* If auto-negotiation timer is running, kill it. */
1407 del_timer(&hp->happy_timer);
1409 HMD(("happy_meal_init: happy_flags[%08x] ",
1411 if (!(hp->happy_flags & HFLAG_INIT)) {
1412 HMD(("set HFLAG_INIT, "));
1413 hp->happy_flags |= HFLAG_INIT;
1414 happy_meal_get_counters(hp, bregs);
1418 HMD(("to happy_meal_poll_stop\n"));
1419 happy_meal_poll_stop(hp, tregs);
1421 /* Stop transmitter and receiver. */
1422 HMD(("happy_meal_init: to happy_meal_stop\n"));
1423 happy_meal_stop(hp, gregs);
1425 /* Alloc and reset the tx/rx descriptor chains. */
1426 HMD(("happy_meal_init: to happy_meal_init_rings\n"));
1427 happy_meal_init_rings(hp);
1429 /* Shut up the MIF. */
1430 HMD(("happy_meal_init: Disable all MIF irqs (old[%08x]), ",
1431 hme_read32(hp, tregs + TCVR_IMASK)));
1432 hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
1434 /* See if we can enable the MIF frame on this card to speak to the DP83840. */
1435 if (hp->happy_flags & HFLAG_FENABLE) {
1436 HMD(("use frame old[%08x], ",
1437 hme_read32(hp, tregs + TCVR_CFG)));
1438 hme_write32(hp, tregs + TCVR_CFG,
1439 hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
1441 HMD(("use bitbang old[%08x], ",
1442 hme_read32(hp, tregs + TCVR_CFG)));
1443 hme_write32(hp, tregs + TCVR_CFG,
1444 hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
1447 /* Check the state of the transceiver. */
1448 HMD(("to happy_meal_transceiver_check\n"));
1449 happy_meal_transceiver_check(hp, tregs);
1451 /* Put the Big Mac into a sane state. */
1452 HMD(("happy_meal_init: "));
1453 switch(hp->tcvr_type) {
1455 /* Cannot operate if we don't know the transceiver type! */
1456 HMD(("AAIEEE no transceiver type, EAGAIN"));
1460 /* Using the MII buffers. */
1461 HMD(("internal, using MII, "));
1462 hme_write32(hp, bregs + BMAC_XIFCFG, 0);
1466 /* Not using the MII, disable it. */
1467 HMD(("external, disable MII, "));
1468 hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
1472 if (happy_meal_tcvr_reset(hp, tregs))
1475 /* Reset the Happy Meal Big Mac transceiver and the receiver. */
1476 HMD(("tx/rx reset, "));
1477 happy_meal_tx_reset(hp, bregs);
1478 happy_meal_rx_reset(hp, bregs);
1480 /* Set jam size and inter-packet gaps to reasonable defaults. */
1481 HMD(("jsize/ipg1/ipg2, "));
1482 hme_write32(hp, bregs + BMAC_JSIZE, DEFAULT_JAMSIZE);
1483 hme_write32(hp, bregs + BMAC_IGAP1, DEFAULT_IPG1);
1484 hme_write32(hp, bregs + BMAC_IGAP2, DEFAULT_IPG2);
1486 /* Load up the MAC address and random seed. */
1487 HMD(("rseed/macaddr, "));
1489 /* The docs recommend to use the 10LSB of our MAC here. */
1490 hme_write32(hp, bregs + BMAC_RSEED, ((e[5] | e[4]<<8)&0x3ff));
1492 hme_write32(hp, bregs + BMAC_MACADDR2, ((e[4] << 8) | e[5]));
1493 hme_write32(hp, bregs + BMAC_MACADDR1, ((e[2] << 8) | e[3]));
1494 hme_write32(hp, bregs + BMAC_MACADDR0, ((e[0] << 8) | e[1]));
1497 if ((hp->dev->flags & IFF_ALLMULTI) ||
1498 (netdev_mc_count(hp->dev) > 64)) {
1499 hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
1500 hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
1501 hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
1502 hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
1503 } else if ((hp->dev->flags & IFF_PROMISC) == 0) {
1505 struct netdev_hw_addr *ha;
1508 memset(hash_table, 0, sizeof(hash_table));
1509 netdev_for_each_mc_addr(ha, hp->dev) {
1510 crc = ether_crc_le(6, ha->addr);
1512 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1514 hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
1515 hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
1516 hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
1517 hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
1519 hme_write32(hp, bregs + BMAC_HTABLE3, 0);
1520 hme_write32(hp, bregs + BMAC_HTABLE2, 0);
1521 hme_write32(hp, bregs + BMAC_HTABLE1, 0);
1522 hme_write32(hp, bregs + BMAC_HTABLE0, 0);
1525 /* Set the RX and TX ring ptrs. */
1526 HMD(("ring ptrs rxr[%08x] txr[%08x]\n",
1527 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)),
1528 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0))));
1529 hme_write32(hp, erxregs + ERX_RING,
1530 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)));
1531 hme_write32(hp, etxregs + ETX_RING,
1532 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));
1534 /* Parity issues in the ERX unit of some HME revisions can cause some
1535 * registers to not be written unless their parity is even. Detect such
1536 * lost writes and simply rewrite with a low bit set (which will be ignored
1537 * since the rxring needs to be 2K aligned).
1539 if (hme_read32(hp, erxregs + ERX_RING) !=
1540 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)))
1541 hme_write32(hp, erxregs + ERX_RING,
1542 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0))
1545 /* Set the supported burst sizes. */
1546 HMD(("happy_meal_init: old[%08x] bursts<",
1547 hme_read32(hp, gregs + GREG_CFG)));
1549 #ifndef CONFIG_SPARC
1550 /* It is always PCI and can handle 64byte bursts. */
1551 hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST64);
1553 if ((hp->happy_bursts & DMA_BURST64) &&
1554 ((hp->happy_flags & HFLAG_PCI) != 0
1556 || sbus_can_burst64()
1559 u32 gcfg = GREG_CFG_BURST64;
1561 /* I have no idea if I should set the extended
1562 * transfer mode bit for Cheerio, so for now I
1566 if ((hp->happy_flags & HFLAG_PCI) == 0) {
1567 struct platform_device *op = hp->happy_dev;
1568 if (sbus_can_dma_64bit()) {
1569 sbus_set_sbus64(&op->dev,
1571 gcfg |= GREG_CFG_64BIT;
1577 hme_write32(hp, gregs + GREG_CFG, gcfg);
1578 } else if (hp->happy_bursts & DMA_BURST32) {
1580 hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST32);
1581 } else if (hp->happy_bursts & DMA_BURST16) {
1583 hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST16);
1586 hme_write32(hp, gregs + GREG_CFG, 0);
1588 #endif /* CONFIG_SPARC */
1590 /* Turn off interrupts we do not want to hear. */
1591 HMD((", enable global interrupts, "));
1592 hme_write32(hp, gregs + GREG_IMASK,
1593 (GREG_IMASK_GOTFRAME | GREG_IMASK_RCNTEXP |
1594 GREG_IMASK_SENTFRAME | GREG_IMASK_TXPERR));
1596 /* Set the transmit ring buffer size. */
1597 HMD(("tx rsize=%d oreg[%08x], ", (int)TX_RING_SIZE,
1598 hme_read32(hp, etxregs + ETX_RSIZE)));
1599 hme_write32(hp, etxregs + ETX_RSIZE, (TX_RING_SIZE >> ETX_RSIZE_SHIFT) - 1);
1601 /* Enable transmitter DVMA. */
1602 HMD(("tx dma enable old[%08x], ",
1603 hme_read32(hp, etxregs + ETX_CFG)));
1604 hme_write32(hp, etxregs + ETX_CFG,
1605 hme_read32(hp, etxregs + ETX_CFG) | ETX_CFG_DMAENABLE);
1607 /* This chip really rots, for the receiver sometimes when you
1608 * write to its control registers not all the bits get there
1609 * properly. I cannot think of a sane way to provide complete
1610 * coverage for this hardware bug yet.
1612 HMD(("erx regs bug old[%08x]\n",
1613 hme_read32(hp, erxregs + ERX_CFG)));
1614 hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
1615 regtmp = hme_read32(hp, erxregs + ERX_CFG);
1616 hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
1617 if (hme_read32(hp, erxregs + ERX_CFG) != ERX_CFG_DEFAULT(RX_OFFSET)) {
1618 printk(KERN_ERR "happy meal: Eieee, rx config register gets greasy fries.\n");
1619 printk(KERN_ERR "happy meal: Trying to set %08x, reread gives %08x\n",
1620 ERX_CFG_DEFAULT(RX_OFFSET), regtmp);
1621 /* XXX Should return failure here... */
1624 /* Enable Big Mac hash table filter. */
1625 HMD(("happy_meal_init: enable hash rx_cfg_old[%08x], ",
1626 hme_read32(hp, bregs + BMAC_RXCFG)));
1627 rxcfg = BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_REJME;
1628 if (hp->dev->flags & IFF_PROMISC)
1629 rxcfg |= BIGMAC_RXCFG_PMISC;
1630 hme_write32(hp, bregs + BMAC_RXCFG, rxcfg);
1632 /* Let the bits settle in the chip. */
1635 /* Ok, configure the Big Mac transmitter. */
1636 HMD(("BIGMAC init, "));
1638 if (hp->happy_flags & HFLAG_FULL)
1639 regtmp |= BIGMAC_TXCFG_FULLDPLX;
1641 /* Don't turn on the "don't give up" bit for now. It could cause hme
1642 * to deadlock with the PHY if a Jabber occurs.
1644 hme_write32(hp, bregs + BMAC_TXCFG, regtmp /*| BIGMAC_TXCFG_DGIVEUP*/);
1646 /* Give up after 16 TX attempts. */
1647 hme_write32(hp, bregs + BMAC_ALIMIT, 16);
1649 /* Enable the output drivers no matter what. */
1650 regtmp = BIGMAC_XCFG_ODENABLE;
1652 /* If card can do lance mode, enable it. */
1653 if (hp->happy_flags & HFLAG_LANCE)
1654 regtmp |= (DEFAULT_IPG0 << 5) | BIGMAC_XCFG_LANCE;
1656 /* Disable the MII buffers if using external transceiver. */
1657 if (hp->tcvr_type == external)
1658 regtmp |= BIGMAC_XCFG_MIIDISAB;
1660 HMD(("XIF config old[%08x], ",
1661 hme_read32(hp, bregs + BMAC_XIFCFG)));
1662 hme_write32(hp, bregs + BMAC_XIFCFG, regtmp);
1664 /* Start things up. */
1665 HMD(("tx old[%08x] and rx [%08x] ON!\n",
1666 hme_read32(hp, bregs + BMAC_TXCFG),
1667 hme_read32(hp, bregs + BMAC_RXCFG)));
1669 /* Set larger TX/RX size to allow for 802.1q */
1670 hme_write32(hp, bregs + BMAC_TXMAX, ETH_FRAME_LEN + 8);
1671 hme_write32(hp, bregs + BMAC_RXMAX, ETH_FRAME_LEN + 8);
1673 hme_write32(hp, bregs + BMAC_TXCFG,
1674 hme_read32(hp, bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE);
1675 hme_write32(hp, bregs + BMAC_RXCFG,
1676 hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE);
1678 /* Get the autonegotiation started, and the watch timer ticking. */
1679 happy_meal_begin_auto_negotiation(hp, tregs, NULL);
1685 /* hp->happy_lock must be held */
1686 static void happy_meal_set_initial_advertisement(struct happy_meal *hp)
1688 void __iomem *tregs = hp->tcvregs;
1689 void __iomem *bregs = hp->bigmacregs;
1690 void __iomem *gregs = hp->gregs;
1692 happy_meal_stop(hp, gregs);
1693 hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
1694 if (hp->happy_flags & HFLAG_FENABLE)
1695 hme_write32(hp, tregs + TCVR_CFG,
1696 hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
1698 hme_write32(hp, tregs + TCVR_CFG,
1699 hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
1700 happy_meal_transceiver_check(hp, tregs);
1701 switch(hp->tcvr_type) {
1705 hme_write32(hp, bregs + BMAC_XIFCFG, 0);
1708 hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
1711 if (happy_meal_tcvr_reset(hp, tregs))
1714 /* Latch PHY registers as of now. */
1715 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1716 hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1718 /* Advertise everything we can support. */
1719 if (hp->sw_bmsr & BMSR_10HALF)
1720 hp->sw_advertise |= (ADVERTISE_10HALF);
1722 hp->sw_advertise &= ~(ADVERTISE_10HALF);
1724 if (hp->sw_bmsr & BMSR_10FULL)
1725 hp->sw_advertise |= (ADVERTISE_10FULL);
1727 hp->sw_advertise &= ~(ADVERTISE_10FULL);
1728 if (hp->sw_bmsr & BMSR_100HALF)
1729 hp->sw_advertise |= (ADVERTISE_100HALF);
1731 hp->sw_advertise &= ~(ADVERTISE_100HALF);
1732 if (hp->sw_bmsr & BMSR_100FULL)
1733 hp->sw_advertise |= (ADVERTISE_100FULL);
1735 hp->sw_advertise &= ~(ADVERTISE_100FULL);
1737 /* Update the PHY advertisement register. */
1738 happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
1741 /* Once status is latched (by happy_meal_interrupt) it is cleared by
1742 * the hardware, so we cannot re-read it and get a correct value.
1744 * hp->happy_lock must be held
1746 static int happy_meal_is_not_so_happy(struct happy_meal *hp, u32 status)
1750 /* Only print messages for non-counter related interrupts. */
1751 if (status & (GREG_STAT_STSTERR | GREG_STAT_TFIFO_UND |
1752 GREG_STAT_MAXPKTERR | GREG_STAT_RXERR |
1753 GREG_STAT_RXPERR | GREG_STAT_RXTERR | GREG_STAT_EOPERR |
1754 GREG_STAT_MIFIRQ | GREG_STAT_TXEACK | GREG_STAT_TXLERR |
1755 GREG_STAT_TXPERR | GREG_STAT_TXTERR | GREG_STAT_SLVERR |
1757 printk(KERN_ERR "%s: Error interrupt for happy meal, status = %08x\n",
1758 hp->dev->name, status);
1760 if (status & GREG_STAT_RFIFOVF) {
1761 /* Receive FIFO overflow is harmless and the hardware will take
1762 care of it, just some packets are lost. Who cares. */
1763 printk(KERN_DEBUG "%s: Happy Meal receive FIFO overflow.\n", hp->dev->name);
1766 if (status & GREG_STAT_STSTERR) {
1767 /* BigMAC SQE link test failed. */
1768 printk(KERN_ERR "%s: Happy Meal BigMAC SQE test failed.\n", hp->dev->name);
1772 if (status & GREG_STAT_TFIFO_UND) {
1773 /* Transmit FIFO underrun, again DMA error likely. */
1774 printk(KERN_ERR "%s: Happy Meal transmitter FIFO underrun, DMA error.\n",
1779 if (status & GREG_STAT_MAXPKTERR) {
1780 /* Driver error, tried to transmit something larger
1781 * than ethernet max mtu.
1783 printk(KERN_ERR "%s: Happy Meal MAX Packet size error.\n", hp->dev->name);
1787 if (status & GREG_STAT_NORXD) {
1788 /* This is harmless, it just means the system is
1789 * quite loaded and the incoming packet rate was
1790 * faster than the interrupt handler could keep up
1793 printk(KERN_INFO "%s: Happy Meal out of receive "
1794 "descriptors, packet dropped.\n",
1798 if (status & (GREG_STAT_RXERR|GREG_STAT_RXPERR|GREG_STAT_RXTERR)) {
1799 /* All sorts of DMA receive errors. */
1800 printk(KERN_ERR "%s: Happy Meal rx DMA errors [ ", hp->dev->name);
1801 if (status & GREG_STAT_RXERR)
1802 printk("GenericError ");
1803 if (status & GREG_STAT_RXPERR)
1804 printk("ParityError ");
1805 if (status & GREG_STAT_RXTERR)
1806 printk("RxTagBotch ");
1811 if (status & GREG_STAT_EOPERR) {
1812 /* Driver bug, didn't set EOP bit in tx descriptor given
1813 * to the happy meal.
1815 printk(KERN_ERR "%s: EOP not set in happy meal transmit descriptor!\n",
1820 if (status & GREG_STAT_MIFIRQ) {
1821 /* MIF signalled an interrupt, were we polling it? */
1822 printk(KERN_ERR "%s: Happy Meal MIF interrupt.\n", hp->dev->name);
1826 (GREG_STAT_TXEACK|GREG_STAT_TXLERR|GREG_STAT_TXPERR|GREG_STAT_TXTERR)) {
1827 /* All sorts of transmit DMA errors. */
1828 printk(KERN_ERR "%s: Happy Meal tx DMA errors [ ", hp->dev->name);
1829 if (status & GREG_STAT_TXEACK)
1830 printk("GenericError ");
1831 if (status & GREG_STAT_TXLERR)
1832 printk("LateError ");
1833 if (status & GREG_STAT_TXPERR)
1834 printk("ParityError ");
1835 if (status & GREG_STAT_TXTERR)
1836 printk("TagBotch ");
1841 if (status & (GREG_STAT_SLVERR|GREG_STAT_SLVPERR)) {
1842 /* Bus or parity error when cpu accessed happy meal registers
1843 * or it's internal FIFO's. Should never see this.
1845 printk(KERN_ERR "%s: Happy Meal register access SBUS slave (%s) error.\n",
1847 (status & GREG_STAT_SLVPERR) ? "parity" : "generic");
1852 printk(KERN_NOTICE "%s: Resetting...\n", hp->dev->name);
1853 happy_meal_init(hp);
1859 /* hp->happy_lock must be held */
1860 static void happy_meal_mif_interrupt(struct happy_meal *hp)
1862 void __iomem *tregs = hp->tcvregs;
1864 printk(KERN_INFO "%s: Link status change.\n", hp->dev->name);
1865 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1866 hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
1868 /* Use the fastest transmission protocol possible. */
1869 if (hp->sw_lpa & LPA_100FULL) {
1870 printk(KERN_INFO "%s: Switching to 100Mbps at full duplex.", hp->dev->name);
1871 hp->sw_bmcr |= (BMCR_FULLDPLX | BMCR_SPEED100);
1872 } else if (hp->sw_lpa & LPA_100HALF) {
1873 printk(KERN_INFO "%s: Switching to 100MBps at half duplex.", hp->dev->name);
1874 hp->sw_bmcr |= BMCR_SPEED100;
1875 } else if (hp->sw_lpa & LPA_10FULL) {
1876 printk(KERN_INFO "%s: Switching to 10MBps at full duplex.", hp->dev->name);
1877 hp->sw_bmcr |= BMCR_FULLDPLX;
1879 printk(KERN_INFO "%s: Using 10Mbps at half duplex.", hp->dev->name);
1881 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1883 /* Finally stop polling and shut up the MIF. */
1884 happy_meal_poll_stop(hp, tregs);
1888 #define TXD(x) printk x
1893 /* hp->happy_lock must be held */
1894 static void happy_meal_tx(struct happy_meal *hp)
1896 struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
1897 struct happy_meal_txd *this;
1898 struct net_device *dev = hp->dev;
1903 while (elem != hp->tx_new) {
1904 struct sk_buff *skb;
1905 u32 flags, dma_addr, dma_len;
1908 TXD(("[%d]", elem));
1909 this = &txbase[elem];
1910 flags = hme_read_desc32(hp, &this->tx_flags);
1911 if (flags & TXFLAG_OWN)
1913 skb = hp->tx_skbs[elem];
1914 if (skb_shinfo(skb)->nr_frags) {
1917 last = elem + skb_shinfo(skb)->nr_frags;
1918 last &= (TX_RING_SIZE - 1);
1919 flags = hme_read_desc32(hp, &txbase[last].tx_flags);
1920 if (flags & TXFLAG_OWN)
1923 hp->tx_skbs[elem] = NULL;
1924 dev->stats.tx_bytes += skb->len;
1926 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1927 dma_addr = hme_read_desc32(hp, &this->tx_addr);
1928 dma_len = hme_read_desc32(hp, &this->tx_flags);
1930 dma_len &= TXFLAG_SIZE;
1932 dma_unmap_single(hp->dma_dev, dma_addr, dma_len, DMA_TO_DEVICE);
1934 dma_unmap_page(hp->dma_dev, dma_addr, dma_len, DMA_TO_DEVICE);
1936 elem = NEXT_TX(elem);
1937 this = &txbase[elem];
1940 dev_consume_skb_irq(skb);
1941 dev->stats.tx_packets++;
1946 if (netif_queue_stopped(dev) &&
1947 TX_BUFFS_AVAIL(hp) > (MAX_SKB_FRAGS + 1))
1948 netif_wake_queue(dev);
1952 #define RXD(x) printk x
1957 /* Originally I used to handle the allocation failure by just giving back just
1958 * that one ring buffer to the happy meal. Problem is that usually when that
1959 * condition is triggered, the happy meal expects you to do something reasonable
1960 * with all of the packets it has DMA'd in. So now I just drop the entire
1961 * ring when we cannot get a new skb and give them all back to the happy meal,
1962 * maybe things will be "happier" now.
1964 * hp->happy_lock must be held
1966 static void happy_meal_rx(struct happy_meal *hp, struct net_device *dev)
1968 struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
1969 struct happy_meal_rxd *this;
1970 int elem = hp->rx_new, drops = 0;
1974 this = &rxbase[elem];
1975 while (!((flags = hme_read_desc32(hp, &this->rx_flags)) & RXFLAG_OWN)) {
1976 struct sk_buff *skb;
1977 int len = flags >> 16;
1978 u16 csum = flags & RXFLAG_CSUM;
1979 u32 dma_addr = hme_read_desc32(hp, &this->rx_addr);
1981 RXD(("[%d ", elem));
1983 /* Check for errors. */
1984 if ((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
1985 RXD(("ERR(%08x)]", flags));
1986 dev->stats.rx_errors++;
1988 dev->stats.rx_length_errors++;
1989 if (len & (RXFLAG_OVERFLOW >> 16)) {
1990 dev->stats.rx_over_errors++;
1991 dev->stats.rx_fifo_errors++;
1994 /* Return it to the Happy meal. */
1996 dev->stats.rx_dropped++;
1997 hme_write_rxd(hp, this,
1998 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
2002 skb = hp->rx_skbs[elem];
2003 if (len > RX_COPY_THRESHOLD) {
2004 struct sk_buff *new_skb;
2007 /* Now refill the entry, if we can. */
2008 new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
2009 if (new_skb == NULL) {
2013 skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
2014 mapping = dma_map_single(hp->dma_dev, new_skb->data,
2017 if (unlikely(dma_mapping_error(hp->dma_dev, mapping))) {
2018 dev_kfree_skb_any(new_skb);
2023 dma_unmap_single(hp->dma_dev, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROM_DEVICE);
2024 hp->rx_skbs[elem] = new_skb;
2025 hme_write_rxd(hp, this,
2026 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
2028 skb_reserve(new_skb, RX_OFFSET);
2030 /* Trim the original skb for the netif. */
2033 struct sk_buff *copy_skb = netdev_alloc_skb(dev, len + 2);
2035 if (copy_skb == NULL) {
2040 skb_reserve(copy_skb, 2);
2041 skb_put(copy_skb, len);
2042 dma_sync_single_for_cpu(hp->dma_dev, dma_addr, len, DMA_FROM_DEVICE);
2043 skb_copy_from_linear_data(skb, copy_skb->data, len);
2044 dma_sync_single_for_device(hp->dma_dev, dma_addr, len, DMA_FROM_DEVICE);
2045 /* Reuse original ring buffer. */
2046 hme_write_rxd(hp, this,
2047 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
2053 /* This card is _fucking_ hot... */
2054 skb->csum = csum_unfold(~(__force __sum16)htons(csum));
2055 skb->ip_summed = CHECKSUM_COMPLETE;
2057 RXD(("len=%d csum=%4x]", len, csum));
2058 skb->protocol = eth_type_trans(skb, dev);
2061 dev->stats.rx_packets++;
2062 dev->stats.rx_bytes += len;
2064 elem = NEXT_RX(elem);
2065 this = &rxbase[elem];
2069 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
2073 static irqreturn_t happy_meal_interrupt(int irq, void *dev_id)
2075 struct net_device *dev = dev_id;
2076 struct happy_meal *hp = netdev_priv(dev);
2077 u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
2079 HMD(("happy_meal_interrupt: status=%08x ", happy_status));
2081 spin_lock(&hp->happy_lock);
2083 if (happy_status & GREG_STAT_ERRORS) {
2085 if (happy_meal_is_not_so_happy(hp, /* un- */ happy_status))
2089 if (happy_status & GREG_STAT_MIFIRQ) {
2091 happy_meal_mif_interrupt(hp);
2094 if (happy_status & GREG_STAT_TXALL) {
2099 if (happy_status & GREG_STAT_RXTOHOST) {
2101 happy_meal_rx(hp, dev);
2106 spin_unlock(&hp->happy_lock);
2112 static irqreturn_t quattro_sbus_interrupt(int irq, void *cookie)
2114 struct quattro *qp = (struct quattro *) cookie;
2117 for (i = 0; i < 4; i++) {
2118 struct net_device *dev = qp->happy_meals[i];
2119 struct happy_meal *hp = netdev_priv(dev);
2120 u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
2122 HMD(("quattro_interrupt: status=%08x ", happy_status));
2124 if (!(happy_status & (GREG_STAT_ERRORS |
2127 GREG_STAT_RXTOHOST)))
2130 spin_lock(&hp->happy_lock);
2132 if (happy_status & GREG_STAT_ERRORS) {
2134 if (happy_meal_is_not_so_happy(hp, happy_status))
2138 if (happy_status & GREG_STAT_MIFIRQ) {
2140 happy_meal_mif_interrupt(hp);
2143 if (happy_status & GREG_STAT_TXALL) {
2148 if (happy_status & GREG_STAT_RXTOHOST) {
2150 happy_meal_rx(hp, dev);
2154 spin_unlock(&hp->happy_lock);
2162 static int happy_meal_open(struct net_device *dev)
2164 struct happy_meal *hp = netdev_priv(dev);
2167 HMD(("happy_meal_open: "));
2169 /* On SBUS Quattro QFE cards, all hme interrupts are concentrated
2170 * into a single source which we register handling at probe time.
2172 if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO) {
2173 res = request_irq(hp->irq, happy_meal_interrupt, IRQF_SHARED,
2177 printk(KERN_ERR "happy_meal(SBUS): Can't order irq %d to go.\n",
2184 HMD(("to happy_meal_init\n"));
2186 spin_lock_irq(&hp->happy_lock);
2187 res = happy_meal_init(hp);
2188 spin_unlock_irq(&hp->happy_lock);
2190 if (res && ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO))
2191 free_irq(hp->irq, dev);
2195 static int happy_meal_close(struct net_device *dev)
2197 struct happy_meal *hp = netdev_priv(dev);
2199 spin_lock_irq(&hp->happy_lock);
2200 happy_meal_stop(hp, hp->gregs);
2201 happy_meal_clean_rings(hp);
2203 /* If auto-negotiation timer is running, kill it. */
2204 del_timer(&hp->happy_timer);
2206 spin_unlock_irq(&hp->happy_lock);
2208 /* On Quattro QFE cards, all hme interrupts are concentrated
2209 * into a single source which we register handling at probe
2210 * time and never unregister.
2212 if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO)
2213 free_irq(hp->irq, dev);
2219 #define SXD(x) printk x
2224 static void happy_meal_tx_timeout(struct net_device *dev, unsigned int txqueue)
2226 struct happy_meal *hp = netdev_priv(dev);
2228 printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
2230 printk (KERN_ERR "%s: Happy Status %08x TX[%08x:%08x]\n", dev->name,
2231 hme_read32(hp, hp->gregs + GREG_STAT),
2232 hme_read32(hp, hp->etxregs + ETX_CFG),
2233 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG));
2235 spin_lock_irq(&hp->happy_lock);
2236 happy_meal_init(hp);
2237 spin_unlock_irq(&hp->happy_lock);
2239 netif_wake_queue(dev);
2242 static void unmap_partial_tx_skb(struct happy_meal *hp, u32 first_mapping,
2243 u32 first_len, u32 first_entry, u32 entry)
2245 struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
2247 dma_unmap_single(hp->dma_dev, first_mapping, first_len, DMA_TO_DEVICE);
2249 first_entry = NEXT_TX(first_entry);
2250 while (first_entry != entry) {
2251 struct happy_meal_txd *this = &txbase[first_entry];
2254 addr = hme_read_desc32(hp, &this->tx_addr);
2255 len = hme_read_desc32(hp, &this->tx_flags);
2257 dma_unmap_page(hp->dma_dev, addr, len, DMA_TO_DEVICE);
2261 static netdev_tx_t happy_meal_start_xmit(struct sk_buff *skb,
2262 struct net_device *dev)
2264 struct happy_meal *hp = netdev_priv(dev);
2268 tx_flags = TXFLAG_OWN;
2269 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2270 const u32 csum_start_off = skb_checksum_start_offset(skb);
2271 const u32 csum_stuff_off = csum_start_off + skb->csum_offset;
2273 tx_flags = (TXFLAG_OWN | TXFLAG_CSENABLE |
2274 ((csum_start_off << 14) & TXFLAG_CSBUFBEGIN) |
2275 ((csum_stuff_off << 20) & TXFLAG_CSLOCATION));
2278 spin_lock_irq(&hp->happy_lock);
2280 if (TX_BUFFS_AVAIL(hp) <= (skb_shinfo(skb)->nr_frags + 1)) {
2281 netif_stop_queue(dev);
2282 spin_unlock_irq(&hp->happy_lock);
2283 printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
2285 return NETDEV_TX_BUSY;
2289 SXD(("SX<l[%d]e[%d]>", len, entry));
2290 hp->tx_skbs[entry] = skb;
2292 if (skb_shinfo(skb)->nr_frags == 0) {
2296 mapping = dma_map_single(hp->dma_dev, skb->data, len, DMA_TO_DEVICE);
2297 if (unlikely(dma_mapping_error(hp->dma_dev, mapping)))
2299 tx_flags |= (TXFLAG_SOP | TXFLAG_EOP);
2300 hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
2301 (tx_flags | (len & TXFLAG_SIZE)),
2303 entry = NEXT_TX(entry);
2305 u32 first_len, first_mapping;
2306 int frag, first_entry = entry;
2308 /* We must give this initial chunk to the device last.
2309 * Otherwise we could race with the device.
2311 first_len = skb_headlen(skb);
2312 first_mapping = dma_map_single(hp->dma_dev, skb->data, first_len,
2314 if (unlikely(dma_mapping_error(hp->dma_dev, first_mapping)))
2316 entry = NEXT_TX(entry);
2318 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
2319 const skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
2320 u32 len, mapping, this_txflags;
2322 len = skb_frag_size(this_frag);
2323 mapping = skb_frag_dma_map(hp->dma_dev, this_frag,
2324 0, len, DMA_TO_DEVICE);
2325 if (unlikely(dma_mapping_error(hp->dma_dev, mapping))) {
2326 unmap_partial_tx_skb(hp, first_mapping, first_len,
2327 first_entry, entry);
2330 this_txflags = tx_flags;
2331 if (frag == skb_shinfo(skb)->nr_frags - 1)
2332 this_txflags |= TXFLAG_EOP;
2333 hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
2334 (this_txflags | (len & TXFLAG_SIZE)),
2336 entry = NEXT_TX(entry);
2338 hme_write_txd(hp, &hp->happy_block->happy_meal_txd[first_entry],
2339 (tx_flags | TXFLAG_SOP | (first_len & TXFLAG_SIZE)),
2345 if (TX_BUFFS_AVAIL(hp) <= (MAX_SKB_FRAGS + 1))
2346 netif_stop_queue(dev);
2349 hme_write32(hp, hp->etxregs + ETX_PENDING, ETX_TP_DMAWAKEUP);
2351 spin_unlock_irq(&hp->happy_lock);
2353 tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
2354 return NETDEV_TX_OK;
2357 hp->tx_skbs[hp->tx_new] = NULL;
2358 spin_unlock_irq(&hp->happy_lock);
2360 dev_kfree_skb_any(skb);
2361 dev->stats.tx_dropped++;
2362 return NETDEV_TX_OK;
2365 static struct net_device_stats *happy_meal_get_stats(struct net_device *dev)
2367 struct happy_meal *hp = netdev_priv(dev);
2369 spin_lock_irq(&hp->happy_lock);
2370 happy_meal_get_counters(hp, hp->bigmacregs);
2371 spin_unlock_irq(&hp->happy_lock);
2376 static void happy_meal_set_multicast(struct net_device *dev)
2378 struct happy_meal *hp = netdev_priv(dev);
2379 void __iomem *bregs = hp->bigmacregs;
2380 struct netdev_hw_addr *ha;
2383 spin_lock_irq(&hp->happy_lock);
2385 if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
2386 hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
2387 hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
2388 hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
2389 hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
2390 } else if (dev->flags & IFF_PROMISC) {
2391 hme_write32(hp, bregs + BMAC_RXCFG,
2392 hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_PMISC);
2396 memset(hash_table, 0, sizeof(hash_table));
2397 netdev_for_each_mc_addr(ha, dev) {
2398 crc = ether_crc_le(6, ha->addr);
2400 hash_table[crc >> 4] |= 1 << (crc & 0xf);
2402 hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
2403 hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
2404 hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
2405 hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
2408 spin_unlock_irq(&hp->happy_lock);
2411 /* Ethtool support... */
2412 static int hme_get_link_ksettings(struct net_device *dev,
2413 struct ethtool_link_ksettings *cmd)
2415 struct happy_meal *hp = netdev_priv(dev);
2420 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2421 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2422 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
2424 /* XXX hardcoded stuff for now */
2425 cmd->base.port = PORT_TP; /* XXX no MII support */
2426 cmd->base.phy_address = 0; /* XXX fixed PHYAD */
2428 /* Record PHY settings. */
2429 spin_lock_irq(&hp->happy_lock);
2430 hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
2431 hp->sw_lpa = happy_meal_tcvr_read(hp, hp->tcvregs, MII_LPA);
2432 spin_unlock_irq(&hp->happy_lock);
2434 if (hp->sw_bmcr & BMCR_ANENABLE) {
2435 cmd->base.autoneg = AUTONEG_ENABLE;
2436 speed = ((hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) ?
2437 SPEED_100 : SPEED_10);
2438 if (speed == SPEED_100)
2440 (hp->sw_lpa & (LPA_100FULL)) ?
2441 DUPLEX_FULL : DUPLEX_HALF;
2444 (hp->sw_lpa & (LPA_10FULL)) ?
2445 DUPLEX_FULL : DUPLEX_HALF;
2447 cmd->base.autoneg = AUTONEG_DISABLE;
2448 speed = (hp->sw_bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
2450 (hp->sw_bmcr & BMCR_FULLDPLX) ?
2451 DUPLEX_FULL : DUPLEX_HALF;
2453 cmd->base.speed = speed;
2454 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2460 static int hme_set_link_ksettings(struct net_device *dev,
2461 const struct ethtool_link_ksettings *cmd)
2463 struct happy_meal *hp = netdev_priv(dev);
2465 /* Verify the settings we care about. */
2466 if (cmd->base.autoneg != AUTONEG_ENABLE &&
2467 cmd->base.autoneg != AUTONEG_DISABLE)
2469 if (cmd->base.autoneg == AUTONEG_DISABLE &&
2470 ((cmd->base.speed != SPEED_100 &&
2471 cmd->base.speed != SPEED_10) ||
2472 (cmd->base.duplex != DUPLEX_HALF &&
2473 cmd->base.duplex != DUPLEX_FULL)))
2476 /* Ok, do it to it. */
2477 spin_lock_irq(&hp->happy_lock);
2478 del_timer(&hp->happy_timer);
2479 happy_meal_begin_auto_negotiation(hp, hp->tcvregs, cmd);
2480 spin_unlock_irq(&hp->happy_lock);
2485 static void hme_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2487 struct happy_meal *hp = netdev_priv(dev);
2489 strlcpy(info->driver, "sunhme", sizeof(info->driver));
2490 strlcpy(info->version, "2.02", sizeof(info->version));
2491 if (hp->happy_flags & HFLAG_PCI) {
2492 struct pci_dev *pdev = hp->happy_dev;
2493 strlcpy(info->bus_info, pci_name(pdev), sizeof(info->bus_info));
2497 const struct linux_prom_registers *regs;
2498 struct platform_device *op = hp->happy_dev;
2499 regs = of_get_property(op->dev.of_node, "regs", NULL);
2501 snprintf(info->bus_info, sizeof(info->bus_info),
2508 static u32 hme_get_link(struct net_device *dev)
2510 struct happy_meal *hp = netdev_priv(dev);
2512 spin_lock_irq(&hp->happy_lock);
2513 hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
2514 spin_unlock_irq(&hp->happy_lock);
2516 return hp->sw_bmsr & BMSR_LSTATUS;
2519 static const struct ethtool_ops hme_ethtool_ops = {
2520 .get_drvinfo = hme_get_drvinfo,
2521 .get_link = hme_get_link,
2522 .get_link_ksettings = hme_get_link_ksettings,
2523 .set_link_ksettings = hme_set_link_ksettings,
2526 static int hme_version_printed;
2529 /* Given a happy meal sbus device, find it's quattro parent.
2530 * If none exist, allocate and return a new one.
2532 * Return NULL on failure.
2534 static struct quattro *quattro_sbus_find(struct platform_device *child)
2536 struct device *parent = child->dev.parent;
2537 struct platform_device *op;
2540 op = to_platform_device(parent);
2541 qp = platform_get_drvdata(op);
2545 qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
2549 for (i = 0; i < 4; i++)
2550 qp->happy_meals[i] = NULL;
2552 qp->quattro_dev = child;
2553 qp->next = qfe_sbus_list;
2556 platform_set_drvdata(op, qp);
2561 /* After all quattro cards have been probed, we call these functions
2562 * to register the IRQ handlers for the cards that have been
2563 * successfully probed and skip the cards that failed to initialize
2565 static int __init quattro_sbus_register_irqs(void)
2569 for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
2570 struct platform_device *op = qp->quattro_dev;
2571 int err, qfe_slot, skip = 0;
2573 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++) {
2574 if (!qp->happy_meals[qfe_slot])
2580 err = request_irq(op->archdata.irqs[0],
2581 quattro_sbus_interrupt,
2582 IRQF_SHARED, "Quattro",
2585 printk(KERN_ERR "Quattro HME: IRQ registration "
2586 "error %d.\n", err);
2594 static void quattro_sbus_free_irqs(void)
2598 for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
2599 struct platform_device *op = qp->quattro_dev;
2600 int qfe_slot, skip = 0;
2602 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++) {
2603 if (!qp->happy_meals[qfe_slot])
2609 free_irq(op->archdata.irqs[0], qp);
2612 #endif /* CONFIG_SBUS */
2615 static struct quattro *quattro_pci_find(struct pci_dev *pdev)
2617 struct pci_dev *bdev = pdev->bus->self;
2620 if (!bdev) return NULL;
2621 for (qp = qfe_pci_list; qp != NULL; qp = qp->next) {
2622 struct pci_dev *qpdev = qp->quattro_dev;
2627 qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
2631 for (i = 0; i < 4; i++)
2632 qp->happy_meals[i] = NULL;
2634 qp->quattro_dev = bdev;
2635 qp->next = qfe_pci_list;
2638 /* No range tricks necessary on PCI. */
2643 #endif /* CONFIG_PCI */
2645 static const struct net_device_ops hme_netdev_ops = {
2646 .ndo_open = happy_meal_open,
2647 .ndo_stop = happy_meal_close,
2648 .ndo_start_xmit = happy_meal_start_xmit,
2649 .ndo_tx_timeout = happy_meal_tx_timeout,
2650 .ndo_get_stats = happy_meal_get_stats,
2651 .ndo_set_rx_mode = happy_meal_set_multicast,
2652 .ndo_set_mac_address = eth_mac_addr,
2653 .ndo_validate_addr = eth_validate_addr,
2657 static int happy_meal_sbus_probe_one(struct platform_device *op, int is_qfe)
2659 struct device_node *dp = op->dev.of_node, *sbus_dp;
2660 struct quattro *qp = NULL;
2661 struct happy_meal *hp;
2662 struct net_device *dev;
2663 int i, qfe_slot = -1;
2667 sbus_dp = op->dev.parent->of_node;
2669 /* We can match PCI devices too, do not accept those here. */
2670 if (!of_node_name_eq(sbus_dp, "sbus") && !of_node_name_eq(sbus_dp, "sbi"))
2674 qp = quattro_sbus_find(op);
2677 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
2678 if (qp->happy_meals[qfe_slot] == NULL)
2685 dev = alloc_etherdev(sizeof(struct happy_meal));
2688 SET_NETDEV_DEV(dev, &op->dev);
2690 if (hme_version_printed++ == 0)
2691 printk(KERN_INFO "%s", version);
2693 /* If user did not specify a MAC address specifically, use
2694 * the Quattro local-mac-address property...
2696 for (i = 0; i < 6; i++) {
2697 if (macaddr[i] != 0)
2700 if (i < 6) { /* a mac address was given */
2701 for (i = 0; i < 6; i++)
2702 addr[i] = macaddr[i];
2703 eth_hw_addr_set(dev, addr);
2706 const unsigned char *addr;
2709 addr = of_get_property(dp, "local-mac-address", &len);
2711 if (qfe_slot != -1 && addr && len == ETH_ALEN)
2712 eth_hw_addr_set(dev, addr);
2714 eth_hw_addr_set(dev, idprom->id_ethaddr);
2717 hp = netdev_priv(dev);
2720 hp->dma_dev = &op->dev;
2722 spin_lock_init(&hp->happy_lock);
2726 hp->qfe_parent = qp;
2727 hp->qfe_ent = qfe_slot;
2728 qp->happy_meals[qfe_slot] = dev;
2731 hp->gregs = of_ioremap(&op->resource[0], 0,
2732 GREG_REG_SIZE, "HME Global Regs");
2734 printk(KERN_ERR "happymeal: Cannot map global registers.\n");
2735 goto err_out_free_netdev;
2738 hp->etxregs = of_ioremap(&op->resource[1], 0,
2739 ETX_REG_SIZE, "HME TX Regs");
2741 printk(KERN_ERR "happymeal: Cannot map MAC TX registers.\n");
2742 goto err_out_iounmap;
2745 hp->erxregs = of_ioremap(&op->resource[2], 0,
2746 ERX_REG_SIZE, "HME RX Regs");
2748 printk(KERN_ERR "happymeal: Cannot map MAC RX registers.\n");
2749 goto err_out_iounmap;
2752 hp->bigmacregs = of_ioremap(&op->resource[3], 0,
2753 BMAC_REG_SIZE, "HME BIGMAC Regs");
2754 if (!hp->bigmacregs) {
2755 printk(KERN_ERR "happymeal: Cannot map BIGMAC registers.\n");
2756 goto err_out_iounmap;
2759 hp->tcvregs = of_ioremap(&op->resource[4], 0,
2760 TCVR_REG_SIZE, "HME Tranceiver Regs");
2762 printk(KERN_ERR "happymeal: Cannot map TCVR registers.\n");
2763 goto err_out_iounmap;
2766 hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
2767 if (hp->hm_revision == 0xff)
2768 hp->hm_revision = 0xa0;
2770 /* Now enable the feature flags we can. */
2771 if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
2772 hp->happy_flags = HFLAG_20_21;
2773 else if (hp->hm_revision != 0xa0)
2774 hp->happy_flags = HFLAG_NOT_A0;
2777 hp->happy_flags |= HFLAG_QUATTRO;
2779 /* Get the supported DVMA burst sizes from our Happy SBUS. */
2780 hp->happy_bursts = of_getintprop_default(sbus_dp,
2781 "burst-sizes", 0x00);
2783 hp->happy_block = dma_alloc_coherent(hp->dma_dev,
2788 if (!hp->happy_block)
2789 goto err_out_iounmap;
2791 /* Force check of the link first time we are brought up. */
2794 /* Force timer state to 'asleep' with count of zero. */
2795 hp->timer_state = asleep;
2796 hp->timer_ticks = 0;
2798 timer_setup(&hp->happy_timer, happy_meal_timer, 0);
2801 dev->netdev_ops = &hme_netdev_ops;
2802 dev->watchdog_timeo = 5*HZ;
2803 dev->ethtool_ops = &hme_ethtool_ops;
2805 /* Happy Meal can do it all... */
2806 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
2807 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
2809 hp->irq = op->archdata.irqs[0];
2811 #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
2812 /* Hook up SBUS register/descriptor accessors. */
2813 hp->read_desc32 = sbus_hme_read_desc32;
2814 hp->write_txd = sbus_hme_write_txd;
2815 hp->write_rxd = sbus_hme_write_rxd;
2816 hp->read32 = sbus_hme_read32;
2817 hp->write32 = sbus_hme_write32;
2820 /* Grrr, Happy Meal comes up by default not advertising
2821 * full duplex 100baseT capabilities, fix this.
2823 spin_lock_irq(&hp->happy_lock);
2824 happy_meal_set_initial_advertisement(hp);
2825 spin_unlock_irq(&hp->happy_lock);
2827 err = register_netdev(hp->dev);
2829 printk(KERN_ERR "happymeal: Cannot register net device, "
2831 goto err_out_free_coherent;
2834 platform_set_drvdata(op, hp);
2837 printk(KERN_INFO "%s: Quattro HME slot %d (SBUS) 10/100baseT Ethernet ",
2838 dev->name, qfe_slot);
2840 printk(KERN_INFO "%s: HAPPY MEAL (SBUS) 10/100baseT Ethernet ",
2843 printk("%pM\n", dev->dev_addr);
2847 err_out_free_coherent:
2848 dma_free_coherent(hp->dma_dev,
2855 of_iounmap(&op->resource[0], hp->gregs, GREG_REG_SIZE);
2857 of_iounmap(&op->resource[1], hp->etxregs, ETX_REG_SIZE);
2859 of_iounmap(&op->resource[2], hp->erxregs, ERX_REG_SIZE);
2861 of_iounmap(&op->resource[3], hp->bigmacregs, BMAC_REG_SIZE);
2863 of_iounmap(&op->resource[4], hp->tcvregs, TCVR_REG_SIZE);
2866 qp->happy_meals[qfe_slot] = NULL;
2868 err_out_free_netdev:
2877 #ifndef CONFIG_SPARC
2878 static int is_quattro_p(struct pci_dev *pdev)
2880 struct pci_dev *busdev = pdev->bus->self;
2881 struct pci_dev *this_pdev;
2884 if (busdev == NULL ||
2885 busdev->vendor != PCI_VENDOR_ID_DEC ||
2886 busdev->device != PCI_DEVICE_ID_DEC_21153)
2890 list_for_each_entry(this_pdev, &pdev->bus->devices, bus_list) {
2891 if (this_pdev->vendor == PCI_VENDOR_ID_SUN &&
2892 this_pdev->device == PCI_DEVICE_ID_SUN_HAPPYMEAL)
2902 /* Fetch MAC address from vital product data of PCI ROM. */
2903 static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, int index, unsigned char *dev_addr)
2907 for (this_offset = 0x20; this_offset < len; this_offset++) {
2908 void __iomem *p = rom_base + this_offset;
2910 if (readb(p + 0) != 0x90 ||
2911 readb(p + 1) != 0x00 ||
2912 readb(p + 2) != 0x09 ||
2913 readb(p + 3) != 0x4e ||
2914 readb(p + 4) != 0x41 ||
2915 readb(p + 5) != 0x06)
2924 for (i = 0; i < 6; i++)
2925 dev_addr[i] = readb(p + i);
2933 static void get_hme_mac_nonsparc(struct pci_dev *pdev, unsigned char *dev_addr)
2936 void __iomem *p = pci_map_rom(pdev, &size);
2942 if (is_quattro_p(pdev))
2943 index = PCI_SLOT(pdev->devfn);
2945 found = readb(p) == 0x55 &&
2946 readb(p + 1) == 0xaa &&
2947 find_eth_addr_in_vpd(p, (64 * 1024), index, dev_addr);
2948 pci_unmap_rom(pdev, p);
2953 /* Sun MAC prefix then 3 random bytes. */
2957 get_random_bytes(&dev_addr[3], 3);
2959 #endif /* !(CONFIG_SPARC) */
2961 static int happy_meal_pci_probe(struct pci_dev *pdev,
2962 const struct pci_device_id *ent)
2964 struct quattro *qp = NULL;
2966 struct device_node *dp;
2968 struct happy_meal *hp;
2969 struct net_device *dev;
2970 void __iomem *hpreg_base;
2971 unsigned long hpreg_res;
2972 int i, qfe_slot = -1;
2977 /* Now make sure pci_dev cookie is there. */
2979 dp = pci_device_to_OF_node(pdev);
2980 snprintf(prom_name, sizeof(prom_name), "%pOFn", dp);
2982 if (is_quattro_p(pdev))
2983 strcpy(prom_name, "SUNW,qfe");
2985 strcpy(prom_name, "SUNW,hme");
2990 if (pci_enable_device(pdev))
2992 pci_set_master(pdev);
2994 if (!strcmp(prom_name, "SUNW,qfe") || !strcmp(prom_name, "qfe")) {
2995 qp = quattro_pci_find(pdev);
2998 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
2999 if (qp->happy_meals[qfe_slot] == NULL)
3005 dev = alloc_etherdev(sizeof(struct happy_meal));
3009 SET_NETDEV_DEV(dev, &pdev->dev);
3011 if (hme_version_printed++ == 0)
3012 printk(KERN_INFO "%s", version);
3014 hp = netdev_priv(dev);
3016 hp->happy_dev = pdev;
3017 hp->dma_dev = &pdev->dev;
3019 spin_lock_init(&hp->happy_lock);
3022 hp->qfe_parent = qp;
3023 hp->qfe_ent = qfe_slot;
3024 qp->happy_meals[qfe_slot] = dev;
3027 hpreg_res = pci_resource_start(pdev, 0);
3029 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
3030 printk(KERN_ERR "happymeal(PCI): Cannot find proper PCI device base address.\n");
3031 goto err_out_clear_quattro;
3033 if (pci_request_regions(pdev, DRV_NAME)) {
3034 printk(KERN_ERR "happymeal(PCI): Cannot obtain PCI resources, "
3036 goto err_out_clear_quattro;
3039 if ((hpreg_base = ioremap(hpreg_res, 0x8000)) == NULL) {
3040 printk(KERN_ERR "happymeal(PCI): Unable to remap card memory.\n");
3041 goto err_out_free_res;
3044 for (i = 0; i < 6; i++) {
3045 if (macaddr[i] != 0)
3048 if (i < 6) { /* a mac address was given */
3049 for (i = 0; i < 6; i++)
3050 addr[i] = macaddr[i];
3051 eth_hw_addr_set(dev, addr);
3055 const unsigned char *addr;
3058 if (qfe_slot != -1 &&
3059 (addr = of_get_property(dp, "local-mac-address", &len))
3062 eth_hw_addr_set(dev, addr);
3064 eth_hw_addr_set(dev, idprom->id_ethaddr);
3069 get_hme_mac_nonsparc(pdev, addr);
3070 eth_hw_addr_set(dev, addr);
3074 /* Layout registers. */
3075 hp->gregs = (hpreg_base + 0x0000UL);
3076 hp->etxregs = (hpreg_base + 0x2000UL);
3077 hp->erxregs = (hpreg_base + 0x4000UL);
3078 hp->bigmacregs = (hpreg_base + 0x6000UL);
3079 hp->tcvregs = (hpreg_base + 0x7000UL);
3082 hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
3083 if (hp->hm_revision == 0xff)
3084 hp->hm_revision = 0xc0 | (pdev->revision & 0x0f);
3086 /* works with this on non-sparc hosts */
3087 hp->hm_revision = 0x20;
3090 /* Now enable the feature flags we can. */
3091 if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
3092 hp->happy_flags = HFLAG_20_21;
3093 else if (hp->hm_revision != 0xa0 && hp->hm_revision != 0xc0)
3094 hp->happy_flags = HFLAG_NOT_A0;
3097 hp->happy_flags |= HFLAG_QUATTRO;
3099 /* And of course, indicate this is PCI. */
3100 hp->happy_flags |= HFLAG_PCI;
3103 /* Assume PCI happy meals can handle all burst sizes. */
3104 hp->happy_bursts = DMA_BURSTBITS;
3107 hp->happy_block = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3108 &hp->hblock_dvma, GFP_KERNEL);
3110 if (!hp->happy_block)
3111 goto err_out_iounmap;
3114 hp->timer_state = asleep;
3115 hp->timer_ticks = 0;
3117 timer_setup(&hp->happy_timer, happy_meal_timer, 0);
3119 hp->irq = pdev->irq;
3121 dev->netdev_ops = &hme_netdev_ops;
3122 dev->watchdog_timeo = 5*HZ;
3123 dev->ethtool_ops = &hme_ethtool_ops;
3125 /* Happy Meal can do it all... */
3126 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
3127 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
3129 #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
3130 /* Hook up PCI register/descriptor accessors. */
3131 hp->read_desc32 = pci_hme_read_desc32;
3132 hp->write_txd = pci_hme_write_txd;
3133 hp->write_rxd = pci_hme_write_rxd;
3134 hp->read32 = pci_hme_read32;
3135 hp->write32 = pci_hme_write32;
3138 /* Grrr, Happy Meal comes up by default not advertising
3139 * full duplex 100baseT capabilities, fix this.
3141 spin_lock_irq(&hp->happy_lock);
3142 happy_meal_set_initial_advertisement(hp);
3143 spin_unlock_irq(&hp->happy_lock);
3145 err = register_netdev(hp->dev);
3147 printk(KERN_ERR "happymeal(PCI): Cannot register net device, "
3149 goto err_out_iounmap;
3152 pci_set_drvdata(pdev, hp);
3155 struct pci_dev *qpdev = qp->quattro_dev;
3158 if (!strncmp(dev->name, "eth", 3)) {
3159 int i = simple_strtoul(dev->name + 3, NULL, 10);
3160 sprintf(prom_name, "-%d", i + 3);
3162 printk(KERN_INFO "%s%s: Quattro HME (PCI/CheerIO) 10/100baseT Ethernet ", dev->name, prom_name);
3163 if (qpdev->vendor == PCI_VENDOR_ID_DEC &&
3164 qpdev->device == PCI_DEVICE_ID_DEC_21153)
3165 printk("DEC 21153 PCI Bridge\n");
3167 printk("unknown bridge %04x.%04x\n",
3168 qpdev->vendor, qpdev->device);
3172 printk(KERN_INFO "%s: Quattro HME slot %d (PCI/CheerIO) 10/100baseT Ethernet ",
3173 dev->name, qfe_slot);
3175 printk(KERN_INFO "%s: HAPPY MEAL (PCI/CheerIO) 10/100BaseT Ethernet ",
3178 printk("%pM\n", dev->dev_addr);
3186 pci_release_regions(pdev);
3188 err_out_clear_quattro:
3190 qp->happy_meals[qfe_slot] = NULL;
3198 static void happy_meal_pci_remove(struct pci_dev *pdev)
3200 struct happy_meal *hp = pci_get_drvdata(pdev);
3201 struct net_device *net_dev = hp->dev;
3203 unregister_netdev(net_dev);
3205 dma_free_coherent(hp->dma_dev, PAGE_SIZE,
3206 hp->happy_block, hp->hblock_dvma);
3208 pci_release_regions(hp->happy_dev);
3210 free_netdev(net_dev);
3213 static const struct pci_device_id happymeal_pci_ids[] = {
3214 { PCI_DEVICE(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_HAPPYMEAL) },
3215 { } /* Terminating entry */
3218 MODULE_DEVICE_TABLE(pci, happymeal_pci_ids);
3220 static struct pci_driver hme_pci_driver = {
3222 .id_table = happymeal_pci_ids,
3223 .probe = happy_meal_pci_probe,
3224 .remove = happy_meal_pci_remove,
3227 static int __init happy_meal_pci_init(void)
3229 return pci_register_driver(&hme_pci_driver);
3232 static void happy_meal_pci_exit(void)
3234 pci_unregister_driver(&hme_pci_driver);
3236 while (qfe_pci_list) {
3237 struct quattro *qfe = qfe_pci_list;
3238 struct quattro *next = qfe->next;
3242 qfe_pci_list = next;
3249 static const struct of_device_id hme_sbus_match[];
3250 static int hme_sbus_probe(struct platform_device *op)
3252 const struct of_device_id *match;
3253 struct device_node *dp = op->dev.of_node;
3254 const char *model = of_get_property(dp, "model", NULL);
3257 match = of_match_device(hme_sbus_match, &op->dev);
3260 is_qfe = (match->data != NULL);
3262 if (!is_qfe && model && !strcmp(model, "SUNW,sbus-qfe"))
3265 return happy_meal_sbus_probe_one(op, is_qfe);
3268 static int hme_sbus_remove(struct platform_device *op)
3270 struct happy_meal *hp = platform_get_drvdata(op);
3271 struct net_device *net_dev = hp->dev;
3273 unregister_netdev(net_dev);
3275 /* XXX qfe parent interrupt... */
3277 of_iounmap(&op->resource[0], hp->gregs, GREG_REG_SIZE);
3278 of_iounmap(&op->resource[1], hp->etxregs, ETX_REG_SIZE);
3279 of_iounmap(&op->resource[2], hp->erxregs, ERX_REG_SIZE);
3280 of_iounmap(&op->resource[3], hp->bigmacregs, BMAC_REG_SIZE);
3281 of_iounmap(&op->resource[4], hp->tcvregs, TCVR_REG_SIZE);
3282 dma_free_coherent(hp->dma_dev,
3287 free_netdev(net_dev);
3292 static const struct of_device_id hme_sbus_match[] = {
3307 MODULE_DEVICE_TABLE(of, hme_sbus_match);
3309 static struct platform_driver hme_sbus_driver = {
3312 .of_match_table = hme_sbus_match,
3314 .probe = hme_sbus_probe,
3315 .remove = hme_sbus_remove,
3318 static int __init happy_meal_sbus_init(void)
3322 err = platform_driver_register(&hme_sbus_driver);
3324 err = quattro_sbus_register_irqs();
3329 static void happy_meal_sbus_exit(void)
3331 platform_driver_unregister(&hme_sbus_driver);
3332 quattro_sbus_free_irqs();
3334 while (qfe_sbus_list) {
3335 struct quattro *qfe = qfe_sbus_list;
3336 struct quattro *next = qfe->next;
3340 qfe_sbus_list = next;
3345 static int __init happy_meal_probe(void)
3350 err = happy_meal_sbus_init();
3354 err = happy_meal_pci_init();
3357 happy_meal_sbus_exit();
3366 static void __exit happy_meal_exit(void)
3369 happy_meal_sbus_exit();
3372 happy_meal_pci_exit();
3376 module_init(happy_meal_probe);
3377 module_exit(happy_meal_exit);