jiffies_round-jiffies_round_relative-conversion-rt2x00-checkpatch-fixes
[linux-2.6/linux-trees-mm.git] / drivers / net / sb1250-mac.c
blob7b53d658e33712fa65d11e4932e260a6a8c3beca
1 /*
2 * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
3 * Copyright (c) 2006, 2007 Maciej W. Rozycki
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * This driver is designed for the Broadcom SiByte SOC built-in
21 * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
23 * Updated to the driver model and the PHY abstraction layer
24 * by Maciej W. Rozycki.
27 #include <linux/bug.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/timer.h>
32 #include <linux/errno.h>
33 #include <linux/ioport.h>
34 #include <linux/slab.h>
35 #include <linux/interrupt.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
40 #include <linux/bitops.h>
41 #include <linux/err.h>
42 #include <linux/ethtool.h>
43 #include <linux/mii.h>
44 #include <linux/phy.h>
45 #include <linux/platform_device.h>
47 #include <asm/cache.h>
48 #include <asm/io.h>
49 #include <asm/processor.h> /* Processor type for cache alignment. */
51 /* This is only here until the firmware is ready. In that case,
52 the firmware leaves the ethernet address in the register for us. */
53 #ifdef CONFIG_SIBYTE_STANDALONE
54 #define SBMAC_ETH0_HWADDR "40:00:00:00:01:00"
55 #define SBMAC_ETH1_HWADDR "40:00:00:00:01:01"
56 #define SBMAC_ETH2_HWADDR "40:00:00:00:01:02"
57 #define SBMAC_ETH3_HWADDR "40:00:00:00:01:03"
58 #endif
61 /* These identify the driver base version and may not be removed. */
62 #if 0
63 static char version1[] __initdata =
64 "sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg\n";
65 #endif
68 /* Operational parameters that usually are not changed. */
70 #define CONFIG_SBMAC_COALESCE
72 /* Time in jiffies before concluding the transmitter is hung. */
73 #define TX_TIMEOUT (2*HZ)
76 MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
77 MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
79 /* A few user-configurable values which may be modified when a driver
80 module is loaded. */
82 /* 1 normal messages, 0 quiet .. 7 verbose. */
83 static int debug = 1;
84 module_param(debug, int, S_IRUGO);
85 MODULE_PARM_DESC(debug, "Debug messages");
87 #ifdef CONFIG_SBMAC_COALESCE
88 static int int_pktcnt_tx = 255;
89 module_param(int_pktcnt_tx, int, S_IRUGO);
90 MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
92 static int int_timeout_tx = 255;
93 module_param(int_timeout_tx, int, S_IRUGO);
94 MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
96 static int int_pktcnt_rx = 64;
97 module_param(int_pktcnt_rx, int, S_IRUGO);
98 MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
100 static int int_timeout_rx = 64;
101 module_param(int_timeout_rx, int, S_IRUGO);
102 MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
103 #endif
105 #include <asm/sibyte/board.h>
106 #include <asm/sibyte/sb1250.h>
107 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
108 #include <asm/sibyte/bcm1480_regs.h>
109 #include <asm/sibyte/bcm1480_int.h>
110 #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
111 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
112 #include <asm/sibyte/sb1250_regs.h>
113 #include <asm/sibyte/sb1250_int.h>
114 #else
115 #error invalid SiByte MAC configuation
116 #endif
117 #include <asm/sibyte/sb1250_scd.h>
118 #include <asm/sibyte/sb1250_mac.h>
119 #include <asm/sibyte/sb1250_dma.h>
121 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
122 #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
123 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
124 #define UNIT_INT(n) (K_INT_MAC_0 + (n))
125 #else
126 #error invalid SiByte MAC configuation
127 #endif
129 #ifdef K_INT_PHY
130 #define SBMAC_PHY_INT K_INT_PHY
131 #else
132 #define SBMAC_PHY_INT PHY_POLL
133 #endif
135 /**********************************************************************
136 * Simple types
137 ********************************************************************* */
139 enum sbmac_speed {
140 sbmac_speed_none = 0,
141 sbmac_speed_10 = SPEED_10,
142 sbmac_speed_100 = SPEED_100,
143 sbmac_speed_1000 = SPEED_1000,
146 enum sbmac_duplex {
147 sbmac_duplex_none = -1,
148 sbmac_duplex_half = DUPLEX_HALF,
149 sbmac_duplex_full = DUPLEX_FULL,
152 enum sbmac_fc {
153 sbmac_fc_none,
154 sbmac_fc_disabled,
155 sbmac_fc_frame,
156 sbmac_fc_collision,
157 sbmac_fc_carrier,
160 enum sbmac_state {
161 sbmac_state_uninit,
162 sbmac_state_off,
163 sbmac_state_on,
164 sbmac_state_broken,
168 /**********************************************************************
169 * Macros
170 ********************************************************************* */
173 #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
174 (d)->sbdma_dscrtable : (d)->f+1)
177 #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
179 #define SBMAC_MAX_TXDESCR 256
180 #define SBMAC_MAX_RXDESCR 256
182 #define ETHER_ALIGN 2
183 #define ETHER_ADDR_LEN 6
184 #define ENET_PACKET_SIZE 1518
185 /*#define ENET_PACKET_SIZE 9216 */
187 /**********************************************************************
188 * DMA Descriptor structure
189 ********************************************************************* */
191 struct sbdmadscr {
192 uint64_t dscr_a;
193 uint64_t dscr_b;
196 /**********************************************************************
197 * DMA Controller structure
198 ********************************************************************* */
200 struct sbmacdma {
203 * This stuff is used to identify the channel and the registers
204 * associated with it.
206 struct sbmac_softc *sbdma_eth; /* back pointer to associated
207 MAC */
208 int sbdma_channel; /* channel number */
209 int sbdma_txdir; /* direction (1=transmit) */
210 int sbdma_maxdescr; /* total # of descriptors
211 in ring */
212 #ifdef CONFIG_SBMAC_COALESCE
213 int sbdma_int_pktcnt;
214 /* # descriptors rx/tx
215 before interrupt */
216 int sbdma_int_timeout;
217 /* # usec rx/tx interrupt */
218 #endif
219 void __iomem *sbdma_config0; /* DMA config register 0 */
220 void __iomem *sbdma_config1; /* DMA config register 1 */
221 void __iomem *sbdma_dscrbase;
222 /* descriptor base address */
223 void __iomem *sbdma_dscrcnt; /* descriptor count register */
224 void __iomem *sbdma_curdscr; /* current descriptor
225 address */
226 void __iomem *sbdma_oodpktlost;
227 /* pkt drop (rx only) */
230 * This stuff is for maintenance of the ring
232 void *sbdma_dscrtable_unaligned;
233 struct sbdmadscr *sbdma_dscrtable;
234 /* base of descriptor table */
235 struct sbdmadscr *sbdma_dscrtable_end;
236 /* end of descriptor table */
237 struct sk_buff **sbdma_ctxtable;
238 /* context table, one
239 per descr */
240 dma_addr_t sbdma_dscrtable_phys;
241 /* and also the phys addr */
242 struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
243 struct sbdmadscr *sbdma_remptr; /* next dscr for sw
244 to remove */
248 /**********************************************************************
249 * Ethernet softc structure
250 ********************************************************************* */
252 struct sbmac_softc {
255 * Linux-specific things
257 struct net_device *sbm_dev; /* pointer to linux device */
258 struct napi_struct napi;
259 struct phy_device *phy_dev; /* the associated PHY device */
260 struct mii_bus mii_bus; /* the MII bus */
261 int phy_irq[PHY_MAX_ADDR];
262 spinlock_t sbm_lock; /* spin lock */
263 int sbm_devflags; /* current device flags */
265 int sbm_buffersize;
268 * Controller-specific things
270 void __iomem *sbm_base; /* MAC's base address */
271 enum sbmac_state sbm_state; /* current state */
273 void __iomem *sbm_macenable; /* MAC Enable Register */
274 void __iomem *sbm_maccfg; /* MAC Config Register */
275 void __iomem *sbm_fifocfg; /* FIFO Config Register */
276 void __iomem *sbm_framecfg; /* Frame Config Register */
277 void __iomem *sbm_rxfilter; /* Receive Filter Register */
278 void __iomem *sbm_isr; /* Interrupt Status Register */
279 void __iomem *sbm_imr; /* Interrupt Mask Register */
280 void __iomem *sbm_mdio; /* MDIO Register */
282 enum sbmac_speed sbm_speed; /* current speed */
283 enum sbmac_duplex sbm_duplex; /* current duplex */
284 enum sbmac_fc sbm_fc; /* cur. flow control setting */
285 int sbm_pause; /* current pause setting */
286 int sbm_link; /* current link state */
288 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
290 struct sbmacdma sbm_txdma; /* only channel 0 for now */
291 struct sbmacdma sbm_rxdma;
292 int rx_hw_checksum;
293 int sbe_idx;
297 /**********************************************************************
298 * Externs
299 ********************************************************************* */
301 /**********************************************************************
302 * Prototypes
303 ********************************************************************* */
305 static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
306 int txrx, int maxdescr);
307 static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
308 static int sbdma_add_rcvbuffer(struct sbmacdma *d, struct sk_buff *m);
309 static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
310 static void sbdma_emptyring(struct sbmacdma *d);
311 static void sbdma_fillring(struct sbmacdma *d);
312 static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
313 int work_to_do, int poll);
314 static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
315 int poll);
316 static int sbmac_initctx(struct sbmac_softc *s);
317 static void sbmac_channel_start(struct sbmac_softc *s);
318 static void sbmac_channel_stop(struct sbmac_softc *s);
319 static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
320 enum sbmac_state);
321 static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
322 static uint64_t sbmac_addr2reg(unsigned char *ptr);
323 static irqreturn_t sbmac_intr(int irq, void *dev_instance);
324 static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
325 static void sbmac_setmulti(struct sbmac_softc *sc);
326 static int sbmac_init(struct platform_device *pldev, long long base);
327 static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
328 static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
329 enum sbmac_fc fc);
331 static int sbmac_open(struct net_device *dev);
332 static void sbmac_tx_timeout (struct net_device *dev);
333 static void sbmac_set_rx_mode(struct net_device *dev);
334 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
335 static int sbmac_close(struct net_device *dev);
336 static int sbmac_poll(struct napi_struct *napi, int budget);
338 static void sbmac_mii_poll(struct net_device *dev);
339 static int sbmac_mii_probe(struct net_device *dev);
341 static void sbmac_mii_sync(void __iomem *sbm_mdio);
342 static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
343 int bitcnt);
344 static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
345 static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
346 u16 val);
349 /**********************************************************************
350 * Globals
351 ********************************************************************* */
353 static char sbmac_string[] = "sb1250-mac";
354 static char sbmac_pretty[] = "SB1250 MAC";
356 static char sbmac_mdio_string[] = "sb1250-mac-mdio";
359 /**********************************************************************
360 * MDIO constants
361 ********************************************************************* */
363 #define MII_COMMAND_START 0x01
364 #define MII_COMMAND_READ 0x02
365 #define MII_COMMAND_WRITE 0x01
366 #define MII_COMMAND_ACK 0x02
368 #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
370 #define ENABLE 1
371 #define DISABLE 0
373 /**********************************************************************
374 * SBMAC_MII_SYNC(sbm_mdio)
376 * Synchronize with the MII - send a pattern of bits to the MII
377 * that will guarantee that it is ready to accept a command.
379 * Input parameters:
380 * sbm_mdio - address of the MAC's MDIO register
382 * Return value:
383 * nothing
384 ********************************************************************* */
386 static void sbmac_mii_sync(void __iomem *sbm_mdio)
388 int cnt;
389 uint64_t bits;
390 int mac_mdio_genc;
392 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
394 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
396 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
398 for (cnt = 0; cnt < 32; cnt++) {
399 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
400 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
404 /**********************************************************************
405 * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
407 * Send some bits to the MII. The bits to be sent are right-
408 * justified in the 'data' parameter.
410 * Input parameters:
411 * sbm_mdio - address of the MAC's MDIO register
412 * data - data to send
413 * bitcnt - number of bits to send
414 ********************************************************************* */
416 static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
417 int bitcnt)
419 int i;
420 uint64_t bits;
421 unsigned int curmask;
422 int mac_mdio_genc;
424 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
426 bits = M_MAC_MDIO_DIR_OUTPUT;
427 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
429 curmask = 1 << (bitcnt - 1);
431 for (i = 0; i < bitcnt; i++) {
432 if (data & curmask)
433 bits |= M_MAC_MDIO_OUT;
434 else bits &= ~M_MAC_MDIO_OUT;
435 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
436 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
437 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
438 curmask >>= 1;
444 /**********************************************************************
445 * SBMAC_MII_READ(bus, phyaddr, regidx)
446 * Read a PHY register.
448 * Input parameters:
449 * bus - MDIO bus handle
450 * phyaddr - PHY's address
451 * regnum - index of register to read
453 * Return value:
454 * value read, or 0xffff if an error occurred.
455 ********************************************************************* */
457 static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
459 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
460 void __iomem *sbm_mdio = sc->sbm_mdio;
461 int idx;
462 int error;
463 int regval;
464 int mac_mdio_genc;
467 * Synchronize ourselves so that the PHY knows the next
468 * thing coming down is a command
470 sbmac_mii_sync(sbm_mdio);
473 * Send the data to the PHY. The sequence is
474 * a "start" command (2 bits)
475 * a "read" command (2 bits)
476 * the PHY addr (5 bits)
477 * the register index (5 bits)
479 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
480 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
481 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
482 sbmac_mii_senddata(sbm_mdio, regidx, 5);
484 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
487 * Switch the port around without a clock transition.
489 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
492 * Send out a clock pulse to signal we want the status
494 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
495 sbm_mdio);
496 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
499 * If an error occurred, the PHY will signal '1' back
501 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
504 * Issue an 'idle' clock pulse, but keep the direction
505 * the same.
507 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
508 sbm_mdio);
509 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
511 regval = 0;
513 for (idx = 0; idx < 16; idx++) {
514 regval <<= 1;
516 if (error == 0) {
517 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
518 regval |= 1;
521 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
522 sbm_mdio);
523 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
526 /* Switch back to output */
527 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
529 if (error == 0)
530 return regval;
531 return 0xffff;
535 /**********************************************************************
536 * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
538 * Write a value to a PHY register.
540 * Input parameters:
541 * bus - MDIO bus handle
542 * phyaddr - PHY to use
543 * regidx - register within the PHY
544 * regval - data to write to register
546 * Return value:
547 * 0 for success
548 ********************************************************************* */
550 static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
551 u16 regval)
553 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
554 void __iomem *sbm_mdio = sc->sbm_mdio;
555 int mac_mdio_genc;
557 sbmac_mii_sync(sbm_mdio);
559 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
560 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
561 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
562 sbmac_mii_senddata(sbm_mdio, regidx, 5);
563 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
564 sbmac_mii_senddata(sbm_mdio, regval, 16);
566 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
568 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
570 return 0;
575 /**********************************************************************
576 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
578 * Initialize a DMA channel context. Since there are potentially
579 * eight DMA channels per MAC, it's nice to do this in a standard
580 * way.
582 * Input parameters:
583 * d - struct sbmacdma (DMA channel context)
584 * s - struct sbmac_softc (pointer to a MAC)
585 * chan - channel number (0..1 right now)
586 * txrx - Identifies DMA_TX or DMA_RX for channel direction
587 * maxdescr - number of descriptors
589 * Return value:
590 * nothing
591 ********************************************************************* */
593 static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
594 int txrx, int maxdescr)
596 #ifdef CONFIG_SBMAC_COALESCE
597 int int_pktcnt, int_timeout;
598 #endif
601 * Save away interesting stuff in the structure
604 d->sbdma_eth = s;
605 d->sbdma_channel = chan;
606 d->sbdma_txdir = txrx;
608 #if 0
609 /* RMON clearing */
610 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
611 #endif
613 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
614 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
615 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
616 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
617 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
618 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
619 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
620 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
621 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
622 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
623 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
624 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
625 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
626 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
627 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
628 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
629 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
630 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
631 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
632 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
633 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
636 * initialize register pointers
639 d->sbdma_config0 =
640 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
641 d->sbdma_config1 =
642 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
643 d->sbdma_dscrbase =
644 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
645 d->sbdma_dscrcnt =
646 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
647 d->sbdma_curdscr =
648 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
649 if (d->sbdma_txdir)
650 d->sbdma_oodpktlost = NULL;
651 else
652 d->sbdma_oodpktlost =
653 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
656 * Allocate memory for the ring
659 d->sbdma_maxdescr = maxdescr;
661 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
662 sizeof(*d->sbdma_dscrtable),
663 GFP_KERNEL);
666 * The descriptor table must be aligned to at least 16 bytes or the
667 * MAC will corrupt it.
669 d->sbdma_dscrtable = (struct sbdmadscr *)
670 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
671 sizeof(*d->sbdma_dscrtable));
673 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
675 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
678 * And context table
681 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
682 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
684 #ifdef CONFIG_SBMAC_COALESCE
686 * Setup Rx/Tx DMA coalescing defaults
689 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
690 if ( int_pktcnt ) {
691 d->sbdma_int_pktcnt = int_pktcnt;
692 } else {
693 d->sbdma_int_pktcnt = 1;
696 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
697 if ( int_timeout ) {
698 d->sbdma_int_timeout = int_timeout;
699 } else {
700 d->sbdma_int_timeout = 0;
702 #endif
706 /**********************************************************************
707 * SBDMA_CHANNEL_START(d)
709 * Initialize the hardware registers for a DMA channel.
711 * Input parameters:
712 * d - DMA channel to init (context must be previously init'd
713 * rxtx - DMA_RX or DMA_TX depending on what type of channel
715 * Return value:
716 * nothing
717 ********************************************************************* */
719 static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
722 * Turn on the DMA channel
725 #ifdef CONFIG_SBMAC_COALESCE
726 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
727 0, d->sbdma_config1);
728 __raw_writeq(M_DMA_EOP_INT_EN |
729 V_DMA_RINGSZ(d->sbdma_maxdescr) |
730 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
731 0, d->sbdma_config0);
732 #else
733 __raw_writeq(0, d->sbdma_config1);
734 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
735 0, d->sbdma_config0);
736 #endif
738 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
741 * Initialize ring pointers
744 d->sbdma_addptr = d->sbdma_dscrtable;
745 d->sbdma_remptr = d->sbdma_dscrtable;
748 /**********************************************************************
749 * SBDMA_CHANNEL_STOP(d)
751 * Initialize the hardware registers for a DMA channel.
753 * Input parameters:
754 * d - DMA channel to init (context must be previously init'd
756 * Return value:
757 * nothing
758 ********************************************************************* */
760 static void sbdma_channel_stop(struct sbmacdma *d)
763 * Turn off the DMA channel
766 __raw_writeq(0, d->sbdma_config1);
768 __raw_writeq(0, d->sbdma_dscrbase);
770 __raw_writeq(0, d->sbdma_config0);
773 * Zero ring pointers
776 d->sbdma_addptr = NULL;
777 d->sbdma_remptr = NULL;
780 static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
782 unsigned long addr;
783 unsigned long newaddr;
785 addr = (unsigned long) skb->data;
787 newaddr = (addr + power2 - 1) & ~(power2 - 1);
789 skb_reserve(skb,newaddr-addr+offset);
793 /**********************************************************************
794 * SBDMA_ADD_RCVBUFFER(d,sb)
796 * Add a buffer to the specified DMA channel. For receive channels,
797 * this queues a buffer for inbound packets.
799 * Input parameters:
800 * d - DMA channel descriptor
801 * sb - sk_buff to add, or NULL if we should allocate one
803 * Return value:
804 * 0 if buffer could not be added (ring is full)
805 * 1 if buffer added successfully
806 ********************************************************************* */
809 static int sbdma_add_rcvbuffer(struct sbmacdma *d, struct sk_buff *sb)
811 struct sbdmadscr *dsc;
812 struct sbdmadscr *nextdsc;
813 struct sk_buff *sb_new = NULL;
814 int pktsize = ENET_PACKET_SIZE;
816 /* get pointer to our current place in the ring */
818 dsc = d->sbdma_addptr;
819 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
822 * figure out if the ring is full - if the next descriptor
823 * is the same as the one that we're going to remove from
824 * the ring, the ring is full
827 if (nextdsc == d->sbdma_remptr) {
828 return -ENOSPC;
832 * Allocate a sk_buff if we don't already have one.
833 * If we do have an sk_buff, reset it so that it's empty.
835 * Note: sk_buffs don't seem to be guaranteed to have any sort
836 * of alignment when they are allocated. Therefore, allocate enough
837 * extra space to make sure that:
839 * 1. the data does not start in the middle of a cache line.
840 * 2. The data does not end in the middle of a cache line
841 * 3. The buffer can be aligned such that the IP addresses are
842 * naturally aligned.
844 * Remember, the SOCs MAC writes whole cache lines at a time,
845 * without reading the old contents first. So, if the sk_buff's
846 * data portion starts in the middle of a cache line, the SOC
847 * DMA will trash the beginning (and ending) portions.
850 if (sb == NULL) {
851 sb_new = dev_alloc_skb(ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN);
852 if (sb_new == NULL) {
853 pr_info("%s: sk_buff allocation failed\n",
854 d->sbdma_eth->sbm_dev->name);
855 return -ENOBUFS;
858 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, ETHER_ALIGN);
860 else {
861 sb_new = sb;
863 * nothing special to reinit buffer, it's already aligned
864 * and sb->data already points to a good place.
869 * fill in the descriptor
872 #ifdef CONFIG_SBMAC_COALESCE
874 * Do not interrupt per DMA transfer.
876 dsc->dscr_a = virt_to_phys(sb_new->data) |
877 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) | 0;
878 #else
879 dsc->dscr_a = virt_to_phys(sb_new->data) |
880 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
881 M_DMA_DSCRA_INTERRUPT;
882 #endif
884 /* receiving: no options */
885 dsc->dscr_b = 0;
888 * fill in the context
891 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
894 * point at next packet
897 d->sbdma_addptr = nextdsc;
900 * Give the buffer to the DMA engine.
903 __raw_writeq(1, d->sbdma_dscrcnt);
905 return 0; /* we did it */
908 /**********************************************************************
909 * SBDMA_ADD_TXBUFFER(d,sb)
911 * Add a transmit buffer to the specified DMA channel, causing a
912 * transmit to start.
914 * Input parameters:
915 * d - DMA channel descriptor
916 * sb - sk_buff to add
918 * Return value:
919 * 0 transmit queued successfully
920 * otherwise error code
921 ********************************************************************* */
924 static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
926 struct sbdmadscr *dsc;
927 struct sbdmadscr *nextdsc;
928 uint64_t phys;
929 uint64_t ncb;
930 int length;
932 /* get pointer to our current place in the ring */
934 dsc = d->sbdma_addptr;
935 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
938 * figure out if the ring is full - if the next descriptor
939 * is the same as the one that we're going to remove from
940 * the ring, the ring is full
943 if (nextdsc == d->sbdma_remptr) {
944 return -ENOSPC;
948 * Under Linux, it's not necessary to copy/coalesce buffers
949 * like it is on NetBSD. We think they're all contiguous,
950 * but that may not be true for GBE.
953 length = sb->len;
956 * fill in the descriptor. Note that the number of cache
957 * blocks in the descriptor is the number of blocks
958 * *spanned*, so we need to add in the offset (if any)
959 * while doing the calculation.
962 phys = virt_to_phys(sb->data);
963 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
965 dsc->dscr_a = phys |
966 V_DMA_DSCRA_A_SIZE(ncb) |
967 #ifndef CONFIG_SBMAC_COALESCE
968 M_DMA_DSCRA_INTERRUPT |
969 #endif
970 M_DMA_ETHTX_SOP;
972 /* transmitting: set outbound options and length */
974 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
975 V_DMA_DSCRB_PKT_SIZE(length);
978 * fill in the context
981 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
984 * point at next packet
987 d->sbdma_addptr = nextdsc;
990 * Give the buffer to the DMA engine.
993 __raw_writeq(1, d->sbdma_dscrcnt);
995 return 0; /* we did it */
1001 /**********************************************************************
1002 * SBDMA_EMPTYRING(d)
1004 * Free all allocated sk_buffs on the specified DMA channel;
1006 * Input parameters:
1007 * d - DMA channel
1009 * Return value:
1010 * nothing
1011 ********************************************************************* */
1013 static void sbdma_emptyring(struct sbmacdma *d)
1015 int idx;
1016 struct sk_buff *sb;
1018 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
1019 sb = d->sbdma_ctxtable[idx];
1020 if (sb) {
1021 dev_kfree_skb(sb);
1022 d->sbdma_ctxtable[idx] = NULL;
1028 /**********************************************************************
1029 * SBDMA_FILLRING(d)
1031 * Fill the specified DMA channel (must be receive channel)
1032 * with sk_buffs
1034 * Input parameters:
1035 * d - DMA channel
1037 * Return value:
1038 * nothing
1039 ********************************************************************* */
1041 static void sbdma_fillring(struct sbmacdma *d)
1043 int idx;
1045 for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
1046 if (sbdma_add_rcvbuffer(d,NULL) != 0)
1047 break;
1051 #ifdef CONFIG_NET_POLL_CONTROLLER
1052 static void sbmac_netpoll(struct net_device *netdev)
1054 struct sbmac_softc *sc = netdev_priv(netdev);
1055 int irq = sc->sbm_dev->irq;
1057 __raw_writeq(0, sc->sbm_imr);
1059 sbmac_intr(irq, netdev);
1061 #ifdef CONFIG_SBMAC_COALESCE
1062 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1063 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1064 sc->sbm_imr);
1065 #else
1066 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1067 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1068 #endif
1070 #endif
1072 /**********************************************************************
1073 * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
1075 * Process "completed" receive buffers on the specified DMA channel.
1077 * Input parameters:
1078 * sc - softc structure
1079 * d - DMA channel context
1080 * work_to_do - no. of packets to process before enabling interrupt
1081 * again (for NAPI)
1082 * poll - 1: using polling (for NAPI)
1084 * Return value:
1085 * nothing
1086 ********************************************************************* */
1088 static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1089 int work_to_do, int poll)
1091 struct net_device *dev = sc->sbm_dev;
1092 int curidx;
1093 int hwidx;
1094 struct sbdmadscr *dsc;
1095 struct sk_buff *sb;
1096 int len;
1097 int work_done = 0;
1098 int dropped = 0;
1100 prefetch(d);
1102 again:
1103 /* Check if the HW dropped any frames */
1104 dev->stats.rx_fifo_errors
1105 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1106 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1108 while (work_to_do-- > 0) {
1110 * figure out where we are (as an index) and where
1111 * the hardware is (also as an index)
1113 * This could be done faster if (for example) the
1114 * descriptor table was page-aligned and contiguous in
1115 * both virtual and physical memory -- you could then
1116 * just compare the low-order bits of the virtual address
1117 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1120 dsc = d->sbdma_remptr;
1121 curidx = dsc - d->sbdma_dscrtable;
1123 prefetch(dsc);
1124 prefetch(&d->sbdma_ctxtable[curidx]);
1126 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1127 d->sbdma_dscrtable_phys) /
1128 sizeof(*d->sbdma_dscrtable);
1131 * If they're the same, that means we've processed all
1132 * of the descriptors up to (but not including) the one that
1133 * the hardware is working on right now.
1136 if (curidx == hwidx)
1137 goto done;
1140 * Otherwise, get the packet's sk_buff ptr back
1143 sb = d->sbdma_ctxtable[curidx];
1144 d->sbdma_ctxtable[curidx] = NULL;
1146 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
1149 * Check packet status. If good, process it.
1150 * If not, silently drop it and put it back on the
1151 * receive ring.
1154 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
1157 * Add a new buffer to replace the old one. If we fail
1158 * to allocate a buffer, we're going to drop this
1159 * packet and put it right back on the receive ring.
1162 if (unlikely (sbdma_add_rcvbuffer(d,NULL) ==
1163 -ENOBUFS)) {
1164 dev->stats.rx_dropped++;
1165 sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
1166 /* No point in continuing at the moment */
1167 printk(KERN_ERR "dropped packet (1)\n");
1168 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1169 goto done;
1170 } else {
1172 * Set length into the packet
1174 skb_put(sb,len);
1177 * Buffer has been replaced on the
1178 * receive ring. Pass the buffer to
1179 * the kernel
1181 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1182 /* Check hw IPv4/TCP checksum if supported */
1183 if (sc->rx_hw_checksum == ENABLE) {
1184 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1185 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1186 sb->ip_summed = CHECKSUM_UNNECESSARY;
1187 /* don't need to set sb->csum */
1188 } else {
1189 sb->ip_summed = CHECKSUM_NONE;
1192 prefetch(sb->data);
1193 prefetch((const void *)(((char *)sb->data)+32));
1194 if (poll)
1195 dropped = netif_receive_skb(sb);
1196 else
1197 dropped = netif_rx(sb);
1199 if (dropped == NET_RX_DROP) {
1200 dev->stats.rx_dropped++;
1201 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1202 goto done;
1204 else {
1205 dev->stats.rx_bytes += len;
1206 dev->stats.rx_packets++;
1209 } else {
1211 * Packet was mangled somehow. Just drop it and
1212 * put it back on the receive ring.
1214 dev->stats.rx_errors++;
1215 sbdma_add_rcvbuffer(d,sb);
1220 * .. and advance to the next buffer.
1223 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1224 work_done++;
1226 if (!poll) {
1227 work_to_do = 32;
1228 goto again; /* collect fifo drop statistics again */
1230 done:
1231 return work_done;
1234 /**********************************************************************
1235 * SBDMA_TX_PROCESS(sc,d)
1237 * Process "completed" transmit buffers on the specified DMA channel.
1238 * This is normally called within the interrupt service routine.
1239 * Note that this isn't really ideal for priority channels, since
1240 * it processes all of the packets on a given channel before
1241 * returning.
1243 * Input parameters:
1244 * sc - softc structure
1245 * d - DMA channel context
1246 * poll - 1: using polling (for NAPI)
1248 * Return value:
1249 * nothing
1250 ********************************************************************* */
1252 static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1253 int poll)
1255 struct net_device *dev = sc->sbm_dev;
1256 int curidx;
1257 int hwidx;
1258 struct sbdmadscr *dsc;
1259 struct sk_buff *sb;
1260 unsigned long flags;
1261 int packets_handled = 0;
1263 spin_lock_irqsave(&(sc->sbm_lock), flags);
1265 if (d->sbdma_remptr == d->sbdma_addptr)
1266 goto end_unlock;
1268 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1269 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
1271 for (;;) {
1273 * figure out where we are (as an index) and where
1274 * the hardware is (also as an index)
1276 * This could be done faster if (for example) the
1277 * descriptor table was page-aligned and contiguous in
1278 * both virtual and physical memory -- you could then
1279 * just compare the low-order bits of the virtual address
1280 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1283 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
1286 * If they're the same, that means we've processed all
1287 * of the descriptors up to (but not including) the one that
1288 * the hardware is working on right now.
1291 if (curidx == hwidx)
1292 break;
1295 * Otherwise, get the packet's sk_buff ptr back
1298 dsc = &(d->sbdma_dscrtable[curidx]);
1299 sb = d->sbdma_ctxtable[curidx];
1300 d->sbdma_ctxtable[curidx] = NULL;
1303 * Stats
1306 dev->stats.tx_bytes += sb->len;
1307 dev->stats.tx_packets++;
1310 * for transmits, we just free buffers.
1313 dev_kfree_skb_irq(sb);
1316 * .. and advance to the next buffer.
1319 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1321 packets_handled++;
1326 * Decide if we should wake up the protocol or not.
1327 * Other drivers seem to do this when we reach a low
1328 * watermark on the transmit queue.
1331 if (packets_handled)
1332 netif_wake_queue(d->sbdma_eth->sbm_dev);
1334 end_unlock:
1335 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
1341 /**********************************************************************
1342 * SBMAC_INITCTX(s)
1344 * Initialize an Ethernet context structure - this is called
1345 * once per MAC on the 1250. Memory is allocated here, so don't
1346 * call it again from inside the ioctl routines that bring the
1347 * interface up/down
1349 * Input parameters:
1350 * s - sbmac context structure
1352 * Return value:
1354 ********************************************************************* */
1356 static int sbmac_initctx(struct sbmac_softc *s)
1360 * figure out the addresses of some ports
1363 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1364 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1365 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1366 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1367 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1368 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1369 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1370 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1373 * Initialize the DMA channels. Right now, only one per MAC is used
1374 * Note: Only do this _once_, as it allocates memory from the kernel!
1377 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1378 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
1381 * initial state is OFF
1384 s->sbm_state = sbmac_state_off;
1386 return 0;
1390 static void sbdma_uninitctx(struct sbmacdma *d)
1392 if (d->sbdma_dscrtable_unaligned) {
1393 kfree(d->sbdma_dscrtable_unaligned);
1394 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
1397 if (d->sbdma_ctxtable) {
1398 kfree(d->sbdma_ctxtable);
1399 d->sbdma_ctxtable = NULL;
1404 static void sbmac_uninitctx(struct sbmac_softc *sc)
1406 sbdma_uninitctx(&(sc->sbm_txdma));
1407 sbdma_uninitctx(&(sc->sbm_rxdma));
1411 /**********************************************************************
1412 * SBMAC_CHANNEL_START(s)
1414 * Start packet processing on this MAC.
1416 * Input parameters:
1417 * s - sbmac structure
1419 * Return value:
1420 * nothing
1421 ********************************************************************* */
1423 static void sbmac_channel_start(struct sbmac_softc *s)
1425 uint64_t reg;
1426 void __iomem *port;
1427 uint64_t cfg,fifo,framecfg;
1428 int idx, th_value;
1431 * Don't do this if running
1434 if (s->sbm_state == sbmac_state_on)
1435 return;
1438 * Bring the controller out of reset, but leave it off.
1441 __raw_writeq(0, s->sbm_macenable);
1444 * Ignore all received packets
1447 __raw_writeq(0, s->sbm_rxfilter);
1450 * Calculate values for various control registers.
1453 cfg = M_MAC_RETRY_EN |
1454 M_MAC_TX_HOLD_SOP_EN |
1455 V_MAC_TX_PAUSE_CNT_16K |
1456 M_MAC_AP_STAT_EN |
1457 M_MAC_FAST_SYNC |
1458 M_MAC_SS_EN |
1462 * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1463 * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1464 * Use a larger RD_THRSH for gigabit
1466 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
1467 th_value = 28;
1468 else
1469 th_value = 64;
1471 fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1472 ((s->sbm_speed == sbmac_speed_1000)
1473 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1474 V_MAC_TX_RL_THRSH(4) |
1475 V_MAC_RX_PL_THRSH(4) |
1476 V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
1477 V_MAC_RX_PL_THRSH(4) |
1478 V_MAC_RX_RL_THRSH(8) |
1481 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1482 V_MAC_MAX_FRAMESZ_DEFAULT |
1483 V_MAC_BACKOFF_SEL(1);
1486 * Clear out the hash address map
1489 port = s->sbm_base + R_MAC_HASH_BASE;
1490 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
1491 __raw_writeq(0, port);
1492 port += sizeof(uint64_t);
1496 * Clear out the exact-match table
1499 port = s->sbm_base + R_MAC_ADDR_BASE;
1500 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
1501 __raw_writeq(0, port);
1502 port += sizeof(uint64_t);
1506 * Clear out the DMA Channel mapping table registers
1509 port = s->sbm_base + R_MAC_CHUP0_BASE;
1510 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1511 __raw_writeq(0, port);
1512 port += sizeof(uint64_t);
1516 port = s->sbm_base + R_MAC_CHLO0_BASE;
1517 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1518 __raw_writeq(0, port);
1519 port += sizeof(uint64_t);
1523 * Program the hardware address. It goes into the hardware-address
1524 * register as well as the first filter register.
1527 reg = sbmac_addr2reg(s->sbm_hwaddr);
1529 port = s->sbm_base + R_MAC_ADDR_BASE;
1530 __raw_writeq(reg, port);
1531 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1533 #ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
1535 * Pass1 SOCs do not receive packets addressed to the
1536 * destination address in the R_MAC_ETHERNET_ADDR register.
1537 * Set the value to zero.
1539 __raw_writeq(0, port);
1540 #else
1541 __raw_writeq(reg, port);
1542 #endif
1545 * Set the receive filter for no packets, and write values
1546 * to the various config registers
1549 __raw_writeq(0, s->sbm_rxfilter);
1550 __raw_writeq(0, s->sbm_imr);
1551 __raw_writeq(framecfg, s->sbm_framecfg);
1552 __raw_writeq(fifo, s->sbm_fifocfg);
1553 __raw_writeq(cfg, s->sbm_maccfg);
1556 * Initialize DMA channels (rings should be ok now)
1559 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1560 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
1563 * Configure the speed, duplex, and flow control
1566 sbmac_set_speed(s,s->sbm_speed);
1567 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
1570 * Fill the receive ring
1573 sbdma_fillring(&(s->sbm_rxdma));
1576 * Turn on the rest of the bits in the enable register
1579 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1580 __raw_writeq(M_MAC_RXDMA_EN0 |
1581 M_MAC_TXDMA_EN0, s->sbm_macenable);
1582 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1583 __raw_writeq(M_MAC_RXDMA_EN0 |
1584 M_MAC_TXDMA_EN0 |
1585 M_MAC_RX_ENABLE |
1586 M_MAC_TX_ENABLE, s->sbm_macenable);
1587 #else
1588 #error invalid SiByte MAC configuation
1589 #endif
1591 #ifdef CONFIG_SBMAC_COALESCE
1592 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1593 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
1594 #else
1595 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1596 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
1597 #endif
1600 * Enable receiving unicasts and broadcasts
1603 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1606 * we're running now.
1609 s->sbm_state = sbmac_state_on;
1612 * Program multicast addresses
1615 sbmac_setmulti(s);
1618 * If channel was in promiscuous mode before, turn that on
1621 if (s->sbm_devflags & IFF_PROMISC) {
1622 sbmac_promiscuous_mode(s,1);
1628 /**********************************************************************
1629 * SBMAC_CHANNEL_STOP(s)
1631 * Stop packet processing on this MAC.
1633 * Input parameters:
1634 * s - sbmac structure
1636 * Return value:
1637 * nothing
1638 ********************************************************************* */
1640 static void sbmac_channel_stop(struct sbmac_softc *s)
1642 /* don't do this if already stopped */
1644 if (s->sbm_state == sbmac_state_off)
1645 return;
1647 /* don't accept any packets, disable all interrupts */
1649 __raw_writeq(0, s->sbm_rxfilter);
1650 __raw_writeq(0, s->sbm_imr);
1652 /* Turn off ticker */
1654 /* XXX */
1656 /* turn off receiver and transmitter */
1658 __raw_writeq(0, s->sbm_macenable);
1660 /* We're stopped now. */
1662 s->sbm_state = sbmac_state_off;
1665 * Stop DMA channels (rings should be ok now)
1668 sbdma_channel_stop(&(s->sbm_rxdma));
1669 sbdma_channel_stop(&(s->sbm_txdma));
1671 /* Empty the receive and transmit rings */
1673 sbdma_emptyring(&(s->sbm_rxdma));
1674 sbdma_emptyring(&(s->sbm_txdma));
1678 /**********************************************************************
1679 * SBMAC_SET_CHANNEL_STATE(state)
1681 * Set the channel's state ON or OFF
1683 * Input parameters:
1684 * state - new state
1686 * Return value:
1687 * old state
1688 ********************************************************************* */
1689 static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1690 enum sbmac_state state)
1692 enum sbmac_state oldstate = sc->sbm_state;
1695 * If same as previous state, return
1698 if (state == oldstate) {
1699 return oldstate;
1703 * If new state is ON, turn channel on
1706 if (state == sbmac_state_on) {
1707 sbmac_channel_start(sc);
1709 else {
1710 sbmac_channel_stop(sc);
1714 * Return previous state
1717 return oldstate;
1721 /**********************************************************************
1722 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
1724 * Turn on or off promiscuous mode
1726 * Input parameters:
1727 * sc - softc
1728 * onoff - 1 to turn on, 0 to turn off
1730 * Return value:
1731 * nothing
1732 ********************************************************************* */
1734 static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1736 uint64_t reg;
1738 if (sc->sbm_state != sbmac_state_on)
1739 return;
1741 if (onoff) {
1742 reg = __raw_readq(sc->sbm_rxfilter);
1743 reg |= M_MAC_ALLPKT_EN;
1744 __raw_writeq(reg, sc->sbm_rxfilter);
1746 else {
1747 reg = __raw_readq(sc->sbm_rxfilter);
1748 reg &= ~M_MAC_ALLPKT_EN;
1749 __raw_writeq(reg, sc->sbm_rxfilter);
1753 /**********************************************************************
1754 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
1756 * Set the iphdr offset as 15 assuming ethernet encapsulation
1758 * Input parameters:
1759 * sc - softc
1761 * Return value:
1762 * nothing
1763 ********************************************************************* */
1765 static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1767 uint64_t reg;
1769 /* Hard code the off set to 15 for now */
1770 reg = __raw_readq(sc->sbm_rxfilter);
1771 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
1772 __raw_writeq(reg, sc->sbm_rxfilter);
1774 /* BCM1250 pass1 didn't have hardware checksum. Everything
1775 later does. */
1776 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
1777 sc->rx_hw_checksum = DISABLE;
1778 } else {
1779 sc->rx_hw_checksum = ENABLE;
1784 /**********************************************************************
1785 * SBMAC_ADDR2REG(ptr)
1787 * Convert six bytes into the 64-bit register value that
1788 * we typically write into the SBMAC's address/mcast registers
1790 * Input parameters:
1791 * ptr - pointer to 6 bytes
1793 * Return value:
1794 * register value
1795 ********************************************************************* */
1797 static uint64_t sbmac_addr2reg(unsigned char *ptr)
1799 uint64_t reg = 0;
1801 ptr += 6;
1803 reg |= (uint64_t) *(--ptr);
1804 reg <<= 8;
1805 reg |= (uint64_t) *(--ptr);
1806 reg <<= 8;
1807 reg |= (uint64_t) *(--ptr);
1808 reg <<= 8;
1809 reg |= (uint64_t) *(--ptr);
1810 reg <<= 8;
1811 reg |= (uint64_t) *(--ptr);
1812 reg <<= 8;
1813 reg |= (uint64_t) *(--ptr);
1815 return reg;
1819 /**********************************************************************
1820 * SBMAC_SET_SPEED(s,speed)
1822 * Configure LAN speed for the specified MAC.
1823 * Warning: must be called when MAC is off!
1825 * Input parameters:
1826 * s - sbmac structure
1827 * speed - speed to set MAC to (see enum sbmac_speed)
1829 * Return value:
1830 * 1 if successful
1831 * 0 indicates invalid parameters
1832 ********************************************************************* */
1834 static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
1836 uint64_t cfg;
1837 uint64_t framecfg;
1840 * Save new current values
1843 s->sbm_speed = speed;
1845 if (s->sbm_state == sbmac_state_on)
1846 return 0; /* save for next restart */
1849 * Read current register values
1852 cfg = __raw_readq(s->sbm_maccfg);
1853 framecfg = __raw_readq(s->sbm_framecfg);
1856 * Mask out the stuff we want to change
1859 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1860 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1861 M_MAC_SLOT_SIZE);
1864 * Now add in the new bits
1867 switch (speed) {
1868 case sbmac_speed_10:
1869 framecfg |= V_MAC_IFG_RX_10 |
1870 V_MAC_IFG_TX_10 |
1871 K_MAC_IFG_THRSH_10 |
1872 V_MAC_SLOT_SIZE_10;
1873 cfg |= V_MAC_SPEED_SEL_10MBPS;
1874 break;
1876 case sbmac_speed_100:
1877 framecfg |= V_MAC_IFG_RX_100 |
1878 V_MAC_IFG_TX_100 |
1879 V_MAC_IFG_THRSH_100 |
1880 V_MAC_SLOT_SIZE_100;
1881 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1882 break;
1884 case sbmac_speed_1000:
1885 framecfg |= V_MAC_IFG_RX_1000 |
1886 V_MAC_IFG_TX_1000 |
1887 V_MAC_IFG_THRSH_1000 |
1888 V_MAC_SLOT_SIZE_1000;
1889 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1890 break;
1892 default:
1893 return 0;
1897 * Send the bits back to the hardware
1900 __raw_writeq(framecfg, s->sbm_framecfg);
1901 __raw_writeq(cfg, s->sbm_maccfg);
1903 return 1;
1906 /**********************************************************************
1907 * SBMAC_SET_DUPLEX(s,duplex,fc)
1909 * Set Ethernet duplex and flow control options for this MAC
1910 * Warning: must be called when MAC is off!
1912 * Input parameters:
1913 * s - sbmac structure
1914 * duplex - duplex setting (see enum sbmac_duplex)
1915 * fc - flow control setting (see enum sbmac_fc)
1917 * Return value:
1918 * 1 if ok
1919 * 0 if an invalid parameter combination was specified
1920 ********************************************************************* */
1922 static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1923 enum sbmac_fc fc)
1925 uint64_t cfg;
1928 * Save new current values
1931 s->sbm_duplex = duplex;
1932 s->sbm_fc = fc;
1934 if (s->sbm_state == sbmac_state_on)
1935 return 0; /* save for next restart */
1938 * Read current register values
1941 cfg = __raw_readq(s->sbm_maccfg);
1944 * Mask off the stuff we're about to change
1947 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
1950 switch (duplex) {
1951 case sbmac_duplex_half:
1952 switch (fc) {
1953 case sbmac_fc_disabled:
1954 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1955 break;
1957 case sbmac_fc_collision:
1958 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1959 break;
1961 case sbmac_fc_carrier:
1962 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1963 break;
1965 case sbmac_fc_frame: /* not valid in half duplex */
1966 default: /* invalid selection */
1967 return 0;
1969 break;
1971 case sbmac_duplex_full:
1972 switch (fc) {
1973 case sbmac_fc_disabled:
1974 cfg |= V_MAC_FC_CMD_DISABLED;
1975 break;
1977 case sbmac_fc_frame:
1978 cfg |= V_MAC_FC_CMD_ENABLED;
1979 break;
1981 case sbmac_fc_collision: /* not valid in full duplex */
1982 case sbmac_fc_carrier: /* not valid in full duplex */
1983 default:
1984 return 0;
1986 break;
1987 default:
1988 return 0;
1992 * Send the bits back to the hardware
1995 __raw_writeq(cfg, s->sbm_maccfg);
1997 return 1;
2003 /**********************************************************************
2004 * SBMAC_INTR()
2006 * Interrupt handler for MAC interrupts
2008 * Input parameters:
2009 * MAC structure
2011 * Return value:
2012 * nothing
2013 ********************************************************************* */
2014 static irqreturn_t sbmac_intr(int irq,void *dev_instance)
2016 struct net_device *dev = (struct net_device *) dev_instance;
2017 struct sbmac_softc *sc = netdev_priv(dev);
2018 uint64_t isr;
2019 int handled = 0;
2022 * Read the ISR (this clears the bits in the real
2023 * register, except for counter addr)
2026 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
2028 if (isr == 0)
2029 return IRQ_RETVAL(0);
2030 handled = 1;
2033 * Transmits on channel 0
2036 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
2037 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
2039 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
2040 if (netif_rx_schedule_prep(dev, &sc->napi)) {
2041 __raw_writeq(0, sc->sbm_imr);
2042 __netif_rx_schedule(dev, &sc->napi);
2043 /* Depend on the exit from poll to reenable intr */
2045 else {
2046 /* may leave some packets behind */
2047 sbdma_rx_process(sc,&(sc->sbm_rxdma),
2048 SBMAC_MAX_RXDESCR * 2, 0);
2051 return IRQ_RETVAL(handled);
2054 /**********************************************************************
2055 * SBMAC_START_TX(skb,dev)
2057 * Start output on the specified interface. Basically, we
2058 * queue as many buffers as we can until the ring fills up, or
2059 * we run off the end of the queue, whichever comes first.
2061 * Input parameters:
2064 * Return value:
2065 * nothing
2066 ********************************************************************* */
2067 static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2069 struct sbmac_softc *sc = netdev_priv(dev);
2071 /* lock eth irq */
2072 spin_lock_irq (&sc->sbm_lock);
2075 * Put the buffer on the transmit ring. If we
2076 * don't have room, stop the queue.
2079 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2080 /* XXX save skb that we could not send */
2081 netif_stop_queue(dev);
2082 spin_unlock_irq(&sc->sbm_lock);
2084 return 1;
2087 dev->trans_start = jiffies;
2089 spin_unlock_irq (&sc->sbm_lock);
2091 return 0;
2094 /**********************************************************************
2095 * SBMAC_SETMULTI(sc)
2097 * Reprogram the multicast table into the hardware, given
2098 * the list of multicasts associated with the interface
2099 * structure.
2101 * Input parameters:
2102 * sc - softc
2104 * Return value:
2105 * nothing
2106 ********************************************************************* */
2108 static void sbmac_setmulti(struct sbmac_softc *sc)
2110 uint64_t reg;
2111 void __iomem *port;
2112 int idx;
2113 struct dev_mc_list *mclist;
2114 struct net_device *dev = sc->sbm_dev;
2117 * Clear out entire multicast table. We do this by nuking
2118 * the entire hash table and all the direct matches except
2119 * the first one, which is used for our station address
2122 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2123 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
2124 __raw_writeq(0, port);
2127 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2128 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
2129 __raw_writeq(0, port);
2133 * Clear the filter to say we don't want any multicasts.
2136 reg = __raw_readq(sc->sbm_rxfilter);
2137 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2138 __raw_writeq(reg, sc->sbm_rxfilter);
2140 if (dev->flags & IFF_ALLMULTI) {
2142 * Enable ALL multicasts. Do this by inverting the
2143 * multicast enable bit.
2145 reg = __raw_readq(sc->sbm_rxfilter);
2146 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2147 __raw_writeq(reg, sc->sbm_rxfilter);
2148 return;
2153 * Progam new multicast entries. For now, only use the
2154 * perfect filter. In the future we'll need to use the
2155 * hash filter if the perfect filter overflows
2158 /* XXX only using perfect filter for now, need to use hash
2159 * XXX if the table overflows */
2161 idx = 1; /* skip station address */
2162 mclist = dev->mc_list;
2163 while (mclist && (idx < MAC_ADDR_COUNT)) {
2164 reg = sbmac_addr2reg(mclist->dmi_addr);
2165 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
2166 __raw_writeq(reg, port);
2167 idx++;
2168 mclist = mclist->next;
2172 * Enable the "accept multicast bits" if we programmed at least one
2173 * multicast.
2176 if (idx > 1) {
2177 reg = __raw_readq(sc->sbm_rxfilter);
2178 reg |= M_MAC_MCAST_EN;
2179 __raw_writeq(reg, sc->sbm_rxfilter);
2183 #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
2184 /**********************************************************************
2185 * SBMAC_PARSE_XDIGIT(str)
2187 * Parse a hex digit, returning its value
2189 * Input parameters:
2190 * str - character
2192 * Return value:
2193 * hex value, or -1 if invalid
2194 ********************************************************************* */
2196 static int sbmac_parse_xdigit(char str)
2198 int digit;
2200 if ((str >= '0') && (str <= '9'))
2201 digit = str - '0';
2202 else if ((str >= 'a') && (str <= 'f'))
2203 digit = str - 'a' + 10;
2204 else if ((str >= 'A') && (str <= 'F'))
2205 digit = str - 'A' + 10;
2206 else
2207 return -1;
2209 return digit;
2212 /**********************************************************************
2213 * SBMAC_PARSE_HWADDR(str,hwaddr)
2215 * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
2216 * Ethernet address.
2218 * Input parameters:
2219 * str - string
2220 * hwaddr - pointer to hardware address
2222 * Return value:
2223 * 0 if ok, else -1
2224 ********************************************************************* */
2226 static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr)
2228 int digit1,digit2;
2229 int idx = 6;
2231 while (*str && (idx > 0)) {
2232 digit1 = sbmac_parse_xdigit(*str);
2233 if (digit1 < 0)
2234 return -1;
2235 str++;
2236 if (!*str)
2237 return -1;
2239 if ((*str == ':') || (*str == '-')) {
2240 digit2 = digit1;
2241 digit1 = 0;
2243 else {
2244 digit2 = sbmac_parse_xdigit(*str);
2245 if (digit2 < 0)
2246 return -1;
2247 str++;
2250 *hwaddr++ = (digit1 << 4) | digit2;
2251 idx--;
2253 if (*str == '-')
2254 str++;
2255 if (*str == ':')
2256 str++;
2258 return 0;
2260 #endif
2262 static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2264 if (new_mtu > ENET_PACKET_SIZE)
2265 return -EINVAL;
2266 _dev->mtu = new_mtu;
2267 pr_info("changing the mtu to %d\n", new_mtu);
2268 return 0;
2271 /**********************************************************************
2272 * SBMAC_INIT(dev)
2274 * Attach routine - init hardware and hook ourselves into linux
2276 * Input parameters:
2277 * dev - net_device structure
2279 * Return value:
2280 * status
2281 ********************************************************************* */
2283 static int sbmac_init(struct platform_device *pldev, long long base)
2285 struct net_device *dev = pldev->dev.driver_data;
2286 int idx = pldev->id;
2287 struct sbmac_softc *sc = netdev_priv(dev);
2288 unsigned char *eaddr;
2289 uint64_t ea_reg;
2290 int i;
2291 int err;
2292 DECLARE_MAC_BUF(mac);
2294 sc->sbm_dev = dev;
2295 sc->sbe_idx = idx;
2297 eaddr = sc->sbm_hwaddr;
2300 * Read the ethernet address. The firwmare left this programmed
2301 * for us in the ethernet address register for each mac.
2304 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2305 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
2306 for (i = 0; i < 6; i++) {
2307 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2308 ea_reg >>= 8;
2311 for (i = 0; i < 6; i++) {
2312 dev->dev_addr[i] = eaddr[i];
2317 * Init packet size
2320 sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;
2323 * Initialize context (get pointers to registers and stuff), then
2324 * allocate the memory for the descriptor tables.
2327 sbmac_initctx(sc);
2330 * Set up Linux device callins
2333 spin_lock_init(&(sc->sbm_lock));
2335 dev->open = sbmac_open;
2336 dev->hard_start_xmit = sbmac_start_tx;
2337 dev->stop = sbmac_close;
2338 dev->set_multicast_list = sbmac_set_rx_mode;
2339 dev->do_ioctl = sbmac_mii_ioctl;
2340 dev->tx_timeout = sbmac_tx_timeout;
2341 dev->watchdog_timeo = TX_TIMEOUT;
2343 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
2345 dev->change_mtu = sb1250_change_mtu;
2346 #ifdef CONFIG_NET_POLL_CONTROLLER
2347 dev->poll_controller = sbmac_netpoll;
2348 #endif
2350 dev->irq = UNIT_INT(idx);
2352 /* This is needed for PASS2 for Rx H/W checksum feature */
2353 sbmac_set_iphdr_offset(sc);
2355 err = register_netdev(dev);
2356 if (err) {
2357 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2358 sbmac_string, idx);
2359 sbmac_uninitctx(sc);
2360 return err;
2363 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2365 if (sc->rx_hw_checksum == ENABLE)
2366 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2369 * Display Ethernet address (this is called during the config
2370 * process so we need to finish off the config message that
2371 * was being displayed)
2373 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %s\n",
2374 dev->name, base, print_mac(mac, eaddr));
2376 sc->mii_bus.name = sbmac_mdio_string;
2377 sc->mii_bus.id = idx;
2378 sc->mii_bus.priv = sc;
2379 sc->mii_bus.read = sbmac_mii_read;
2380 sc->mii_bus.write = sbmac_mii_write;
2381 sc->mii_bus.irq = sc->phy_irq;
2382 for (i = 0; i < PHY_MAX_ADDR; ++i)
2383 sc->mii_bus.irq[i] = SBMAC_PHY_INT;
2385 sc->mii_bus.dev = &pldev->dev;
2386 dev_set_drvdata(&pldev->dev, &sc->mii_bus);
2388 return 0;
2392 static int sbmac_open(struct net_device *dev)
2394 struct sbmac_softc *sc = netdev_priv(dev);
2395 int err;
2397 if (debug > 1)
2398 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2401 * map/route interrupt (clear status first, in case something
2402 * weird is pending; we haven't initialized the mac registers
2403 * yet)
2406 __raw_readq(sc->sbm_isr);
2407 err = request_irq(dev->irq, &sbmac_intr, IRQF_SHARED, dev->name, dev);
2408 if (err) {
2409 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2410 dev->irq);
2411 goto out_err;
2415 * Probe PHY address
2417 err = mdiobus_register(&sc->mii_bus);
2418 if (err) {
2419 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2420 dev->name);
2421 goto out_unirq;
2424 sc->sbm_speed = sbmac_speed_none;
2425 sc->sbm_duplex = sbmac_duplex_none;
2426 sc->sbm_fc = sbmac_fc_none;
2427 sc->sbm_pause = -1;
2428 sc->sbm_link = 0;
2431 * Attach to the PHY
2433 err = sbmac_mii_probe(dev);
2434 if (err)
2435 goto out_unregister;
2438 * Turn on the channel
2441 sbmac_set_channel_state(sc,sbmac_state_on);
2443 netif_start_queue(dev);
2445 sbmac_set_rx_mode(dev);
2447 phy_start(sc->phy_dev);
2449 napi_enable(&sc->napi);
2451 return 0;
2453 out_unregister:
2454 mdiobus_unregister(&sc->mii_bus);
2456 out_unirq:
2457 free_irq(dev->irq, dev);
2459 out_err:
2460 return err;
2463 static int sbmac_mii_probe(struct net_device *dev)
2465 struct sbmac_softc *sc = netdev_priv(dev);
2466 struct phy_device *phy_dev;
2467 int i;
2469 for (i = 0; i < PHY_MAX_ADDR; i++) {
2470 phy_dev = sc->mii_bus.phy_map[i];
2471 if (phy_dev)
2472 break;
2474 if (!phy_dev) {
2475 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2476 return -ENXIO;
2479 phy_dev = phy_connect(dev, phy_dev->dev.bus_id, &sbmac_mii_poll, 0,
2480 PHY_INTERFACE_MODE_GMII);
2481 if (IS_ERR(phy_dev)) {
2482 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2483 return PTR_ERR(phy_dev);
2486 /* Remove any features not supported by the controller */
2487 phy_dev->supported &= SUPPORTED_10baseT_Half |
2488 SUPPORTED_10baseT_Full |
2489 SUPPORTED_100baseT_Half |
2490 SUPPORTED_100baseT_Full |
2491 SUPPORTED_1000baseT_Half |
2492 SUPPORTED_1000baseT_Full |
2493 SUPPORTED_Autoneg |
2494 SUPPORTED_MII |
2495 SUPPORTED_Pause |
2496 SUPPORTED_Asym_Pause;
2497 phy_dev->advertising = phy_dev->supported;
2499 pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2500 dev->name, phy_dev->drv->name,
2501 phy_dev->dev.bus_id, phy_dev->irq);
2503 sc->phy_dev = phy_dev;
2505 return 0;
2509 static void sbmac_mii_poll(struct net_device *dev)
2511 struct sbmac_softc *sc = netdev_priv(dev);
2512 struct phy_device *phy_dev = sc->phy_dev;
2513 unsigned long flags;
2514 enum sbmac_fc fc;
2515 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2517 link_chg = (sc->sbm_link != phy_dev->link);
2518 speed_chg = (sc->sbm_speed != phy_dev->speed);
2519 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2520 pause_chg = (sc->sbm_pause != phy_dev->pause);
2522 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2523 return; /* Hmmm... */
2525 if (!phy_dev->link) {
2526 if (link_chg) {
2527 sc->sbm_link = phy_dev->link;
2528 sc->sbm_speed = sbmac_speed_none;
2529 sc->sbm_duplex = sbmac_duplex_none;
2530 sc->sbm_fc = sbmac_fc_disabled;
2531 sc->sbm_pause = -1;
2532 pr_info("%s: link unavailable\n", dev->name);
2534 return;
2537 if (phy_dev->duplex == DUPLEX_FULL) {
2538 if (phy_dev->pause)
2539 fc = sbmac_fc_frame;
2540 else
2541 fc = sbmac_fc_disabled;
2542 } else
2543 fc = sbmac_fc_collision;
2544 fc_chg = (sc->sbm_fc != fc);
2546 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2547 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2549 spin_lock_irqsave(&sc->sbm_lock, flags);
2551 sc->sbm_speed = phy_dev->speed;
2552 sc->sbm_duplex = phy_dev->duplex;
2553 sc->sbm_fc = fc;
2554 sc->sbm_pause = phy_dev->pause;
2555 sc->sbm_link = phy_dev->link;
2557 if ((speed_chg || duplex_chg || fc_chg) &&
2558 sc->sbm_state != sbmac_state_off) {
2560 * something changed, restart the channel
2562 if (debug > 1)
2563 pr_debug("%s: restarting channel "
2564 "because PHY state changed\n", dev->name);
2565 sbmac_channel_stop(sc);
2566 sbmac_channel_start(sc);
2569 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2573 static void sbmac_tx_timeout (struct net_device *dev)
2575 struct sbmac_softc *sc = netdev_priv(dev);
2577 spin_lock_irq (&sc->sbm_lock);
2580 dev->trans_start = jiffies;
2581 dev->stats.tx_errors++;
2583 spin_unlock_irq (&sc->sbm_lock);
2585 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2591 static void sbmac_set_rx_mode(struct net_device *dev)
2593 unsigned long flags;
2594 struct sbmac_softc *sc = netdev_priv(dev);
2596 spin_lock_irqsave(&sc->sbm_lock, flags);
2597 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2599 * Promiscuous changed.
2602 if (dev->flags & IFF_PROMISC) {
2603 sbmac_promiscuous_mode(sc,1);
2605 else {
2606 sbmac_promiscuous_mode(sc,0);
2609 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2612 * Program the multicasts. Do this every time.
2615 sbmac_setmulti(sc);
2619 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2621 struct sbmac_softc *sc = netdev_priv(dev);
2623 if (!netif_running(dev) || !sc->phy_dev)
2624 return -EINVAL;
2626 return phy_mii_ioctl(sc->phy_dev, if_mii(rq), cmd);
2629 static int sbmac_close(struct net_device *dev)
2631 struct sbmac_softc *sc = netdev_priv(dev);
2633 napi_disable(&sc->napi);
2635 phy_stop(sc->phy_dev);
2637 sbmac_set_channel_state(sc, sbmac_state_off);
2639 netif_stop_queue(dev);
2641 if (debug > 1)
2642 pr_debug("%s: Shutting down ethercard\n", dev->name);
2644 phy_disconnect(sc->phy_dev);
2645 sc->phy_dev = NULL;
2647 mdiobus_unregister(&sc->mii_bus);
2649 free_irq(dev->irq, dev);
2651 sbdma_emptyring(&(sc->sbm_txdma));
2652 sbdma_emptyring(&(sc->sbm_rxdma));
2654 return 0;
2657 static int sbmac_poll(struct napi_struct *napi, int budget)
2659 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2660 struct net_device *dev = sc->sbm_dev;
2661 int work_done;
2663 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
2664 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2666 if (work_done < budget) {
2667 netif_rx_complete(dev, napi);
2669 #ifdef CONFIG_SBMAC_COALESCE
2670 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2671 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2672 sc->sbm_imr);
2673 #else
2674 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2675 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2676 #endif
2679 return work_done;
2683 static int __init sbmac_probe(struct platform_device *pldev)
2685 struct net_device *dev;
2686 struct sbmac_softc *sc;
2687 void __iomem *sbm_base;
2688 struct resource *res;
2689 u64 sbmac_orig_hwaddr;
2690 int err;
2692 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2693 BUG_ON(!res);
2694 sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
2695 if (!sbm_base) {
2696 printk(KERN_ERR "%s: unable to map device registers\n",
2697 pldev->dev.bus_id);
2698 err = -ENOMEM;
2699 goto out_out;
2703 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2704 * value for us by the firmware if we're going to use this MAC.
2705 * If we find a zero, skip this MAC.
2707 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2708 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", pldev->dev.bus_id,
2709 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2710 if (sbmac_orig_hwaddr == 0) {
2711 err = 0;
2712 goto out_unmap;
2716 * Okay, cool. Initialize this MAC.
2718 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2719 if (!dev) {
2720 printk(KERN_ERR "%s: unable to allocate etherdev\n",
2721 pldev->dev.bus_id);
2722 err = -ENOMEM;
2723 goto out_unmap;
2726 pldev->dev.driver_data = dev;
2727 SET_NETDEV_DEV(dev, &pldev->dev);
2729 sc = netdev_priv(dev);
2730 sc->sbm_base = sbm_base;
2732 err = sbmac_init(pldev, res->start);
2733 if (err)
2734 goto out_kfree;
2736 return 0;
2738 out_kfree:
2739 free_netdev(dev);
2740 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2742 out_unmap:
2743 iounmap(sbm_base);
2745 out_out:
2746 return err;
2749 static int __exit sbmac_remove(struct platform_device *pldev)
2751 struct net_device *dev = pldev->dev.driver_data;
2752 struct sbmac_softc *sc = netdev_priv(dev);
2754 unregister_netdev(dev);
2755 sbmac_uninitctx(sc);
2756 iounmap(sc->sbm_base);
2757 free_netdev(dev);
2759 return 0;
2763 static struct platform_device **sbmac_pldev;
2764 static int sbmac_max_units;
2766 #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
2767 static void __init sbmac_setup_hwaddr(int idx, char *addr)
2769 void __iomem *sbm_base;
2770 unsigned long start, end;
2771 uint8_t eaddr[6];
2772 uint64_t val;
2774 if (idx >= sbmac_max_units)
2775 return;
2777 start = A_MAC_CHANNEL_BASE(idx);
2778 end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2780 sbm_base = ioremap_nocache(start, end - start + 1);
2781 if (!sbm_base) {
2782 printk(KERN_ERR "%s: unable to map device registers\n",
2783 sbmac_string);
2784 return;
2787 sbmac_parse_hwaddr(addr, eaddr);
2788 val = sbmac_addr2reg(eaddr);
2789 __raw_writeq(val, sbm_base + R_MAC_ETHERNET_ADDR);
2790 val = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2792 iounmap(sbm_base);
2794 #endif
2796 static int __init sbmac_platform_probe_one(int idx)
2798 struct platform_device *pldev;
2799 struct {
2800 struct resource r;
2801 char name[strlen(sbmac_pretty) + 4];
2802 } *res;
2803 int err;
2805 res = kzalloc(sizeof(*res), GFP_KERNEL);
2806 if (!res) {
2807 printk(KERN_ERR "%s.%d: unable to allocate memory\n",
2808 sbmac_string, idx);
2809 err = -ENOMEM;
2810 goto out_err;
2814 * This is the base address of the MAC.
2816 snprintf(res->name, sizeof(res->name), "%s %d", sbmac_pretty, idx);
2817 res->r.name = res->name;
2818 res->r.flags = IORESOURCE_MEM;
2819 res->r.start = A_MAC_CHANNEL_BASE(idx);
2820 res->r.end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2822 pldev = platform_device_register_simple(sbmac_string, idx, &res->r, 1);
2823 if (IS_ERR(pldev)) {
2824 printk(KERN_ERR "%s.%d: unable to register platform device\n",
2825 sbmac_string, idx);
2826 err = PTR_ERR(pldev);
2827 goto out_kfree;
2830 if (!pldev->dev.driver) {
2831 err = 0; /* No hardware at this address. */
2832 goto out_unregister;
2835 sbmac_pldev[idx] = pldev;
2836 return 0;
2838 out_unregister:
2839 platform_device_unregister(pldev);
2841 out_kfree:
2842 kfree(res);
2844 out_err:
2845 return err;
2848 static void __init sbmac_platform_probe(void)
2850 int i;
2852 /* Set the number of available units based on the SOC type. */
2853 switch (soc_type) {
2854 case K_SYS_SOC_TYPE_BCM1250:
2855 case K_SYS_SOC_TYPE_BCM1250_ALT:
2856 sbmac_max_units = 3;
2857 break;
2858 case K_SYS_SOC_TYPE_BCM1120:
2859 case K_SYS_SOC_TYPE_BCM1125:
2860 case K_SYS_SOC_TYPE_BCM1125H:
2861 case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
2862 sbmac_max_units = 2;
2863 break;
2864 case K_SYS_SOC_TYPE_BCM1x55:
2865 case K_SYS_SOC_TYPE_BCM1x80:
2866 sbmac_max_units = 4;
2867 break;
2868 default:
2869 return; /* none */
2873 * For bringup when not using the firmware, we can pre-fill
2874 * the MAC addresses using the environment variables
2875 * specified in this file (or maybe from the config file?)
2877 #ifdef SBMAC_ETH0_HWADDR
2878 sbmac_setup_hwaddr(0, SBMAC_ETH0_HWADDR);
2879 #endif
2880 #ifdef SBMAC_ETH1_HWADDR
2881 sbmac_setup_hwaddr(1, SBMAC_ETH1_HWADDR);
2882 #endif
2883 #ifdef SBMAC_ETH2_HWADDR
2884 sbmac_setup_hwaddr(2, SBMAC_ETH2_HWADDR);
2885 #endif
2886 #ifdef SBMAC_ETH3_HWADDR
2887 sbmac_setup_hwaddr(3, SBMAC_ETH3_HWADDR);
2888 #endif
2890 sbmac_pldev = kcalloc(sbmac_max_units, sizeof(*sbmac_pldev),
2891 GFP_KERNEL);
2892 if (!sbmac_pldev) {
2893 printk(KERN_ERR "%s: unable to allocate memory\n",
2894 sbmac_string);
2895 return;
2899 * Walk through the Ethernet controllers and find
2900 * those who have their MAC addresses set.
2902 for (i = 0; i < sbmac_max_units; i++)
2903 if (sbmac_platform_probe_one(i))
2904 break;
2908 static void __exit sbmac_platform_cleanup(void)
2910 int i;
2912 for (i = 0; i < sbmac_max_units; i++)
2913 platform_device_unregister(sbmac_pldev[i]);
2914 kfree(sbmac_pldev);
2918 static struct platform_driver sbmac_driver = {
2919 .probe = sbmac_probe,
2920 .remove = __exit_p(sbmac_remove),
2921 .driver = {
2922 .name = sbmac_string,
2926 static int __init sbmac_init_module(void)
2928 int err;
2930 err = platform_driver_register(&sbmac_driver);
2931 if (err)
2932 return err;
2934 sbmac_platform_probe();
2936 return err;
2939 static void __exit sbmac_cleanup_module(void)
2941 sbmac_platform_cleanup();
2942 platform_driver_unregister(&sbmac_driver);
2945 module_init(sbmac_init_module);
2946 module_exit(sbmac_cleanup_module);