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,
7 * 2006, 2008 David S. Miller (davem@davemloft.net)
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"
66 #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
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 hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
551 "Link is up using %s transceiver at %dMb/s, %s Duplex.\n",
552 hp->tcvr_type == external ? "external" : "internal",
553 hp->sw_lpa & (LPA_100HALF | LPA_100FULL) ? 100 : 10,
554 hp->sw_lpa & (LPA_100FULL | LPA_10FULL) ? "Full" : "Half");
557 static void display_forced_link_mode(struct happy_meal *hp, void __iomem *tregs)
559 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
562 "Link has been forced up using %s transceiver at %dMb/s, %s Duplex.\n",
563 hp->tcvr_type == external ? "external" : "internal",
564 hp->sw_bmcr & BMCR_SPEED100 ? 100 : 10,
565 hp->sw_bmcr & BMCR_FULLDPLX ? "Full" : "Half");
568 static int set_happy_link_modes(struct happy_meal *hp, void __iomem *tregs)
572 /* All we care about is making sure the bigmac tx_cfg has a
573 * proper duplex setting.
575 if (hp->timer_state == arbwait) {
576 hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
577 if (!(hp->sw_lpa & (LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL)))
579 if (hp->sw_lpa & LPA_100FULL)
581 else if (hp->sw_lpa & LPA_100HALF)
583 else if (hp->sw_lpa & LPA_10FULL)
588 /* Forcing a link mode. */
589 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
590 if (hp->sw_bmcr & BMCR_FULLDPLX)
596 /* Before changing other bits in the tx_cfg register, and in
597 * general any of other the TX config registers too, you
600 * 2) Poll with reads until that bit reads back as zero
601 * 3) Make TX configuration changes
602 * 4) Set Enable once more
604 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
605 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
606 ~(BIGMAC_TXCFG_ENABLE));
607 while (hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) & BIGMAC_TXCFG_ENABLE)
610 hp->happy_flags |= HFLAG_FULL;
611 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
612 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
613 BIGMAC_TXCFG_FULLDPLX);
615 hp->happy_flags &= ~(HFLAG_FULL);
616 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
617 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
618 ~(BIGMAC_TXCFG_FULLDPLX));
620 hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
621 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
622 BIGMAC_TXCFG_ENABLE);
628 static int happy_meal_init(struct happy_meal *hp);
630 static int is_lucent_phy(struct happy_meal *hp)
632 void __iomem *tregs = hp->tcvregs;
633 unsigned short mr2, mr3;
636 mr2 = happy_meal_tcvr_read(hp, tregs, 2);
637 mr3 = happy_meal_tcvr_read(hp, tregs, 3);
638 if ((mr2 & 0xffff) == 0x0180 &&
639 ((mr3 & 0xffff) >> 10) == 0x1d)
645 static void happy_meal_timer(struct timer_list *t)
647 struct happy_meal *hp = from_timer(hp, t, happy_timer);
648 void __iomem *tregs = hp->tcvregs;
649 int restart_timer = 0;
651 spin_lock_irq(&hp->happy_lock);
654 switch(hp->timer_state) {
656 /* Only allow for 5 ticks, thats 10 seconds and much too
657 * long to wait for arbitration to complete.
659 if (hp->timer_ticks >= 10) {
660 /* Enter force mode. */
662 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
663 printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful, trying force link mode\n",
665 hp->sw_bmcr = BMCR_SPEED100;
666 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
668 if (!is_lucent_phy(hp)) {
669 /* OK, seems we need do disable the transceiver for the first
670 * tick to make sure we get an accurate link state at the
673 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
674 hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
675 happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG, hp->sw_csconfig);
677 hp->timer_state = ltrywait;
681 /* Anything interesting happen? */
682 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
683 if (hp->sw_bmsr & BMSR_ANEGCOMPLETE) {
686 /* Just what we've been waiting for... */
687 ret = set_happy_link_modes(hp, tregs);
689 /* Ooops, something bad happened, go to force
692 * XXX Broken hubs which don't support 802.3u
693 * XXX auto-negotiation make this happen as well.
698 /* Success, at least so far, advance our state engine. */
699 hp->timer_state = lupwait;
708 /* Auto negotiation was successful and we are awaiting a
709 * link up status. I have decided to let this timer run
710 * forever until some sort of error is signalled, reporting
711 * a message to the user at 10 second intervals.
713 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
714 if (hp->sw_bmsr & BMSR_LSTATUS) {
715 /* Wheee, it's up, display the link mode in use and put
716 * the timer to sleep.
718 display_link_mode(hp, tregs);
719 hp->timer_state = asleep;
722 if (hp->timer_ticks >= 10) {
723 printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
724 "not completely up.\n", hp->dev->name);
734 /* Making the timeout here too long can make it take
735 * annoyingly long to attempt all of the link mode
736 * permutations, but then again this is essentially
737 * error recovery code for the most part.
739 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
740 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
741 if (hp->timer_ticks == 1) {
742 if (!is_lucent_phy(hp)) {
743 /* Re-enable transceiver, we'll re-enable the transceiver next
744 * tick, then check link state on the following tick.
746 hp->sw_csconfig |= CSCONFIG_TCVDISAB;
747 happy_meal_tcvr_write(hp, tregs,
748 DP83840_CSCONFIG, hp->sw_csconfig);
753 if (hp->timer_ticks == 2) {
754 if (!is_lucent_phy(hp)) {
755 hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
756 happy_meal_tcvr_write(hp, tregs,
757 DP83840_CSCONFIG, hp->sw_csconfig);
762 if (hp->sw_bmsr & BMSR_LSTATUS) {
763 /* Force mode selection success. */
764 display_forced_link_mode(hp, tregs);
765 set_happy_link_modes(hp, tregs); /* XXX error? then what? */
766 hp->timer_state = asleep;
769 if (hp->timer_ticks >= 4) { /* 6 seconds or so... */
772 ret = try_next_permutation(hp, tregs);
774 /* Aieee, tried them all, reset the
775 * chip and try all over again.
778 /* Let the user know... */
779 printk(KERN_NOTICE "%s: Link down, cable problem?\n",
782 ret = happy_meal_init(hp);
785 printk(KERN_ERR "%s: Error, cannot re-init the "
786 "Happy Meal.\n", hp->dev->name);
790 if (!is_lucent_phy(hp)) {
791 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
793 hp->sw_csconfig |= CSCONFIG_TCVDISAB;
794 happy_meal_tcvr_write(hp, tregs,
795 DP83840_CSCONFIG, hp->sw_csconfig);
807 /* Can't happens.... */
808 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
812 hp->timer_state = asleep; /* foo on you */
817 hp->happy_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
818 add_timer(&hp->happy_timer);
822 spin_unlock_irq(&hp->happy_lock);
825 #define TX_RESET_TRIES 32
826 #define RX_RESET_TRIES 32
828 /* hp->happy_lock must be held */
829 static void happy_meal_tx_reset(struct happy_meal *hp, void __iomem *bregs)
831 int tries = TX_RESET_TRIES;
833 HMD(("happy_meal_tx_reset: reset, "));
835 /* Would you like to try our SMCC Delux? */
836 hme_write32(hp, bregs + BMAC_TXSWRESET, 0);
837 while ((hme_read32(hp, bregs + BMAC_TXSWRESET) & 1) && --tries)
840 /* Lettuce, tomato, buggy hardware (no extra charge)? */
842 printk(KERN_ERR "happy meal: Transceiver BigMac ATTACK!");
848 /* hp->happy_lock must be held */
849 static void happy_meal_rx_reset(struct happy_meal *hp, void __iomem *bregs)
851 int tries = RX_RESET_TRIES;
853 HMD(("happy_meal_rx_reset: reset, "));
855 /* We have a special on GNU/Viking hardware bugs today. */
856 hme_write32(hp, bregs + BMAC_RXSWRESET, 0);
857 while ((hme_read32(hp, bregs + BMAC_RXSWRESET) & 1) && --tries)
860 /* Will that be all? */
862 printk(KERN_ERR "happy meal: Receiver BigMac ATTACK!");
864 /* Don't forget your vik_1137125_wa. Have a nice day. */
868 #define STOP_TRIES 16
870 /* hp->happy_lock must be held */
871 static void happy_meal_stop(struct happy_meal *hp, void __iomem *gregs)
873 int tries = STOP_TRIES;
875 HMD(("happy_meal_stop: reset, "));
877 /* We're consolidating our STB products, it's your lucky day. */
878 hme_write32(hp, gregs + GREG_SWRESET, GREG_RESET_ALL);
879 while (hme_read32(hp, gregs + GREG_SWRESET) && --tries)
882 /* Come back next week when we are "Sun Microelectronics". */
884 printk(KERN_ERR "happy meal: Fry guys.");
886 /* Remember: "Different name, same old buggy as shit hardware." */
890 /* hp->happy_lock must be held */
891 static void happy_meal_get_counters(struct happy_meal *hp, void __iomem *bregs)
893 struct net_device_stats *stats = &hp->dev->stats;
895 stats->rx_crc_errors += hme_read32(hp, bregs + BMAC_RCRCECTR);
896 hme_write32(hp, bregs + BMAC_RCRCECTR, 0);
898 stats->rx_frame_errors += hme_read32(hp, bregs + BMAC_UNALECTR);
899 hme_write32(hp, bregs + BMAC_UNALECTR, 0);
901 stats->rx_length_errors += hme_read32(hp, bregs + BMAC_GLECTR);
902 hme_write32(hp, bregs + BMAC_GLECTR, 0);
904 stats->tx_aborted_errors += hme_read32(hp, bregs + BMAC_EXCTR);
907 (hme_read32(hp, bregs + BMAC_EXCTR) +
908 hme_read32(hp, bregs + BMAC_LTCTR));
909 hme_write32(hp, bregs + BMAC_EXCTR, 0);
910 hme_write32(hp, bregs + BMAC_LTCTR, 0);
913 /* hp->happy_lock must be held */
914 static void happy_meal_poll_stop(struct happy_meal *hp, void __iomem *tregs)
916 ASD(("happy_meal_poll_stop: "));
918 /* If polling disabled or not polling already, nothing to do. */
919 if ((hp->happy_flags & (HFLAG_POLLENABLE | HFLAG_POLL)) !=
920 (HFLAG_POLLENABLE | HFLAG_POLL)) {
921 HMD(("not polling, return\n"));
925 /* Shut up the MIF. */
926 ASD(("were polling, mif ints off, "));
927 hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
929 /* Turn off polling. */
930 ASD(("polling off, "));
931 hme_write32(hp, tregs + TCVR_CFG,
932 hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_PENABLE));
934 /* We are no longer polling. */
935 hp->happy_flags &= ~(HFLAG_POLL);
937 /* Let the bits set. */
942 /* Only Sun can take such nice parts and fuck up the programming interface
943 * like this. Good job guys...
945 #define TCVR_RESET_TRIES 16 /* It should reset quickly */
946 #define TCVR_UNISOLATE_TRIES 32 /* Dis-isolation can take longer. */
948 /* hp->happy_lock must be held */
949 static int happy_meal_tcvr_reset(struct happy_meal *hp, void __iomem *tregs)
952 int result, tries = TCVR_RESET_TRIES;
954 tconfig = hme_read32(hp, tregs + TCVR_CFG);
955 ASD(("happy_meal_tcvr_reset: tcfg<%08lx> ", tconfig));
956 if (hp->tcvr_type == external) {
958 hme_write32(hp, tregs + TCVR_CFG, tconfig & ~(TCV_CFG_PSELECT));
959 hp->tcvr_type = internal;
960 hp->paddr = TCV_PADDR_ITX;
962 happy_meal_tcvr_write(hp, tregs, MII_BMCR,
963 (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
964 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
965 if (result == TCVR_FAILURE) {
966 ASD(("phyread_fail>\n"));
969 ASD(("phyread_ok,PSELECT>"));
970 hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
971 hp->tcvr_type = external;
972 hp->paddr = TCV_PADDR_ETX;
974 if (tconfig & TCV_CFG_MDIO1) {
975 ASD(("internal<PSELECT,"));
976 hme_write32(hp, tregs + TCVR_CFG, (tconfig | TCV_CFG_PSELECT));
978 happy_meal_tcvr_write(hp, tregs, MII_BMCR,
979 (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
980 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
981 if (result == TCVR_FAILURE) {
982 ASD(("phyread_fail>\n"));
985 ASD(("phyread_ok,~PSELECT>"));
986 hme_write32(hp, tregs + TCVR_CFG, (tconfig & ~(TCV_CFG_PSELECT)));
987 hp->tcvr_type = internal;
988 hp->paddr = TCV_PADDR_ITX;
992 ASD(("BMCR_RESET "));
993 happy_meal_tcvr_write(hp, tregs, MII_BMCR, BMCR_RESET);
996 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
997 if (result == TCVR_FAILURE)
999 hp->sw_bmcr = result;
1000 if (!(result & BMCR_RESET))
1005 ASD(("BMCR RESET FAILED!\n"));
1008 ASD(("RESET_OK\n"));
1010 /* Get fresh copies of the PHY registers. */
1011 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1012 hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
1013 hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
1014 hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1017 hp->sw_bmcr &= ~(BMCR_ISOLATE);
1018 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1020 tries = TCVR_UNISOLATE_TRIES;
1022 result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1023 if (result == TCVR_FAILURE)
1025 if (!(result & BMCR_ISOLATE))
1030 ASD((" FAILED!\n"));
1033 ASD((" SUCCESS and CSCONFIG_DFBYPASS\n"));
1034 if (!is_lucent_phy(hp)) {
1035 result = happy_meal_tcvr_read(hp, tregs,
1037 happy_meal_tcvr_write(hp, tregs,
1038 DP83840_CSCONFIG, (result | CSCONFIG_DFBYPASS));
1043 /* Figure out whether we have an internal or external transceiver.
1045 * hp->happy_lock must be held
1047 static void happy_meal_transceiver_check(struct happy_meal *hp, void __iomem *tregs)
1049 unsigned long tconfig = hme_read32(hp, tregs + TCVR_CFG);
1051 ASD(("happy_meal_transceiver_check: tcfg=%08lx ", tconfig));
1052 if (hp->happy_flags & HFLAG_POLL) {
1053 /* If we are polling, we must stop to get the transceiver type. */
1054 ASD(("<polling> "));
1055 if (hp->tcvr_type == internal) {
1056 if (tconfig & TCV_CFG_MDIO1) {
1057 ASD(("<internal> <poll stop> "));
1058 happy_meal_poll_stop(hp, tregs);
1059 hp->paddr = TCV_PADDR_ETX;
1060 hp->tcvr_type = external;
1061 ASD(("<external>\n"));
1062 tconfig &= ~(TCV_CFG_PENABLE);
1063 tconfig |= TCV_CFG_PSELECT;
1064 hme_write32(hp, tregs + TCVR_CFG, tconfig);
1067 if (hp->tcvr_type == external) {
1068 ASD(("<external> "));
1069 if (!(hme_read32(hp, tregs + TCVR_STATUS) >> 16)) {
1070 ASD(("<poll stop> "));
1071 happy_meal_poll_stop(hp, tregs);
1072 hp->paddr = TCV_PADDR_ITX;
1073 hp->tcvr_type = internal;
1074 ASD(("<internal>\n"));
1075 hme_write32(hp, tregs + TCVR_CFG,
1076 hme_read32(hp, tregs + TCVR_CFG) &
1077 ~(TCV_CFG_PSELECT));
1085 u32 reread = hme_read32(hp, tregs + TCVR_CFG);
1087 /* Else we can just work off of the MDIO bits. */
1088 ASD(("<not polling> "));
1089 if (reread & TCV_CFG_MDIO1) {
1090 hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
1091 hp->paddr = TCV_PADDR_ETX;
1092 hp->tcvr_type = external;
1093 ASD(("<external>\n"));
1095 if (reread & TCV_CFG_MDIO0) {
1096 hme_write32(hp, tregs + TCVR_CFG,
1097 tconfig & ~(TCV_CFG_PSELECT));
1098 hp->paddr = TCV_PADDR_ITX;
1099 hp->tcvr_type = internal;
1100 ASD(("<internal>\n"));
1102 printk(KERN_ERR "happy meal: Transceiver and a coke please.");
1103 hp->tcvr_type = none; /* Grrr... */
1110 /* The receive ring buffers are a bit tricky to get right. Here goes...
1112 * The buffers we dma into must be 64 byte aligned. So we use a special
1113 * alloc_skb() routine for the happy meal to allocate 64 bytes more than
1116 * We use skb_reserve() to align the data block we get in the skb. We
1117 * also program the etxregs->cfg register to use an offset of 2. This
1118 * imperical constant plus the ethernet header size will always leave
1119 * us with a nicely aligned ip header once we pass things up to the
1122 * The numbers work out to:
1124 * Max ethernet frame size 1518
1125 * Ethernet header size 14
1126 * Happy Meal base offset 2
1128 * Say a skb data area is at 0xf001b010, and its size alloced is
1129 * (ETH_FRAME_LEN + 64 + 2) = (1514 + 64 + 2) = 1580 bytes.
1131 * First our alloc_skb() routine aligns the data base to a 64 byte
1132 * boundary. We now have 0xf001b040 as our skb data address. We
1133 * plug this into the receive descriptor address.
1135 * Next, we skb_reserve() 2 bytes to account for the Happy Meal offset.
1136 * So now the data we will end up looking at starts at 0xf001b042. When
1137 * the packet arrives, we will check out the size received and subtract
1138 * this from the skb->length. Then we just pass the packet up to the
1139 * protocols as is, and allocate a new skb to replace this slot we have
1140 * just received from.
1142 * The ethernet layer will strip the ether header from the front of the
1143 * skb we just sent to it, this leaves us with the ip header sitting
1144 * nicely aligned at 0xf001b050. Also, for tcp and udp packets the
1145 * Happy Meal has even checksummed the tcp/udp data for us. The 16
1146 * bit checksum is obtained from the low bits of the receive descriptor
1149 * skb->csum = rxd->rx_flags & 0xffff;
1150 * skb->ip_summed = CHECKSUM_COMPLETE;
1152 * before sending off the skb to the protocols, and we are good as gold.
1154 static void happy_meal_clean_rings(struct happy_meal *hp)
1158 for (i = 0; i < RX_RING_SIZE; i++) {
1159 if (hp->rx_skbs[i] != NULL) {
1160 struct sk_buff *skb = hp->rx_skbs[i];
1161 struct happy_meal_rxd *rxd;
1164 rxd = &hp->happy_block->happy_meal_rxd[i];
1165 dma_addr = hme_read_desc32(hp, &rxd->rx_addr);
1166 dma_unmap_single(hp->dma_dev, dma_addr,
1167 RX_BUF_ALLOC_SIZE, DMA_FROM_DEVICE);
1168 dev_kfree_skb_any(skb);
1169 hp->rx_skbs[i] = NULL;
1173 for (i = 0; i < TX_RING_SIZE; i++) {
1174 if (hp->tx_skbs[i] != NULL) {
1175 struct sk_buff *skb = hp->tx_skbs[i];
1176 struct happy_meal_txd *txd;
1180 hp->tx_skbs[i] = NULL;
1182 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1183 txd = &hp->happy_block->happy_meal_txd[i];
1184 dma_addr = hme_read_desc32(hp, &txd->tx_addr);
1186 dma_unmap_single(hp->dma_dev, dma_addr,
1187 (hme_read_desc32(hp, &txd->tx_flags)
1191 dma_unmap_page(hp->dma_dev, dma_addr,
1192 (hme_read_desc32(hp, &txd->tx_flags)
1196 if (frag != skb_shinfo(skb)->nr_frags)
1200 dev_kfree_skb_any(skb);
1205 /* hp->happy_lock must be held */
1206 static void happy_meal_init_rings(struct happy_meal *hp)
1208 struct hmeal_init_block *hb = hp->happy_block;
1211 HMD(("happy_meal_init_rings: counters to zero, "));
1212 hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;
1214 /* Free any skippy bufs left around in the rings. */
1216 happy_meal_clean_rings(hp);
1218 /* Now get new skippy bufs for the receive ring. */
1219 HMD(("init rxring, "));
1220 for (i = 0; i < RX_RING_SIZE; i++) {
1221 struct sk_buff *skb;
1224 skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
1226 hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
1229 hp->rx_skbs[i] = skb;
1231 /* Because we reserve afterwards. */
1232 skb_put(skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
1233 mapping = dma_map_single(hp->dma_dev, skb->data, RX_BUF_ALLOC_SIZE,
1235 if (dma_mapping_error(hp->dma_dev, mapping)) {
1236 dev_kfree_skb_any(skb);
1237 hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
1240 hme_write_rxd(hp, &hb->happy_meal_rxd[i],
1241 (RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16)),
1243 skb_reserve(skb, RX_OFFSET);
1246 HMD(("init txring, "));
1247 for (i = 0; i < TX_RING_SIZE; i++)
1248 hme_write_txd(hp, &hb->happy_meal_txd[i], 0, 0);
1253 /* hp->happy_lock must be held */
1255 happy_meal_begin_auto_negotiation(struct happy_meal *hp,
1256 void __iomem *tregs,
1257 const struct ethtool_link_ksettings *ep)
1261 /* Read all of the registers we are interested in now. */
1262 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1263 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1264 hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
1265 hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
1267 /* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */
1269 hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1270 if (!ep || ep->base.autoneg == AUTONEG_ENABLE) {
1271 /* Advertise everything we can support. */
1272 if (hp->sw_bmsr & BMSR_10HALF)
1273 hp->sw_advertise |= (ADVERTISE_10HALF);
1275 hp->sw_advertise &= ~(ADVERTISE_10HALF);
1277 if (hp->sw_bmsr & BMSR_10FULL)
1278 hp->sw_advertise |= (ADVERTISE_10FULL);
1280 hp->sw_advertise &= ~(ADVERTISE_10FULL);
1281 if (hp->sw_bmsr & BMSR_100HALF)
1282 hp->sw_advertise |= (ADVERTISE_100HALF);
1284 hp->sw_advertise &= ~(ADVERTISE_100HALF);
1285 if (hp->sw_bmsr & BMSR_100FULL)
1286 hp->sw_advertise |= (ADVERTISE_100FULL);
1288 hp->sw_advertise &= ~(ADVERTISE_100FULL);
1289 happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
1291 /* XXX Currently no Happy Meal cards I know off support 100BaseT4,
1292 * XXX and this is because the DP83840 does not support it, changes
1293 * XXX would need to be made to the tx/rx logic in the driver as well
1294 * XXX so I completely skip checking for it in the BMSR for now.
1297 #ifdef AUTO_SWITCH_DEBUG
1298 ASD(("%s: Advertising [ ", hp->dev->name));
1299 if (hp->sw_advertise & ADVERTISE_10HALF)
1301 if (hp->sw_advertise & ADVERTISE_10FULL)
1303 if (hp->sw_advertise & ADVERTISE_100HALF)
1305 if (hp->sw_advertise & ADVERTISE_100FULL)
1309 /* Enable Auto-Negotiation, this is usually on already... */
1310 hp->sw_bmcr |= BMCR_ANENABLE;
1311 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1313 /* Restart it to make sure it is going. */
1314 hp->sw_bmcr |= BMCR_ANRESTART;
1315 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1317 /* BMCR_ANRESTART self clears when the process has begun. */
1319 timeout = 64; /* More than enough. */
1321 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1322 if (!(hp->sw_bmcr & BMCR_ANRESTART))
1323 break; /* got it. */
1327 printk(KERN_ERR "%s: Happy Meal would not start auto negotiation "
1328 "BMCR=0x%04x\n", hp->dev->name, hp->sw_bmcr);
1329 printk(KERN_NOTICE "%s: Performing force link detection.\n",
1333 hp->timer_state = arbwait;
1337 /* Force the link up, trying first a particular mode.
1338 * Either we are here at the request of ethtool or
1339 * because the Happy Meal would not start to autoneg.
1342 /* Disable auto-negotiation in BMCR, enable the duplex and
1343 * speed setting, init the timer state machine, and fire it off.
1345 if (!ep || ep->base.autoneg == AUTONEG_ENABLE) {
1346 hp->sw_bmcr = BMCR_SPEED100;
1348 if (ep->base.speed == SPEED_100)
1349 hp->sw_bmcr = BMCR_SPEED100;
1352 if (ep->base.duplex == DUPLEX_FULL)
1353 hp->sw_bmcr |= BMCR_FULLDPLX;
1355 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1357 if (!is_lucent_phy(hp)) {
1358 /* OK, seems we need do disable the transceiver for the first
1359 * tick to make sure we get an accurate link state at the
1362 hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
1364 hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
1365 happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
1368 hp->timer_state = ltrywait;
1371 hp->timer_ticks = 0;
1372 hp->happy_timer.expires = jiffies + (12 * HZ)/10; /* 1.2 sec. */
1373 add_timer(&hp->happy_timer);
1376 /* hp->happy_lock must be held */
1377 static int happy_meal_init(struct happy_meal *hp)
1379 const unsigned char *e = &hp->dev->dev_addr[0];
1380 void __iomem *gregs = hp->gregs;
1381 void __iomem *etxregs = hp->etxregs;
1382 void __iomem *erxregs = hp->erxregs;
1383 void __iomem *bregs = hp->bigmacregs;
1384 void __iomem *tregs = hp->tcvregs;
1387 /* If auto-negotiation timer is running, kill it. */
1388 del_timer(&hp->happy_timer);
1390 HMD(("happy_meal_init: happy_flags[%08x] ",
1392 if (!(hp->happy_flags & HFLAG_INIT)) {
1393 HMD(("set HFLAG_INIT, "));
1394 hp->happy_flags |= HFLAG_INIT;
1395 happy_meal_get_counters(hp, bregs);
1399 HMD(("to happy_meal_poll_stop\n"));
1400 happy_meal_poll_stop(hp, tregs);
1402 /* Stop transmitter and receiver. */
1403 HMD(("happy_meal_init: to happy_meal_stop\n"));
1404 happy_meal_stop(hp, gregs);
1406 /* Alloc and reset the tx/rx descriptor chains. */
1407 HMD(("happy_meal_init: to happy_meal_init_rings\n"));
1408 happy_meal_init_rings(hp);
1410 /* Shut up the MIF. */
1411 HMD(("happy_meal_init: Disable all MIF irqs (old[%08x]), ",
1412 hme_read32(hp, tregs + TCVR_IMASK)));
1413 hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
1415 /* See if we can enable the MIF frame on this card to speak to the DP83840. */
1416 if (hp->happy_flags & HFLAG_FENABLE) {
1417 HMD(("use frame old[%08x], ",
1418 hme_read32(hp, tregs + TCVR_CFG)));
1419 hme_write32(hp, tregs + TCVR_CFG,
1420 hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
1422 HMD(("use bitbang old[%08x], ",
1423 hme_read32(hp, tregs + TCVR_CFG)));
1424 hme_write32(hp, tregs + TCVR_CFG,
1425 hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
1428 /* Check the state of the transceiver. */
1429 HMD(("to happy_meal_transceiver_check\n"));
1430 happy_meal_transceiver_check(hp, tregs);
1432 /* Put the Big Mac into a sane state. */
1433 HMD(("happy_meal_init: "));
1434 switch(hp->tcvr_type) {
1436 /* Cannot operate if we don't know the transceiver type! */
1437 HMD(("AAIEEE no transceiver type, EAGAIN"));
1441 /* Using the MII buffers. */
1442 HMD(("internal, using MII, "));
1443 hme_write32(hp, bregs + BMAC_XIFCFG, 0);
1447 /* Not using the MII, disable it. */
1448 HMD(("external, disable MII, "));
1449 hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
1453 if (happy_meal_tcvr_reset(hp, tregs))
1456 /* Reset the Happy Meal Big Mac transceiver and the receiver. */
1457 HMD(("tx/rx reset, "));
1458 happy_meal_tx_reset(hp, bregs);
1459 happy_meal_rx_reset(hp, bregs);
1461 /* Set jam size and inter-packet gaps to reasonable defaults. */
1462 HMD(("jsize/ipg1/ipg2, "));
1463 hme_write32(hp, bregs + BMAC_JSIZE, DEFAULT_JAMSIZE);
1464 hme_write32(hp, bregs + BMAC_IGAP1, DEFAULT_IPG1);
1465 hme_write32(hp, bregs + BMAC_IGAP2, DEFAULT_IPG2);
1467 /* Load up the MAC address and random seed. */
1468 HMD(("rseed/macaddr, "));
1470 /* The docs recommend to use the 10LSB of our MAC here. */
1471 hme_write32(hp, bregs + BMAC_RSEED, ((e[5] | e[4]<<8)&0x3ff));
1473 hme_write32(hp, bregs + BMAC_MACADDR2, ((e[4] << 8) | e[5]));
1474 hme_write32(hp, bregs + BMAC_MACADDR1, ((e[2] << 8) | e[3]));
1475 hme_write32(hp, bregs + BMAC_MACADDR0, ((e[0] << 8) | e[1]));
1478 if ((hp->dev->flags & IFF_ALLMULTI) ||
1479 (netdev_mc_count(hp->dev) > 64)) {
1480 hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
1481 hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
1482 hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
1483 hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
1484 } else if ((hp->dev->flags & IFF_PROMISC) == 0) {
1486 struct netdev_hw_addr *ha;
1489 memset(hash_table, 0, sizeof(hash_table));
1490 netdev_for_each_mc_addr(ha, hp->dev) {
1491 crc = ether_crc_le(6, ha->addr);
1493 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1495 hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
1496 hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
1497 hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
1498 hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
1500 hme_write32(hp, bregs + BMAC_HTABLE3, 0);
1501 hme_write32(hp, bregs + BMAC_HTABLE2, 0);
1502 hme_write32(hp, bregs + BMAC_HTABLE1, 0);
1503 hme_write32(hp, bregs + BMAC_HTABLE0, 0);
1506 /* Set the RX and TX ring ptrs. */
1507 HMD(("ring ptrs rxr[%08x] txr[%08x]\n",
1508 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)),
1509 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0))));
1510 hme_write32(hp, erxregs + ERX_RING,
1511 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)));
1512 hme_write32(hp, etxregs + ETX_RING,
1513 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));
1515 /* Parity issues in the ERX unit of some HME revisions can cause some
1516 * registers to not be written unless their parity is even. Detect such
1517 * lost writes and simply rewrite with a low bit set (which will be ignored
1518 * since the rxring needs to be 2K aligned).
1520 if (hme_read32(hp, erxregs + ERX_RING) !=
1521 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)))
1522 hme_write32(hp, erxregs + ERX_RING,
1523 ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0))
1526 /* Set the supported burst sizes. */
1527 HMD(("happy_meal_init: old[%08x] bursts<",
1528 hme_read32(hp, gregs + GREG_CFG)));
1530 #ifndef CONFIG_SPARC
1531 /* It is always PCI and can handle 64byte bursts. */
1532 hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST64);
1534 if ((hp->happy_bursts & DMA_BURST64) &&
1535 ((hp->happy_flags & HFLAG_PCI) != 0
1537 || sbus_can_burst64()
1540 u32 gcfg = GREG_CFG_BURST64;
1542 /* I have no idea if I should set the extended
1543 * transfer mode bit for Cheerio, so for now I
1547 if ((hp->happy_flags & HFLAG_PCI) == 0) {
1548 struct platform_device *op = hp->happy_dev;
1549 if (sbus_can_dma_64bit()) {
1550 sbus_set_sbus64(&op->dev,
1552 gcfg |= GREG_CFG_64BIT;
1558 hme_write32(hp, gregs + GREG_CFG, gcfg);
1559 } else if (hp->happy_bursts & DMA_BURST32) {
1561 hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST32);
1562 } else if (hp->happy_bursts & DMA_BURST16) {
1564 hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST16);
1567 hme_write32(hp, gregs + GREG_CFG, 0);
1569 #endif /* CONFIG_SPARC */
1571 /* Turn off interrupts we do not want to hear. */
1572 HMD((", enable global interrupts, "));
1573 hme_write32(hp, gregs + GREG_IMASK,
1574 (GREG_IMASK_GOTFRAME | GREG_IMASK_RCNTEXP |
1575 GREG_IMASK_SENTFRAME | GREG_IMASK_TXPERR));
1577 /* Set the transmit ring buffer size. */
1578 HMD(("tx rsize=%d oreg[%08x], ", (int)TX_RING_SIZE,
1579 hme_read32(hp, etxregs + ETX_RSIZE)));
1580 hme_write32(hp, etxregs + ETX_RSIZE, (TX_RING_SIZE >> ETX_RSIZE_SHIFT) - 1);
1582 /* Enable transmitter DVMA. */
1583 HMD(("tx dma enable old[%08x], ",
1584 hme_read32(hp, etxregs + ETX_CFG)));
1585 hme_write32(hp, etxregs + ETX_CFG,
1586 hme_read32(hp, etxregs + ETX_CFG) | ETX_CFG_DMAENABLE);
1588 /* This chip really rots, for the receiver sometimes when you
1589 * write to its control registers not all the bits get there
1590 * properly. I cannot think of a sane way to provide complete
1591 * coverage for this hardware bug yet.
1593 HMD(("erx regs bug old[%08x]\n",
1594 hme_read32(hp, erxregs + ERX_CFG)));
1595 hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
1596 regtmp = hme_read32(hp, erxregs + ERX_CFG);
1597 hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
1598 if (hme_read32(hp, erxregs + ERX_CFG) != ERX_CFG_DEFAULT(RX_OFFSET)) {
1599 printk(KERN_ERR "happy meal: Eieee, rx config register gets greasy fries.\n");
1600 printk(KERN_ERR "happy meal: Trying to set %08x, reread gives %08x\n",
1601 ERX_CFG_DEFAULT(RX_OFFSET), regtmp);
1602 /* XXX Should return failure here... */
1605 /* Enable Big Mac hash table filter. */
1606 HMD(("happy_meal_init: enable hash rx_cfg_old[%08x], ",
1607 hme_read32(hp, bregs + BMAC_RXCFG)));
1608 rxcfg = BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_REJME;
1609 if (hp->dev->flags & IFF_PROMISC)
1610 rxcfg |= BIGMAC_RXCFG_PMISC;
1611 hme_write32(hp, bregs + BMAC_RXCFG, rxcfg);
1613 /* Let the bits settle in the chip. */
1616 /* Ok, configure the Big Mac transmitter. */
1617 HMD(("BIGMAC init, "));
1619 if (hp->happy_flags & HFLAG_FULL)
1620 regtmp |= BIGMAC_TXCFG_FULLDPLX;
1622 /* Don't turn on the "don't give up" bit for now. It could cause hme
1623 * to deadlock with the PHY if a Jabber occurs.
1625 hme_write32(hp, bregs + BMAC_TXCFG, regtmp /*| BIGMAC_TXCFG_DGIVEUP*/);
1627 /* Give up after 16 TX attempts. */
1628 hme_write32(hp, bregs + BMAC_ALIMIT, 16);
1630 /* Enable the output drivers no matter what. */
1631 regtmp = BIGMAC_XCFG_ODENABLE;
1633 /* If card can do lance mode, enable it. */
1634 if (hp->happy_flags & HFLAG_LANCE)
1635 regtmp |= (DEFAULT_IPG0 << 5) | BIGMAC_XCFG_LANCE;
1637 /* Disable the MII buffers if using external transceiver. */
1638 if (hp->tcvr_type == external)
1639 regtmp |= BIGMAC_XCFG_MIIDISAB;
1641 HMD(("XIF config old[%08x], ",
1642 hme_read32(hp, bregs + BMAC_XIFCFG)));
1643 hme_write32(hp, bregs + BMAC_XIFCFG, regtmp);
1645 /* Start things up. */
1646 HMD(("tx old[%08x] and rx [%08x] ON!\n",
1647 hme_read32(hp, bregs + BMAC_TXCFG),
1648 hme_read32(hp, bregs + BMAC_RXCFG)));
1650 /* Set larger TX/RX size to allow for 802.1q */
1651 hme_write32(hp, bregs + BMAC_TXMAX, ETH_FRAME_LEN + 8);
1652 hme_write32(hp, bregs + BMAC_RXMAX, ETH_FRAME_LEN + 8);
1654 hme_write32(hp, bregs + BMAC_TXCFG,
1655 hme_read32(hp, bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE);
1656 hme_write32(hp, bregs + BMAC_RXCFG,
1657 hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE);
1659 /* Get the autonegotiation started, and the watch timer ticking. */
1660 happy_meal_begin_auto_negotiation(hp, tregs, NULL);
1666 /* hp->happy_lock must be held */
1667 static void happy_meal_set_initial_advertisement(struct happy_meal *hp)
1669 void __iomem *tregs = hp->tcvregs;
1670 void __iomem *bregs = hp->bigmacregs;
1671 void __iomem *gregs = hp->gregs;
1673 happy_meal_stop(hp, gregs);
1674 hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
1675 if (hp->happy_flags & HFLAG_FENABLE)
1676 hme_write32(hp, tregs + TCVR_CFG,
1677 hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
1679 hme_write32(hp, tregs + TCVR_CFG,
1680 hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
1681 happy_meal_transceiver_check(hp, tregs);
1682 switch(hp->tcvr_type) {
1686 hme_write32(hp, bregs + BMAC_XIFCFG, 0);
1689 hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
1692 if (happy_meal_tcvr_reset(hp, tregs))
1695 /* Latch PHY registers as of now. */
1696 hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1697 hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1699 /* Advertise everything we can support. */
1700 if (hp->sw_bmsr & BMSR_10HALF)
1701 hp->sw_advertise |= (ADVERTISE_10HALF);
1703 hp->sw_advertise &= ~(ADVERTISE_10HALF);
1705 if (hp->sw_bmsr & BMSR_10FULL)
1706 hp->sw_advertise |= (ADVERTISE_10FULL);
1708 hp->sw_advertise &= ~(ADVERTISE_10FULL);
1709 if (hp->sw_bmsr & BMSR_100HALF)
1710 hp->sw_advertise |= (ADVERTISE_100HALF);
1712 hp->sw_advertise &= ~(ADVERTISE_100HALF);
1713 if (hp->sw_bmsr & BMSR_100FULL)
1714 hp->sw_advertise |= (ADVERTISE_100FULL);
1716 hp->sw_advertise &= ~(ADVERTISE_100FULL);
1718 /* Update the PHY advertisement register. */
1719 happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
1722 /* Once status is latched (by happy_meal_interrupt) it is cleared by
1723 * the hardware, so we cannot re-read it and get a correct value.
1725 * hp->happy_lock must be held
1727 static int happy_meal_is_not_so_happy(struct happy_meal *hp, u32 status)
1731 /* Only print messages for non-counter related interrupts. */
1732 if (status & (GREG_STAT_STSTERR | GREG_STAT_TFIFO_UND |
1733 GREG_STAT_MAXPKTERR | GREG_STAT_RXERR |
1734 GREG_STAT_RXPERR | GREG_STAT_RXTERR | GREG_STAT_EOPERR |
1735 GREG_STAT_MIFIRQ | GREG_STAT_TXEACK | GREG_STAT_TXLERR |
1736 GREG_STAT_TXPERR | GREG_STAT_TXTERR | GREG_STAT_SLVERR |
1738 printk(KERN_ERR "%s: Error interrupt for happy meal, status = %08x\n",
1739 hp->dev->name, status);
1741 if (status & GREG_STAT_RFIFOVF) {
1742 /* Receive FIFO overflow is harmless and the hardware will take
1743 care of it, just some packets are lost. Who cares. */
1744 printk(KERN_DEBUG "%s: Happy Meal receive FIFO overflow.\n", hp->dev->name);
1747 if (status & GREG_STAT_STSTERR) {
1748 /* BigMAC SQE link test failed. */
1749 printk(KERN_ERR "%s: Happy Meal BigMAC SQE test failed.\n", hp->dev->name);
1753 if (status & GREG_STAT_TFIFO_UND) {
1754 /* Transmit FIFO underrun, again DMA error likely. */
1755 printk(KERN_ERR "%s: Happy Meal transmitter FIFO underrun, DMA error.\n",
1760 if (status & GREG_STAT_MAXPKTERR) {
1761 /* Driver error, tried to transmit something larger
1762 * than ethernet max mtu.
1764 printk(KERN_ERR "%s: Happy Meal MAX Packet size error.\n", hp->dev->name);
1768 if (status & GREG_STAT_NORXD) {
1769 /* This is harmless, it just means the system is
1770 * quite loaded and the incoming packet rate was
1771 * faster than the interrupt handler could keep up
1774 printk(KERN_INFO "%s: Happy Meal out of receive "
1775 "descriptors, packet dropped.\n",
1779 if (status & (GREG_STAT_RXERR|GREG_STAT_RXPERR|GREG_STAT_RXTERR)) {
1780 /* All sorts of DMA receive errors. */
1781 printk(KERN_ERR "%s: Happy Meal rx DMA errors [ ", hp->dev->name);
1782 if (status & GREG_STAT_RXERR)
1783 printk("GenericError ");
1784 if (status & GREG_STAT_RXPERR)
1785 printk("ParityError ");
1786 if (status & GREG_STAT_RXTERR)
1787 printk("RxTagBotch ");
1792 if (status & GREG_STAT_EOPERR) {
1793 /* Driver bug, didn't set EOP bit in tx descriptor given
1794 * to the happy meal.
1796 printk(KERN_ERR "%s: EOP not set in happy meal transmit descriptor!\n",
1801 if (status & GREG_STAT_MIFIRQ) {
1802 /* MIF signalled an interrupt, were we polling it? */
1803 printk(KERN_ERR "%s: Happy Meal MIF interrupt.\n", hp->dev->name);
1807 (GREG_STAT_TXEACK|GREG_STAT_TXLERR|GREG_STAT_TXPERR|GREG_STAT_TXTERR)) {
1808 /* All sorts of transmit DMA errors. */
1809 printk(KERN_ERR "%s: Happy Meal tx DMA errors [ ", hp->dev->name);
1810 if (status & GREG_STAT_TXEACK)
1811 printk("GenericError ");
1812 if (status & GREG_STAT_TXLERR)
1813 printk("LateError ");
1814 if (status & GREG_STAT_TXPERR)
1815 printk("ParityError ");
1816 if (status & GREG_STAT_TXTERR)
1817 printk("TagBotch ");
1822 if (status & (GREG_STAT_SLVERR|GREG_STAT_SLVPERR)) {
1823 /* Bus or parity error when cpu accessed happy meal registers
1824 * or it's internal FIFO's. Should never see this.
1826 printk(KERN_ERR "%s: Happy Meal register access SBUS slave (%s) error.\n",
1828 (status & GREG_STAT_SLVPERR) ? "parity" : "generic");
1833 printk(KERN_NOTICE "%s: Resetting...\n", hp->dev->name);
1834 happy_meal_init(hp);
1840 /* hp->happy_lock must be held */
1841 static void happy_meal_mif_interrupt(struct happy_meal *hp)
1843 void __iomem *tregs = hp->tcvregs;
1845 printk(KERN_INFO "%s: Link status change.\n", hp->dev->name);
1846 hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1847 hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
1849 /* Use the fastest transmission protocol possible. */
1850 if (hp->sw_lpa & LPA_100FULL) {
1851 printk(KERN_INFO "%s: Switching to 100Mbps at full duplex.", hp->dev->name);
1852 hp->sw_bmcr |= (BMCR_FULLDPLX | BMCR_SPEED100);
1853 } else if (hp->sw_lpa & LPA_100HALF) {
1854 printk(KERN_INFO "%s: Switching to 100MBps at half duplex.", hp->dev->name);
1855 hp->sw_bmcr |= BMCR_SPEED100;
1856 } else if (hp->sw_lpa & LPA_10FULL) {
1857 printk(KERN_INFO "%s: Switching to 10MBps at full duplex.", hp->dev->name);
1858 hp->sw_bmcr |= BMCR_FULLDPLX;
1860 printk(KERN_INFO "%s: Using 10Mbps at half duplex.", hp->dev->name);
1862 happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1864 /* Finally stop polling and shut up the MIF. */
1865 happy_meal_poll_stop(hp, tregs);
1869 #define TXD(x) printk x
1874 /* hp->happy_lock must be held */
1875 static void happy_meal_tx(struct happy_meal *hp)
1877 struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
1878 struct happy_meal_txd *this;
1879 struct net_device *dev = hp->dev;
1884 while (elem != hp->tx_new) {
1885 struct sk_buff *skb;
1886 u32 flags, dma_addr, dma_len;
1889 TXD(("[%d]", elem));
1890 this = &txbase[elem];
1891 flags = hme_read_desc32(hp, &this->tx_flags);
1892 if (flags & TXFLAG_OWN)
1894 skb = hp->tx_skbs[elem];
1895 if (skb_shinfo(skb)->nr_frags) {
1898 last = elem + skb_shinfo(skb)->nr_frags;
1899 last &= (TX_RING_SIZE - 1);
1900 flags = hme_read_desc32(hp, &txbase[last].tx_flags);
1901 if (flags & TXFLAG_OWN)
1904 hp->tx_skbs[elem] = NULL;
1905 dev->stats.tx_bytes += skb->len;
1907 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1908 dma_addr = hme_read_desc32(hp, &this->tx_addr);
1909 dma_len = hme_read_desc32(hp, &this->tx_flags);
1911 dma_len &= TXFLAG_SIZE;
1913 dma_unmap_single(hp->dma_dev, dma_addr, dma_len, DMA_TO_DEVICE);
1915 dma_unmap_page(hp->dma_dev, dma_addr, dma_len, DMA_TO_DEVICE);
1917 elem = NEXT_TX(elem);
1918 this = &txbase[elem];
1921 dev_consume_skb_irq(skb);
1922 dev->stats.tx_packets++;
1927 if (netif_queue_stopped(dev) &&
1928 TX_BUFFS_AVAIL(hp) > (MAX_SKB_FRAGS + 1))
1929 netif_wake_queue(dev);
1933 #define RXD(x) printk x
1938 /* Originally I used to handle the allocation failure by just giving back just
1939 * that one ring buffer to the happy meal. Problem is that usually when that
1940 * condition is triggered, the happy meal expects you to do something reasonable
1941 * with all of the packets it has DMA'd in. So now I just drop the entire
1942 * ring when we cannot get a new skb and give them all back to the happy meal,
1943 * maybe things will be "happier" now.
1945 * hp->happy_lock must be held
1947 static void happy_meal_rx(struct happy_meal *hp, struct net_device *dev)
1949 struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
1950 struct happy_meal_rxd *this;
1951 int elem = hp->rx_new, drops = 0;
1955 this = &rxbase[elem];
1956 while (!((flags = hme_read_desc32(hp, &this->rx_flags)) & RXFLAG_OWN)) {
1957 struct sk_buff *skb;
1958 int len = flags >> 16;
1959 u16 csum = flags & RXFLAG_CSUM;
1960 u32 dma_addr = hme_read_desc32(hp, &this->rx_addr);
1962 RXD(("[%d ", elem));
1964 /* Check for errors. */
1965 if ((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
1966 RXD(("ERR(%08x)]", flags));
1967 dev->stats.rx_errors++;
1969 dev->stats.rx_length_errors++;
1970 if (len & (RXFLAG_OVERFLOW >> 16)) {
1971 dev->stats.rx_over_errors++;
1972 dev->stats.rx_fifo_errors++;
1975 /* Return it to the Happy meal. */
1977 dev->stats.rx_dropped++;
1978 hme_write_rxd(hp, this,
1979 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
1983 skb = hp->rx_skbs[elem];
1984 if (len > RX_COPY_THRESHOLD) {
1985 struct sk_buff *new_skb;
1988 /* Now refill the entry, if we can. */
1989 new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
1990 if (new_skb == NULL) {
1994 skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
1995 mapping = dma_map_single(hp->dma_dev, new_skb->data,
1998 if (unlikely(dma_mapping_error(hp->dma_dev, mapping))) {
1999 dev_kfree_skb_any(new_skb);
2004 dma_unmap_single(hp->dma_dev, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROM_DEVICE);
2005 hp->rx_skbs[elem] = new_skb;
2006 hme_write_rxd(hp, this,
2007 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
2009 skb_reserve(new_skb, RX_OFFSET);
2011 /* Trim the original skb for the netif. */
2014 struct sk_buff *copy_skb = netdev_alloc_skb(dev, len + 2);
2016 if (copy_skb == NULL) {
2021 skb_reserve(copy_skb, 2);
2022 skb_put(copy_skb, len);
2023 dma_sync_single_for_cpu(hp->dma_dev, dma_addr, len + 2, DMA_FROM_DEVICE);
2024 skb_copy_from_linear_data(skb, copy_skb->data, len);
2025 dma_sync_single_for_device(hp->dma_dev, dma_addr, len + 2, DMA_FROM_DEVICE);
2026 /* Reuse original ring buffer. */
2027 hme_write_rxd(hp, this,
2028 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
2034 /* This card is _fucking_ hot... */
2035 skb->csum = csum_unfold(~(__force __sum16)htons(csum));
2036 skb->ip_summed = CHECKSUM_COMPLETE;
2038 RXD(("len=%d csum=%4x]", len, csum));
2039 skb->protocol = eth_type_trans(skb, dev);
2042 dev->stats.rx_packets++;
2043 dev->stats.rx_bytes += len;
2045 elem = NEXT_RX(elem);
2046 this = &rxbase[elem];
2050 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
2054 static irqreturn_t happy_meal_interrupt(int irq, void *dev_id)
2056 struct net_device *dev = dev_id;
2057 struct happy_meal *hp = netdev_priv(dev);
2058 u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
2060 HMD(("happy_meal_interrupt: status=%08x ", happy_status));
2062 spin_lock(&hp->happy_lock);
2064 if (happy_status & GREG_STAT_ERRORS) {
2066 if (happy_meal_is_not_so_happy(hp, /* un- */ happy_status))
2070 if (happy_status & GREG_STAT_MIFIRQ) {
2072 happy_meal_mif_interrupt(hp);
2075 if (happy_status & GREG_STAT_TXALL) {
2080 if (happy_status & GREG_STAT_RXTOHOST) {
2082 happy_meal_rx(hp, dev);
2087 spin_unlock(&hp->happy_lock);
2093 static irqreturn_t quattro_sbus_interrupt(int irq, void *cookie)
2095 struct quattro *qp = (struct quattro *) cookie;
2098 for (i = 0; i < 4; i++) {
2099 struct net_device *dev = qp->happy_meals[i];
2100 struct happy_meal *hp = netdev_priv(dev);
2101 u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
2103 HMD(("quattro_interrupt: status=%08x ", happy_status));
2105 if (!(happy_status & (GREG_STAT_ERRORS |
2108 GREG_STAT_RXTOHOST)))
2111 spin_lock(&hp->happy_lock);
2113 if (happy_status & GREG_STAT_ERRORS) {
2115 if (happy_meal_is_not_so_happy(hp, happy_status))
2119 if (happy_status & GREG_STAT_MIFIRQ) {
2121 happy_meal_mif_interrupt(hp);
2124 if (happy_status & GREG_STAT_TXALL) {
2129 if (happy_status & GREG_STAT_RXTOHOST) {
2131 happy_meal_rx(hp, dev);
2135 spin_unlock(&hp->happy_lock);
2143 static int happy_meal_open(struct net_device *dev)
2145 struct happy_meal *hp = netdev_priv(dev);
2148 HMD(("happy_meal_open: "));
2150 /* On SBUS Quattro QFE cards, all hme interrupts are concentrated
2151 * into a single source which we register handling at probe time.
2153 if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO) {
2154 res = request_irq(hp->irq, happy_meal_interrupt, IRQF_SHARED,
2158 printk(KERN_ERR "happy_meal(SBUS): Can't order irq %d to go.\n",
2165 HMD(("to happy_meal_init\n"));
2167 spin_lock_irq(&hp->happy_lock);
2168 res = happy_meal_init(hp);
2169 spin_unlock_irq(&hp->happy_lock);
2171 if (res && ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO))
2172 free_irq(hp->irq, dev);
2176 static int happy_meal_close(struct net_device *dev)
2178 struct happy_meal *hp = netdev_priv(dev);
2180 spin_lock_irq(&hp->happy_lock);
2181 happy_meal_stop(hp, hp->gregs);
2182 happy_meal_clean_rings(hp);
2184 /* If auto-negotiation timer is running, kill it. */
2185 del_timer(&hp->happy_timer);
2187 spin_unlock_irq(&hp->happy_lock);
2189 /* On Quattro QFE cards, all hme interrupts are concentrated
2190 * into a single source which we register handling at probe
2191 * time and never unregister.
2193 if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO)
2194 free_irq(hp->irq, dev);
2200 #define SXD(x) printk x
2205 static void happy_meal_tx_timeout(struct net_device *dev, unsigned int txqueue)
2207 struct happy_meal *hp = netdev_priv(dev);
2209 printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
2211 printk (KERN_ERR "%s: Happy Status %08x TX[%08x:%08x]\n", dev->name,
2212 hme_read32(hp, hp->gregs + GREG_STAT),
2213 hme_read32(hp, hp->etxregs + ETX_CFG),
2214 hme_read32(hp, hp->bigmacregs + BMAC_TXCFG));
2216 spin_lock_irq(&hp->happy_lock);
2217 happy_meal_init(hp);
2218 spin_unlock_irq(&hp->happy_lock);
2220 netif_wake_queue(dev);
2223 static void unmap_partial_tx_skb(struct happy_meal *hp, u32 first_mapping,
2224 u32 first_len, u32 first_entry, u32 entry)
2226 struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
2228 dma_unmap_single(hp->dma_dev, first_mapping, first_len, DMA_TO_DEVICE);
2230 first_entry = NEXT_TX(first_entry);
2231 while (first_entry != entry) {
2232 struct happy_meal_txd *this = &txbase[first_entry];
2235 addr = hme_read_desc32(hp, &this->tx_addr);
2236 len = hme_read_desc32(hp, &this->tx_flags);
2238 dma_unmap_page(hp->dma_dev, addr, len, DMA_TO_DEVICE);
2242 static netdev_tx_t happy_meal_start_xmit(struct sk_buff *skb,
2243 struct net_device *dev)
2245 struct happy_meal *hp = netdev_priv(dev);
2249 tx_flags = TXFLAG_OWN;
2250 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2251 const u32 csum_start_off = skb_checksum_start_offset(skb);
2252 const u32 csum_stuff_off = csum_start_off + skb->csum_offset;
2254 tx_flags = (TXFLAG_OWN | TXFLAG_CSENABLE |
2255 ((csum_start_off << 14) & TXFLAG_CSBUFBEGIN) |
2256 ((csum_stuff_off << 20) & TXFLAG_CSLOCATION));
2259 spin_lock_irq(&hp->happy_lock);
2261 if (TX_BUFFS_AVAIL(hp) <= (skb_shinfo(skb)->nr_frags + 1)) {
2262 netif_stop_queue(dev);
2263 spin_unlock_irq(&hp->happy_lock);
2264 printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
2266 return NETDEV_TX_BUSY;
2270 SXD(("SX<l[%d]e[%d]>", len, entry));
2271 hp->tx_skbs[entry] = skb;
2273 if (skb_shinfo(skb)->nr_frags == 0) {
2277 mapping = dma_map_single(hp->dma_dev, skb->data, len, DMA_TO_DEVICE);
2278 if (unlikely(dma_mapping_error(hp->dma_dev, mapping)))
2280 tx_flags |= (TXFLAG_SOP | TXFLAG_EOP);
2281 hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
2282 (tx_flags | (len & TXFLAG_SIZE)),
2284 entry = NEXT_TX(entry);
2286 u32 first_len, first_mapping;
2287 int frag, first_entry = entry;
2289 /* We must give this initial chunk to the device last.
2290 * Otherwise we could race with the device.
2292 first_len = skb_headlen(skb);
2293 first_mapping = dma_map_single(hp->dma_dev, skb->data, first_len,
2295 if (unlikely(dma_mapping_error(hp->dma_dev, first_mapping)))
2297 entry = NEXT_TX(entry);
2299 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
2300 const skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
2301 u32 len, mapping, this_txflags;
2303 len = skb_frag_size(this_frag);
2304 mapping = skb_frag_dma_map(hp->dma_dev, this_frag,
2305 0, len, DMA_TO_DEVICE);
2306 if (unlikely(dma_mapping_error(hp->dma_dev, mapping))) {
2307 unmap_partial_tx_skb(hp, first_mapping, first_len,
2308 first_entry, entry);
2311 this_txflags = tx_flags;
2312 if (frag == skb_shinfo(skb)->nr_frags - 1)
2313 this_txflags |= TXFLAG_EOP;
2314 hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
2315 (this_txflags | (len & TXFLAG_SIZE)),
2317 entry = NEXT_TX(entry);
2319 hme_write_txd(hp, &hp->happy_block->happy_meal_txd[first_entry],
2320 (tx_flags | TXFLAG_SOP | (first_len & TXFLAG_SIZE)),
2326 if (TX_BUFFS_AVAIL(hp) <= (MAX_SKB_FRAGS + 1))
2327 netif_stop_queue(dev);
2330 hme_write32(hp, hp->etxregs + ETX_PENDING, ETX_TP_DMAWAKEUP);
2332 spin_unlock_irq(&hp->happy_lock);
2334 tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
2335 return NETDEV_TX_OK;
2338 hp->tx_skbs[hp->tx_new] = NULL;
2339 spin_unlock_irq(&hp->happy_lock);
2341 dev_kfree_skb_any(skb);
2342 dev->stats.tx_dropped++;
2343 return NETDEV_TX_OK;
2346 static struct net_device_stats *happy_meal_get_stats(struct net_device *dev)
2348 struct happy_meal *hp = netdev_priv(dev);
2350 spin_lock_irq(&hp->happy_lock);
2351 happy_meal_get_counters(hp, hp->bigmacregs);
2352 spin_unlock_irq(&hp->happy_lock);
2357 static void happy_meal_set_multicast(struct net_device *dev)
2359 struct happy_meal *hp = netdev_priv(dev);
2360 void __iomem *bregs = hp->bigmacregs;
2361 struct netdev_hw_addr *ha;
2364 spin_lock_irq(&hp->happy_lock);
2366 if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
2367 hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
2368 hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
2369 hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
2370 hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
2371 } else if (dev->flags & IFF_PROMISC) {
2372 hme_write32(hp, bregs + BMAC_RXCFG,
2373 hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_PMISC);
2377 memset(hash_table, 0, sizeof(hash_table));
2378 netdev_for_each_mc_addr(ha, dev) {
2379 crc = ether_crc_le(6, ha->addr);
2381 hash_table[crc >> 4] |= 1 << (crc & 0xf);
2383 hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
2384 hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
2385 hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
2386 hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
2389 spin_unlock_irq(&hp->happy_lock);
2392 /* Ethtool support... */
2393 static int hme_get_link_ksettings(struct net_device *dev,
2394 struct ethtool_link_ksettings *cmd)
2396 struct happy_meal *hp = netdev_priv(dev);
2401 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2402 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2403 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
2405 /* XXX hardcoded stuff for now */
2406 cmd->base.port = PORT_TP; /* XXX no MII support */
2407 cmd->base.phy_address = 0; /* XXX fixed PHYAD */
2409 /* Record PHY settings. */
2410 spin_lock_irq(&hp->happy_lock);
2411 hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
2412 hp->sw_lpa = happy_meal_tcvr_read(hp, hp->tcvregs, MII_LPA);
2413 spin_unlock_irq(&hp->happy_lock);
2415 if (hp->sw_bmcr & BMCR_ANENABLE) {
2416 cmd->base.autoneg = AUTONEG_ENABLE;
2417 speed = ((hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) ?
2418 SPEED_100 : SPEED_10);
2419 if (speed == SPEED_100)
2421 (hp->sw_lpa & (LPA_100FULL)) ?
2422 DUPLEX_FULL : DUPLEX_HALF;
2425 (hp->sw_lpa & (LPA_10FULL)) ?
2426 DUPLEX_FULL : DUPLEX_HALF;
2428 cmd->base.autoneg = AUTONEG_DISABLE;
2429 speed = (hp->sw_bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
2431 (hp->sw_bmcr & BMCR_FULLDPLX) ?
2432 DUPLEX_FULL : DUPLEX_HALF;
2434 cmd->base.speed = speed;
2435 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2441 static int hme_set_link_ksettings(struct net_device *dev,
2442 const struct ethtool_link_ksettings *cmd)
2444 struct happy_meal *hp = netdev_priv(dev);
2446 /* Verify the settings we care about. */
2447 if (cmd->base.autoneg != AUTONEG_ENABLE &&
2448 cmd->base.autoneg != AUTONEG_DISABLE)
2450 if (cmd->base.autoneg == AUTONEG_DISABLE &&
2451 ((cmd->base.speed != SPEED_100 &&
2452 cmd->base.speed != SPEED_10) ||
2453 (cmd->base.duplex != DUPLEX_HALF &&
2454 cmd->base.duplex != DUPLEX_FULL)))
2457 /* Ok, do it to it. */
2458 spin_lock_irq(&hp->happy_lock);
2459 del_timer(&hp->happy_timer);
2460 happy_meal_begin_auto_negotiation(hp, hp->tcvregs, cmd);
2461 spin_unlock_irq(&hp->happy_lock);
2466 static void hme_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2468 struct happy_meal *hp = netdev_priv(dev);
2470 strlcpy(info->driver, "sunhme", sizeof(info->driver));
2471 strlcpy(info->version, "2.02", sizeof(info->version));
2472 if (hp->happy_flags & HFLAG_PCI) {
2473 struct pci_dev *pdev = hp->happy_dev;
2474 strlcpy(info->bus_info, pci_name(pdev), sizeof(info->bus_info));
2478 const struct linux_prom_registers *regs;
2479 struct platform_device *op = hp->happy_dev;
2480 regs = of_get_property(op->dev.of_node, "regs", NULL);
2482 snprintf(info->bus_info, sizeof(info->bus_info),
2489 static u32 hme_get_link(struct net_device *dev)
2491 struct happy_meal *hp = netdev_priv(dev);
2493 spin_lock_irq(&hp->happy_lock);
2494 hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
2495 spin_unlock_irq(&hp->happy_lock);
2497 return hp->sw_bmsr & BMSR_LSTATUS;
2500 static const struct ethtool_ops hme_ethtool_ops = {
2501 .get_drvinfo = hme_get_drvinfo,
2502 .get_link = hme_get_link,
2503 .get_link_ksettings = hme_get_link_ksettings,
2504 .set_link_ksettings = hme_set_link_ksettings,
2507 static int hme_version_printed;
2510 /* Given a happy meal sbus device, find it's quattro parent.
2511 * If none exist, allocate and return a new one.
2513 * Return NULL on failure.
2515 static struct quattro *quattro_sbus_find(struct platform_device *child)
2517 struct device *parent = child->dev.parent;
2518 struct platform_device *op;
2521 op = to_platform_device(parent);
2522 qp = platform_get_drvdata(op);
2526 qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
2530 for (i = 0; i < 4; i++)
2531 qp->happy_meals[i] = NULL;
2533 qp->quattro_dev = child;
2534 qp->next = qfe_sbus_list;
2537 platform_set_drvdata(op, qp);
2542 /* After all quattro cards have been probed, we call these functions
2543 * to register the IRQ handlers for the cards that have been
2544 * successfully probed and skip the cards that failed to initialize
2546 static int __init quattro_sbus_register_irqs(void)
2550 for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
2551 struct platform_device *op = qp->quattro_dev;
2552 int err, qfe_slot, skip = 0;
2554 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++) {
2555 if (!qp->happy_meals[qfe_slot])
2561 err = request_irq(op->archdata.irqs[0],
2562 quattro_sbus_interrupt,
2563 IRQF_SHARED, "Quattro",
2566 printk(KERN_ERR "Quattro HME: IRQ registration "
2567 "error %d.\n", err);
2575 static void quattro_sbus_free_irqs(void)
2579 for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
2580 struct platform_device *op = qp->quattro_dev;
2581 int qfe_slot, skip = 0;
2583 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++) {
2584 if (!qp->happy_meals[qfe_slot])
2590 free_irq(op->archdata.irqs[0], qp);
2593 #endif /* CONFIG_SBUS */
2596 static struct quattro *quattro_pci_find(struct pci_dev *pdev)
2598 struct pci_dev *bdev = pdev->bus->self;
2601 if (!bdev) return NULL;
2602 for (qp = qfe_pci_list; qp != NULL; qp = qp->next) {
2603 struct pci_dev *qpdev = qp->quattro_dev;
2608 qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
2612 for (i = 0; i < 4; i++)
2613 qp->happy_meals[i] = NULL;
2615 qp->quattro_dev = bdev;
2616 qp->next = qfe_pci_list;
2619 /* No range tricks necessary on PCI. */
2624 #endif /* CONFIG_PCI */
2626 static const struct net_device_ops hme_netdev_ops = {
2627 .ndo_open = happy_meal_open,
2628 .ndo_stop = happy_meal_close,
2629 .ndo_start_xmit = happy_meal_start_xmit,
2630 .ndo_tx_timeout = happy_meal_tx_timeout,
2631 .ndo_get_stats = happy_meal_get_stats,
2632 .ndo_set_rx_mode = happy_meal_set_multicast,
2633 .ndo_set_mac_address = eth_mac_addr,
2634 .ndo_validate_addr = eth_validate_addr,
2638 static int happy_meal_sbus_probe_one(struct platform_device *op, int is_qfe)
2640 struct device_node *dp = op->dev.of_node, *sbus_dp;
2641 struct quattro *qp = NULL;
2642 struct happy_meal *hp;
2643 struct net_device *dev;
2644 int i, qfe_slot = -1;
2648 sbus_dp = op->dev.parent->of_node;
2650 /* We can match PCI devices too, do not accept those here. */
2651 if (!of_node_name_eq(sbus_dp, "sbus") && !of_node_name_eq(sbus_dp, "sbi"))
2655 qp = quattro_sbus_find(op);
2658 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
2659 if (qp->happy_meals[qfe_slot] == NULL)
2666 dev = alloc_etherdev(sizeof(struct happy_meal));
2669 SET_NETDEV_DEV(dev, &op->dev);
2671 if (hme_version_printed++ == 0)
2672 printk(KERN_INFO "%s", version);
2674 /* If user did not specify a MAC address specifically, use
2675 * the Quattro local-mac-address property...
2677 for (i = 0; i < 6; i++) {
2678 if (macaddr[i] != 0)
2681 if (i < 6) { /* a mac address was given */
2682 for (i = 0; i < 6; i++)
2683 addr[i] = macaddr[i];
2684 eth_hw_addr_set(dev, addr);
2687 const unsigned char *addr;
2690 addr = of_get_property(dp, "local-mac-address", &len);
2692 if (qfe_slot != -1 && addr && len == ETH_ALEN)
2693 eth_hw_addr_set(dev, addr);
2695 eth_hw_addr_set(dev, idprom->id_ethaddr);
2698 hp = netdev_priv(dev);
2701 hp->dma_dev = &op->dev;
2703 spin_lock_init(&hp->happy_lock);
2707 hp->qfe_parent = qp;
2708 hp->qfe_ent = qfe_slot;
2709 qp->happy_meals[qfe_slot] = dev;
2712 hp->gregs = of_ioremap(&op->resource[0], 0,
2713 GREG_REG_SIZE, "HME Global Regs");
2715 printk(KERN_ERR "happymeal: Cannot map global registers.\n");
2716 goto err_out_free_netdev;
2719 hp->etxregs = of_ioremap(&op->resource[1], 0,
2720 ETX_REG_SIZE, "HME TX Regs");
2722 printk(KERN_ERR "happymeal: Cannot map MAC TX registers.\n");
2723 goto err_out_iounmap;
2726 hp->erxregs = of_ioremap(&op->resource[2], 0,
2727 ERX_REG_SIZE, "HME RX Regs");
2729 printk(KERN_ERR "happymeal: Cannot map MAC RX registers.\n");
2730 goto err_out_iounmap;
2733 hp->bigmacregs = of_ioremap(&op->resource[3], 0,
2734 BMAC_REG_SIZE, "HME BIGMAC Regs");
2735 if (!hp->bigmacregs) {
2736 printk(KERN_ERR "happymeal: Cannot map BIGMAC registers.\n");
2737 goto err_out_iounmap;
2740 hp->tcvregs = of_ioremap(&op->resource[4], 0,
2741 TCVR_REG_SIZE, "HME Tranceiver Regs");
2743 printk(KERN_ERR "happymeal: Cannot map TCVR registers.\n");
2744 goto err_out_iounmap;
2747 hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
2748 if (hp->hm_revision == 0xff)
2749 hp->hm_revision = 0xa0;
2751 /* Now enable the feature flags we can. */
2752 if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
2753 hp->happy_flags = HFLAG_20_21;
2754 else if (hp->hm_revision != 0xa0)
2755 hp->happy_flags = HFLAG_NOT_A0;
2758 hp->happy_flags |= HFLAG_QUATTRO;
2760 /* Get the supported DVMA burst sizes from our Happy SBUS. */
2761 hp->happy_bursts = of_getintprop_default(sbus_dp,
2762 "burst-sizes", 0x00);
2764 hp->happy_block = dma_alloc_coherent(hp->dma_dev,
2769 if (!hp->happy_block)
2770 goto err_out_iounmap;
2772 /* Force check of the link first time we are brought up. */
2775 /* Force timer state to 'asleep' with count of zero. */
2776 hp->timer_state = asleep;
2777 hp->timer_ticks = 0;
2779 timer_setup(&hp->happy_timer, happy_meal_timer, 0);
2782 dev->netdev_ops = &hme_netdev_ops;
2783 dev->watchdog_timeo = 5*HZ;
2784 dev->ethtool_ops = &hme_ethtool_ops;
2786 /* Happy Meal can do it all... */
2787 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
2788 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
2790 hp->irq = op->archdata.irqs[0];
2792 #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
2793 /* Hook up SBUS register/descriptor accessors. */
2794 hp->read_desc32 = sbus_hme_read_desc32;
2795 hp->write_txd = sbus_hme_write_txd;
2796 hp->write_rxd = sbus_hme_write_rxd;
2797 hp->read32 = sbus_hme_read32;
2798 hp->write32 = sbus_hme_write32;
2801 /* Grrr, Happy Meal comes up by default not advertising
2802 * full duplex 100baseT capabilities, fix this.
2804 spin_lock_irq(&hp->happy_lock);
2805 happy_meal_set_initial_advertisement(hp);
2806 spin_unlock_irq(&hp->happy_lock);
2808 err = register_netdev(hp->dev);
2810 printk(KERN_ERR "happymeal: Cannot register net device, "
2812 goto err_out_free_coherent;
2815 platform_set_drvdata(op, hp);
2818 printk(KERN_INFO "%s: Quattro HME slot %d (SBUS) 10/100baseT Ethernet ",
2819 dev->name, qfe_slot);
2821 printk(KERN_INFO "%s: HAPPY MEAL (SBUS) 10/100baseT Ethernet ",
2824 printk("%pM\n", dev->dev_addr);
2828 err_out_free_coherent:
2829 dma_free_coherent(hp->dma_dev,
2836 of_iounmap(&op->resource[0], hp->gregs, GREG_REG_SIZE);
2838 of_iounmap(&op->resource[1], hp->etxregs, ETX_REG_SIZE);
2840 of_iounmap(&op->resource[2], hp->erxregs, ERX_REG_SIZE);
2842 of_iounmap(&op->resource[3], hp->bigmacregs, BMAC_REG_SIZE);
2844 of_iounmap(&op->resource[4], hp->tcvregs, TCVR_REG_SIZE);
2847 qp->happy_meals[qfe_slot] = NULL;
2849 err_out_free_netdev:
2858 #ifndef CONFIG_SPARC
2859 static int is_quattro_p(struct pci_dev *pdev)
2861 struct pci_dev *busdev = pdev->bus->self;
2862 struct pci_dev *this_pdev;
2865 if (busdev == NULL ||
2866 busdev->vendor != PCI_VENDOR_ID_DEC ||
2867 busdev->device != PCI_DEVICE_ID_DEC_21153)
2871 list_for_each_entry(this_pdev, &pdev->bus->devices, bus_list) {
2872 if (this_pdev->vendor == PCI_VENDOR_ID_SUN &&
2873 this_pdev->device == PCI_DEVICE_ID_SUN_HAPPYMEAL)
2883 /* Fetch MAC address from vital product data of PCI ROM. */
2884 static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, int index, unsigned char *dev_addr)
2888 for (this_offset = 0x20; this_offset < len; this_offset++) {
2889 void __iomem *p = rom_base + this_offset;
2891 if (readb(p + 0) != 0x90 ||
2892 readb(p + 1) != 0x00 ||
2893 readb(p + 2) != 0x09 ||
2894 readb(p + 3) != 0x4e ||
2895 readb(p + 4) != 0x41 ||
2896 readb(p + 5) != 0x06)
2905 for (i = 0; i < 6; i++)
2906 dev_addr[i] = readb(p + i);
2914 static void get_hme_mac_nonsparc(struct pci_dev *pdev, unsigned char *dev_addr)
2917 void __iomem *p = pci_map_rom(pdev, &size);
2923 if (is_quattro_p(pdev))
2924 index = PCI_SLOT(pdev->devfn);
2926 found = readb(p) == 0x55 &&
2927 readb(p + 1) == 0xaa &&
2928 find_eth_addr_in_vpd(p, (64 * 1024), index, dev_addr);
2929 pci_unmap_rom(pdev, p);
2934 /* Sun MAC prefix then 3 random bytes. */
2938 get_random_bytes(&dev_addr[3], 3);
2940 #endif /* !(CONFIG_SPARC) */
2942 static int happy_meal_pci_probe(struct pci_dev *pdev,
2943 const struct pci_device_id *ent)
2945 struct quattro *qp = NULL;
2947 struct device_node *dp;
2949 struct happy_meal *hp;
2950 struct net_device *dev;
2951 void __iomem *hpreg_base;
2952 unsigned long hpreg_res;
2953 int i, qfe_slot = -1;
2958 /* Now make sure pci_dev cookie is there. */
2960 dp = pci_device_to_OF_node(pdev);
2961 snprintf(prom_name, sizeof(prom_name), "%pOFn", dp);
2963 if (is_quattro_p(pdev))
2964 strcpy(prom_name, "SUNW,qfe");
2966 strcpy(prom_name, "SUNW,hme");
2971 if (pci_enable_device(pdev))
2973 pci_set_master(pdev);
2975 if (!strcmp(prom_name, "SUNW,qfe") || !strcmp(prom_name, "qfe")) {
2976 qp = quattro_pci_find(pdev);
2979 for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
2980 if (qp->happy_meals[qfe_slot] == NULL)
2986 dev = alloc_etherdev(sizeof(struct happy_meal));
2990 SET_NETDEV_DEV(dev, &pdev->dev);
2992 if (hme_version_printed++ == 0)
2993 printk(KERN_INFO "%s", version);
2995 hp = netdev_priv(dev);
2997 hp->happy_dev = pdev;
2998 hp->dma_dev = &pdev->dev;
3000 spin_lock_init(&hp->happy_lock);
3003 hp->qfe_parent = qp;
3004 hp->qfe_ent = qfe_slot;
3005 qp->happy_meals[qfe_slot] = dev;
3008 hpreg_res = pci_resource_start(pdev, 0);
3010 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
3011 printk(KERN_ERR "happymeal(PCI): Cannot find proper PCI device base address.\n");
3012 goto err_out_clear_quattro;
3014 if (pci_request_regions(pdev, DRV_NAME)) {
3015 printk(KERN_ERR "happymeal(PCI): Cannot obtain PCI resources, "
3017 goto err_out_clear_quattro;
3020 if ((hpreg_base = ioremap(hpreg_res, 0x8000)) == NULL) {
3021 printk(KERN_ERR "happymeal(PCI): Unable to remap card memory.\n");
3022 goto err_out_free_res;
3025 for (i = 0; i < 6; i++) {
3026 if (macaddr[i] != 0)
3029 if (i < 6) { /* a mac address was given */
3030 for (i = 0; i < 6; i++)
3031 addr[i] = macaddr[i];
3032 eth_hw_addr_set(dev, addr);
3036 const unsigned char *addr;
3039 if (qfe_slot != -1 &&
3040 (addr = of_get_property(dp, "local-mac-address", &len))
3043 eth_hw_addr_set(dev, addr);
3045 eth_hw_addr_set(dev, idprom->id_ethaddr);
3050 get_hme_mac_nonsparc(pdev, addr);
3051 eth_hw_addr_set(dev, addr);
3055 /* Layout registers. */
3056 hp->gregs = (hpreg_base + 0x0000UL);
3057 hp->etxregs = (hpreg_base + 0x2000UL);
3058 hp->erxregs = (hpreg_base + 0x4000UL);
3059 hp->bigmacregs = (hpreg_base + 0x6000UL);
3060 hp->tcvregs = (hpreg_base + 0x7000UL);
3063 hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
3064 if (hp->hm_revision == 0xff)
3065 hp->hm_revision = 0xc0 | (pdev->revision & 0x0f);
3067 /* works with this on non-sparc hosts */
3068 hp->hm_revision = 0x20;
3071 /* Now enable the feature flags we can. */
3072 if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
3073 hp->happy_flags = HFLAG_20_21;
3074 else if (hp->hm_revision != 0xa0 && hp->hm_revision != 0xc0)
3075 hp->happy_flags = HFLAG_NOT_A0;
3078 hp->happy_flags |= HFLAG_QUATTRO;
3080 /* And of course, indicate this is PCI. */
3081 hp->happy_flags |= HFLAG_PCI;
3084 /* Assume PCI happy meals can handle all burst sizes. */
3085 hp->happy_bursts = DMA_BURSTBITS;
3088 hp->happy_block = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
3089 &hp->hblock_dvma, GFP_KERNEL);
3091 if (!hp->happy_block)
3092 goto err_out_iounmap;
3095 hp->timer_state = asleep;
3096 hp->timer_ticks = 0;
3098 timer_setup(&hp->happy_timer, happy_meal_timer, 0);
3100 hp->irq = pdev->irq;
3102 dev->netdev_ops = &hme_netdev_ops;
3103 dev->watchdog_timeo = 5*HZ;
3104 dev->ethtool_ops = &hme_ethtool_ops;
3106 /* Happy Meal can do it all... */
3107 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
3108 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
3110 #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
3111 /* Hook up PCI register/descriptor accessors. */
3112 hp->read_desc32 = pci_hme_read_desc32;
3113 hp->write_txd = pci_hme_write_txd;
3114 hp->write_rxd = pci_hme_write_rxd;
3115 hp->read32 = pci_hme_read32;
3116 hp->write32 = pci_hme_write32;
3119 /* Grrr, Happy Meal comes up by default not advertising
3120 * full duplex 100baseT capabilities, fix this.
3122 spin_lock_irq(&hp->happy_lock);
3123 happy_meal_set_initial_advertisement(hp);
3124 spin_unlock_irq(&hp->happy_lock);
3126 err = register_netdev(hp->dev);
3128 printk(KERN_ERR "happymeal(PCI): Cannot register net device, "
3130 goto err_out_free_coherent;
3133 pci_set_drvdata(pdev, hp);
3136 struct pci_dev *qpdev = qp->quattro_dev;
3139 if (!strncmp(dev->name, "eth", 3)) {
3140 int i = simple_strtoul(dev->name + 3, NULL, 10);
3141 sprintf(prom_name, "-%d", i + 3);
3143 printk(KERN_INFO "%s%s: Quattro HME (PCI/CheerIO) 10/100baseT Ethernet ", dev->name, prom_name);
3144 if (qpdev->vendor == PCI_VENDOR_ID_DEC &&
3145 qpdev->device == PCI_DEVICE_ID_DEC_21153)
3146 printk("DEC 21153 PCI Bridge\n");
3148 printk("unknown bridge %04x.%04x\n",
3149 qpdev->vendor, qpdev->device);
3153 printk(KERN_INFO "%s: Quattro HME slot %d (PCI/CheerIO) 10/100baseT Ethernet ",
3154 dev->name, qfe_slot);
3156 printk(KERN_INFO "%s: HAPPY MEAL (PCI/CheerIO) 10/100BaseT Ethernet ",
3159 printk("%pM\n", dev->dev_addr);
3163 err_out_free_coherent:
3164 dma_free_coherent(hp->dma_dev, PAGE_SIZE,
3165 hp->happy_block, hp->hblock_dvma);
3171 pci_release_regions(pdev);
3173 err_out_clear_quattro:
3175 qp->happy_meals[qfe_slot] = NULL;
3183 static void happy_meal_pci_remove(struct pci_dev *pdev)
3185 struct happy_meal *hp = pci_get_drvdata(pdev);
3186 struct net_device *net_dev = hp->dev;
3188 unregister_netdev(net_dev);
3190 dma_free_coherent(hp->dma_dev, PAGE_SIZE,
3191 hp->happy_block, hp->hblock_dvma);
3193 pci_release_regions(hp->happy_dev);
3195 free_netdev(net_dev);
3198 static const struct pci_device_id happymeal_pci_ids[] = {
3199 { PCI_DEVICE(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_HAPPYMEAL) },
3200 { } /* Terminating entry */
3203 MODULE_DEVICE_TABLE(pci, happymeal_pci_ids);
3205 static struct pci_driver hme_pci_driver = {
3207 .id_table = happymeal_pci_ids,
3208 .probe = happy_meal_pci_probe,
3209 .remove = happy_meal_pci_remove,
3212 static int __init happy_meal_pci_init(void)
3214 return pci_register_driver(&hme_pci_driver);
3217 static void happy_meal_pci_exit(void)
3219 pci_unregister_driver(&hme_pci_driver);
3221 while (qfe_pci_list) {
3222 struct quattro *qfe = qfe_pci_list;
3223 struct quattro *next = qfe->next;
3227 qfe_pci_list = next;
3234 static const struct of_device_id hme_sbus_match[];
3235 static int hme_sbus_probe(struct platform_device *op)
3237 const struct of_device_id *match;
3238 struct device_node *dp = op->dev.of_node;
3239 const char *model = of_get_property(dp, "model", NULL);
3242 match = of_match_device(hme_sbus_match, &op->dev);
3245 is_qfe = (match->data != NULL);
3247 if (!is_qfe && model && !strcmp(model, "SUNW,sbus-qfe"))
3250 return happy_meal_sbus_probe_one(op, is_qfe);
3253 static int hme_sbus_remove(struct platform_device *op)
3255 struct happy_meal *hp = platform_get_drvdata(op);
3256 struct net_device *net_dev = hp->dev;
3258 unregister_netdev(net_dev);
3260 /* XXX qfe parent interrupt... */
3262 of_iounmap(&op->resource[0], hp->gregs, GREG_REG_SIZE);
3263 of_iounmap(&op->resource[1], hp->etxregs, ETX_REG_SIZE);
3264 of_iounmap(&op->resource[2], hp->erxregs, ERX_REG_SIZE);
3265 of_iounmap(&op->resource[3], hp->bigmacregs, BMAC_REG_SIZE);
3266 of_iounmap(&op->resource[4], hp->tcvregs, TCVR_REG_SIZE);
3267 dma_free_coherent(hp->dma_dev,
3272 free_netdev(net_dev);
3277 static const struct of_device_id hme_sbus_match[] = {
3292 MODULE_DEVICE_TABLE(of, hme_sbus_match);
3294 static struct platform_driver hme_sbus_driver = {
3297 .of_match_table = hme_sbus_match,
3299 .probe = hme_sbus_probe,
3300 .remove = hme_sbus_remove,
3303 static int __init happy_meal_sbus_init(void)
3307 err = platform_driver_register(&hme_sbus_driver);
3309 err = quattro_sbus_register_irqs();
3314 static void happy_meal_sbus_exit(void)
3316 platform_driver_unregister(&hme_sbus_driver);
3317 quattro_sbus_free_irqs();
3319 while (qfe_sbus_list) {
3320 struct quattro *qfe = qfe_sbus_list;
3321 struct quattro *next = qfe->next;
3325 qfe_sbus_list = next;
3330 static int __init happy_meal_probe(void)
3335 err = happy_meal_sbus_init();
3339 err = happy_meal_pci_init();
3342 happy_meal_sbus_exit();
3351 static void __exit happy_meal_exit(void)
3354 happy_meal_sbus_exit();
3357 happy_meal_pci_exit();
3361 module_init(happy_meal_probe);
3362 module_exit(happy_meal_exit);