RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / sb1250-mac.c
blob5f020e3e8620124df8c8600a73945cf80beed519
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 /* Operational parameters that usually are not changed. */
53 #define CONFIG_SBMAC_COALESCE
55 /* Time in jiffies before concluding the transmitter is hung. */
56 #define TX_TIMEOUT (2*HZ)
59 MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
60 MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
62 /* A few user-configurable values which may be modified when a driver
63 module is loaded. */
65 /* 1 normal messages, 0 quiet .. 7 verbose. */
66 static int debug = 1;
67 module_param(debug, int, S_IRUGO);
68 MODULE_PARM_DESC(debug, "Debug messages");
70 #ifdef CONFIG_SBMAC_COALESCE
71 static int int_pktcnt_tx = 255;
72 module_param(int_pktcnt_tx, int, S_IRUGO);
73 MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
75 static int int_timeout_tx = 255;
76 module_param(int_timeout_tx, int, S_IRUGO);
77 MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
79 static int int_pktcnt_rx = 64;
80 module_param(int_pktcnt_rx, int, S_IRUGO);
81 MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
83 static int int_timeout_rx = 64;
84 module_param(int_timeout_rx, int, S_IRUGO);
85 MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
86 #endif
88 #include <asm/sibyte/board.h>
89 #include <asm/sibyte/sb1250.h>
90 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
91 #include <asm/sibyte/bcm1480_regs.h>
92 #include <asm/sibyte/bcm1480_int.h>
93 #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
94 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
95 #include <asm/sibyte/sb1250_regs.h>
96 #include <asm/sibyte/sb1250_int.h>
97 #else
98 #error invalid SiByte MAC configuation
99 #endif
100 #include <asm/sibyte/sb1250_scd.h>
101 #include <asm/sibyte/sb1250_mac.h>
102 #include <asm/sibyte/sb1250_dma.h>
104 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
105 #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
106 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
107 #define UNIT_INT(n) (K_INT_MAC_0 + (n))
108 #else
109 #error invalid SiByte MAC configuation
110 #endif
112 #ifdef K_INT_PHY
113 #define SBMAC_PHY_INT K_INT_PHY
114 #else
115 #define SBMAC_PHY_INT PHY_POLL
116 #endif
118 /**********************************************************************
119 * Simple types
120 ********************************************************************* */
122 enum sbmac_speed {
123 sbmac_speed_none = 0,
124 sbmac_speed_10 = SPEED_10,
125 sbmac_speed_100 = SPEED_100,
126 sbmac_speed_1000 = SPEED_1000,
129 enum sbmac_duplex {
130 sbmac_duplex_none = -1,
131 sbmac_duplex_half = DUPLEX_HALF,
132 sbmac_duplex_full = DUPLEX_FULL,
135 enum sbmac_fc {
136 sbmac_fc_none,
137 sbmac_fc_disabled,
138 sbmac_fc_frame,
139 sbmac_fc_collision,
140 sbmac_fc_carrier,
143 enum sbmac_state {
144 sbmac_state_uninit,
145 sbmac_state_off,
146 sbmac_state_on,
147 sbmac_state_broken,
151 /**********************************************************************
152 * Macros
153 ********************************************************************* */
156 #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
157 (d)->sbdma_dscrtable : (d)->f+1)
160 #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
162 #define SBMAC_MAX_TXDESCR 256
163 #define SBMAC_MAX_RXDESCR 256
165 #define ETHER_ADDR_LEN 6
166 #define ENET_PACKET_SIZE 1518
167 /*#define ENET_PACKET_SIZE 9216 */
169 /**********************************************************************
170 * DMA Descriptor structure
171 ********************************************************************* */
173 struct sbdmadscr {
174 uint64_t dscr_a;
175 uint64_t dscr_b;
178 /**********************************************************************
179 * DMA Controller structure
180 ********************************************************************* */
182 struct sbmacdma {
185 * This stuff is used to identify the channel and the registers
186 * associated with it.
188 struct sbmac_softc *sbdma_eth; /* back pointer to associated
189 MAC */
190 int sbdma_channel; /* channel number */
191 int sbdma_txdir; /* direction (1=transmit) */
192 int sbdma_maxdescr; /* total # of descriptors
193 in ring */
194 #ifdef CONFIG_SBMAC_COALESCE
195 int sbdma_int_pktcnt;
196 /* # descriptors rx/tx
197 before interrupt */
198 int sbdma_int_timeout;
199 /* # usec rx/tx interrupt */
200 #endif
201 void __iomem *sbdma_config0; /* DMA config register 0 */
202 void __iomem *sbdma_config1; /* DMA config register 1 */
203 void __iomem *sbdma_dscrbase;
204 /* descriptor base address */
205 void __iomem *sbdma_dscrcnt; /* descriptor count register */
206 void __iomem *sbdma_curdscr; /* current descriptor
207 address */
208 void __iomem *sbdma_oodpktlost;
209 /* pkt drop (rx only) */
212 * This stuff is for maintenance of the ring
214 void *sbdma_dscrtable_unaligned;
215 struct sbdmadscr *sbdma_dscrtable;
216 /* base of descriptor table */
217 struct sbdmadscr *sbdma_dscrtable_end;
218 /* end of descriptor table */
219 struct sk_buff **sbdma_ctxtable;
220 /* context table, one
221 per descr */
222 dma_addr_t sbdma_dscrtable_phys;
223 /* and also the phys addr */
224 struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
225 struct sbdmadscr *sbdma_remptr; /* next dscr for sw
226 to remove */
230 /**********************************************************************
231 * Ethernet softc structure
232 ********************************************************************* */
234 struct sbmac_softc {
237 * Linux-specific things
239 struct net_device *sbm_dev; /* pointer to linux device */
240 struct napi_struct napi;
241 struct phy_device *phy_dev; /* the associated PHY device */
242 struct mii_bus *mii_bus; /* the MII bus */
243 int phy_irq[PHY_MAX_ADDR];
244 spinlock_t sbm_lock; /* spin lock */
245 int sbm_devflags; /* current device flags */
248 * Controller-specific things
250 void __iomem *sbm_base; /* MAC's base address */
251 enum sbmac_state sbm_state; /* current state */
253 void __iomem *sbm_macenable; /* MAC Enable Register */
254 void __iomem *sbm_maccfg; /* MAC Config Register */
255 void __iomem *sbm_fifocfg; /* FIFO Config Register */
256 void __iomem *sbm_framecfg; /* Frame Config Register */
257 void __iomem *sbm_rxfilter; /* Receive Filter Register */
258 void __iomem *sbm_isr; /* Interrupt Status Register */
259 void __iomem *sbm_imr; /* Interrupt Mask Register */
260 void __iomem *sbm_mdio; /* MDIO Register */
262 enum sbmac_speed sbm_speed; /* current speed */
263 enum sbmac_duplex sbm_duplex; /* current duplex */
264 enum sbmac_fc sbm_fc; /* cur. flow control setting */
265 int sbm_pause; /* current pause setting */
266 int sbm_link; /* current link state */
268 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
270 struct sbmacdma sbm_txdma; /* only channel 0 for now */
271 struct sbmacdma sbm_rxdma;
272 int rx_hw_checksum;
273 int sbe_idx;
277 /**********************************************************************
278 * Externs
279 ********************************************************************* */
281 /**********************************************************************
282 * Prototypes
283 ********************************************************************* */
285 static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
286 int txrx, int maxdescr);
287 static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
288 static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
289 struct sk_buff *m);
290 static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
291 static void sbdma_emptyring(struct sbmacdma *d);
292 static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
293 static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
294 int work_to_do, int poll);
295 static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
296 int poll);
297 static int sbmac_initctx(struct sbmac_softc *s);
298 static void sbmac_channel_start(struct sbmac_softc *s);
299 static void sbmac_channel_stop(struct sbmac_softc *s);
300 static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
301 enum sbmac_state);
302 static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
303 static uint64_t sbmac_addr2reg(unsigned char *ptr);
304 static irqreturn_t sbmac_intr(int irq, void *dev_instance);
305 static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
306 static void sbmac_setmulti(struct sbmac_softc *sc);
307 static int sbmac_init(struct platform_device *pldev, long long base);
308 static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
309 static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
310 enum sbmac_fc fc);
312 static int sbmac_open(struct net_device *dev);
313 static void sbmac_tx_timeout (struct net_device *dev);
314 static void sbmac_set_rx_mode(struct net_device *dev);
315 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
316 static int sbmac_close(struct net_device *dev);
317 static int sbmac_poll(struct napi_struct *napi, int budget);
319 static void sbmac_mii_poll(struct net_device *dev);
320 static int sbmac_mii_probe(struct net_device *dev);
322 static void sbmac_mii_sync(void __iomem *sbm_mdio);
323 static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
324 int bitcnt);
325 static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
326 static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
327 u16 val);
330 /**********************************************************************
331 * Globals
332 ********************************************************************* */
334 static char sbmac_string[] = "sb1250-mac";
336 static char sbmac_mdio_string[] = "sb1250-mac-mdio";
339 /**********************************************************************
340 * MDIO constants
341 ********************************************************************* */
343 #define MII_COMMAND_START 0x01
344 #define MII_COMMAND_READ 0x02
345 #define MII_COMMAND_WRITE 0x01
346 #define MII_COMMAND_ACK 0x02
348 #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
350 #define ENABLE 1
351 #define DISABLE 0
353 /**********************************************************************
354 * SBMAC_MII_SYNC(sbm_mdio)
356 * Synchronize with the MII - send a pattern of bits to the MII
357 * that will guarantee that it is ready to accept a command.
359 * Input parameters:
360 * sbm_mdio - address of the MAC's MDIO register
362 * Return value:
363 * nothing
364 ********************************************************************* */
366 static void sbmac_mii_sync(void __iomem *sbm_mdio)
368 int cnt;
369 uint64_t bits;
370 int mac_mdio_genc;
372 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
374 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
376 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
378 for (cnt = 0; cnt < 32; cnt++) {
379 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
380 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
384 /**********************************************************************
385 * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
387 * Send some bits to the MII. The bits to be sent are right-
388 * justified in the 'data' parameter.
390 * Input parameters:
391 * sbm_mdio - address of the MAC's MDIO register
392 * data - data to send
393 * bitcnt - number of bits to send
394 ********************************************************************* */
396 static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
397 int bitcnt)
399 int i;
400 uint64_t bits;
401 unsigned int curmask;
402 int mac_mdio_genc;
404 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
406 bits = M_MAC_MDIO_DIR_OUTPUT;
407 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
409 curmask = 1 << (bitcnt - 1);
411 for (i = 0; i < bitcnt; i++) {
412 if (data & curmask)
413 bits |= M_MAC_MDIO_OUT;
414 else bits &= ~M_MAC_MDIO_OUT;
415 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
416 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
417 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
418 curmask >>= 1;
424 /**********************************************************************
425 * SBMAC_MII_READ(bus, phyaddr, regidx)
426 * Read a PHY register.
428 * Input parameters:
429 * bus - MDIO bus handle
430 * phyaddr - PHY's address
431 * regnum - index of register to read
433 * Return value:
434 * value read, or 0xffff if an error occurred.
435 ********************************************************************* */
437 static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
439 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
440 void __iomem *sbm_mdio = sc->sbm_mdio;
441 int idx;
442 int error;
443 int regval;
444 int mac_mdio_genc;
447 * Synchronize ourselves so that the PHY knows the next
448 * thing coming down is a command
450 sbmac_mii_sync(sbm_mdio);
453 * Send the data to the PHY. The sequence is
454 * a "start" command (2 bits)
455 * a "read" command (2 bits)
456 * the PHY addr (5 bits)
457 * the register index (5 bits)
459 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
460 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
461 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
462 sbmac_mii_senddata(sbm_mdio, regidx, 5);
464 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
467 * Switch the port around without a clock transition.
469 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
472 * Send out a clock pulse to signal we want the status
474 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
475 sbm_mdio);
476 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
479 * If an error occurred, the PHY will signal '1' back
481 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
484 * Issue an 'idle' clock pulse, but keep the direction
485 * the same.
487 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
488 sbm_mdio);
489 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
491 regval = 0;
493 for (idx = 0; idx < 16; idx++) {
494 regval <<= 1;
496 if (error == 0) {
497 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
498 regval |= 1;
501 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
502 sbm_mdio);
503 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
506 /* Switch back to output */
507 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
509 if (error == 0)
510 return regval;
511 return 0xffff;
515 /**********************************************************************
516 * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
518 * Write a value to a PHY register.
520 * Input parameters:
521 * bus - MDIO bus handle
522 * phyaddr - PHY to use
523 * regidx - register within the PHY
524 * regval - data to write to register
526 * Return value:
527 * 0 for success
528 ********************************************************************* */
530 static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
531 u16 regval)
533 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
534 void __iomem *sbm_mdio = sc->sbm_mdio;
535 int mac_mdio_genc;
537 sbmac_mii_sync(sbm_mdio);
539 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
540 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
541 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
542 sbmac_mii_senddata(sbm_mdio, regidx, 5);
543 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
544 sbmac_mii_senddata(sbm_mdio, regval, 16);
546 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
548 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
550 return 0;
555 /**********************************************************************
556 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
558 * Initialize a DMA channel context. Since there are potentially
559 * eight DMA channels per MAC, it's nice to do this in a standard
560 * way.
562 * Input parameters:
563 * d - struct sbmacdma (DMA channel context)
564 * s - struct sbmac_softc (pointer to a MAC)
565 * chan - channel number (0..1 right now)
566 * txrx - Identifies DMA_TX or DMA_RX for channel direction
567 * maxdescr - number of descriptors
569 * Return value:
570 * nothing
571 ********************************************************************* */
573 static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
574 int txrx, int maxdescr)
576 #ifdef CONFIG_SBMAC_COALESCE
577 int int_pktcnt, int_timeout;
578 #endif
581 * Save away interesting stuff in the structure
584 d->sbdma_eth = s;
585 d->sbdma_channel = chan;
586 d->sbdma_txdir = txrx;
589 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
590 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
591 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
592 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
593 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
594 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
595 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
596 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
597 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
598 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
599 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
600 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
601 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
602 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
603 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
604 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
605 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
606 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
607 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
608 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
609 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
612 * initialize register pointers
615 d->sbdma_config0 =
616 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
617 d->sbdma_config1 =
618 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
619 d->sbdma_dscrbase =
620 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
621 d->sbdma_dscrcnt =
622 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
623 d->sbdma_curdscr =
624 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
625 if (d->sbdma_txdir)
626 d->sbdma_oodpktlost = NULL;
627 else
628 d->sbdma_oodpktlost =
629 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
632 * Allocate memory for the ring
635 d->sbdma_maxdescr = maxdescr;
637 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
638 sizeof(*d->sbdma_dscrtable),
639 GFP_KERNEL);
642 * The descriptor table must be aligned to at least 16 bytes or the
643 * MAC will corrupt it.
645 d->sbdma_dscrtable = (struct sbdmadscr *)
646 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
647 sizeof(*d->sbdma_dscrtable));
649 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
651 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
654 * And context table
657 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
658 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
660 #ifdef CONFIG_SBMAC_COALESCE
662 * Setup Rx/Tx DMA coalescing defaults
665 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
666 if ( int_pktcnt ) {
667 d->sbdma_int_pktcnt = int_pktcnt;
668 } else {
669 d->sbdma_int_pktcnt = 1;
672 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
673 if ( int_timeout ) {
674 d->sbdma_int_timeout = int_timeout;
675 } else {
676 d->sbdma_int_timeout = 0;
678 #endif
682 /**********************************************************************
683 * SBDMA_CHANNEL_START(d)
685 * Initialize the hardware registers for a DMA channel.
687 * Input parameters:
688 * d - DMA channel to init (context must be previously init'd
689 * rxtx - DMA_RX or DMA_TX depending on what type of channel
691 * Return value:
692 * nothing
693 ********************************************************************* */
695 static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
698 * Turn on the DMA channel
701 #ifdef CONFIG_SBMAC_COALESCE
702 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
703 0, d->sbdma_config1);
704 __raw_writeq(M_DMA_EOP_INT_EN |
705 V_DMA_RINGSZ(d->sbdma_maxdescr) |
706 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
707 0, d->sbdma_config0);
708 #else
709 __raw_writeq(0, d->sbdma_config1);
710 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
711 0, d->sbdma_config0);
712 #endif
714 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
717 * Initialize ring pointers
720 d->sbdma_addptr = d->sbdma_dscrtable;
721 d->sbdma_remptr = d->sbdma_dscrtable;
724 /**********************************************************************
725 * SBDMA_CHANNEL_STOP(d)
727 * Initialize the hardware registers for a DMA channel.
729 * Input parameters:
730 * d - DMA channel to init (context must be previously init'd
732 * Return value:
733 * nothing
734 ********************************************************************* */
736 static void sbdma_channel_stop(struct sbmacdma *d)
739 * Turn off the DMA channel
742 __raw_writeq(0, d->sbdma_config1);
744 __raw_writeq(0, d->sbdma_dscrbase);
746 __raw_writeq(0, d->sbdma_config0);
749 * Zero ring pointers
752 d->sbdma_addptr = NULL;
753 d->sbdma_remptr = NULL;
756 static inline void sbdma_align_skb(struct sk_buff *skb,
757 unsigned int power2, unsigned int offset)
759 unsigned char *addr = skb->data;
760 unsigned char *newaddr = PTR_ALIGN(addr, power2);
762 skb_reserve(skb, newaddr - addr + offset);
766 /**********************************************************************
767 * SBDMA_ADD_RCVBUFFER(d,sb)
769 * Add a buffer to the specified DMA channel. For receive channels,
770 * this queues a buffer for inbound packets.
772 * Input parameters:
773 * sc - softc structure
774 * d - DMA channel descriptor
775 * sb - sk_buff to add, or NULL if we should allocate one
777 * Return value:
778 * 0 if buffer could not be added (ring is full)
779 * 1 if buffer added successfully
780 ********************************************************************* */
783 static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
784 struct sk_buff *sb)
786 struct net_device *dev = sc->sbm_dev;
787 struct sbdmadscr *dsc;
788 struct sbdmadscr *nextdsc;
789 struct sk_buff *sb_new = NULL;
790 int pktsize = ENET_PACKET_SIZE;
792 /* get pointer to our current place in the ring */
794 dsc = d->sbdma_addptr;
795 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
798 * figure out if the ring is full - if the next descriptor
799 * is the same as the one that we're going to remove from
800 * the ring, the ring is full
803 if (nextdsc == d->sbdma_remptr) {
804 return -ENOSPC;
808 * Allocate a sk_buff if we don't already have one.
809 * If we do have an sk_buff, reset it so that it's empty.
811 * Note: sk_buffs don't seem to be guaranteed to have any sort
812 * of alignment when they are allocated. Therefore, allocate enough
813 * extra space to make sure that:
815 * 1. the data does not start in the middle of a cache line.
816 * 2. The data does not end in the middle of a cache line
817 * 3. The buffer can be aligned such that the IP addresses are
818 * naturally aligned.
820 * Remember, the SOCs MAC writes whole cache lines at a time,
821 * without reading the old contents first. So, if the sk_buff's
822 * data portion starts in the middle of a cache line, the SOC
823 * DMA will trash the beginning (and ending) portions.
826 if (sb == NULL) {
827 sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
828 SMP_CACHE_BYTES * 2 +
829 NET_IP_ALIGN);
830 if (sb_new == NULL) {
831 pr_info("%s: sk_buff allocation failed\n",
832 d->sbdma_eth->sbm_dev->name);
833 return -ENOBUFS;
836 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
838 else {
839 sb_new = sb;
841 * nothing special to reinit buffer, it's already aligned
842 * and sb->data already points to a good place.
847 * fill in the descriptor
850 #ifdef CONFIG_SBMAC_COALESCE
852 * Do not interrupt per DMA transfer.
854 dsc->dscr_a = virt_to_phys(sb_new->data) |
855 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
856 #else
857 dsc->dscr_a = virt_to_phys(sb_new->data) |
858 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
859 M_DMA_DSCRA_INTERRUPT;
860 #endif
862 /* receiving: no options */
863 dsc->dscr_b = 0;
866 * fill in the context
869 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
872 * point at next packet
875 d->sbdma_addptr = nextdsc;
878 * Give the buffer to the DMA engine.
881 __raw_writeq(1, d->sbdma_dscrcnt);
883 return 0; /* we did it */
886 /**********************************************************************
887 * SBDMA_ADD_TXBUFFER(d,sb)
889 * Add a transmit buffer to the specified DMA channel, causing a
890 * transmit to start.
892 * Input parameters:
893 * d - DMA channel descriptor
894 * sb - sk_buff to add
896 * Return value:
897 * 0 transmit queued successfully
898 * otherwise error code
899 ********************************************************************* */
902 static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
904 struct sbdmadscr *dsc;
905 struct sbdmadscr *nextdsc;
906 uint64_t phys;
907 uint64_t ncb;
908 int length;
910 /* get pointer to our current place in the ring */
912 dsc = d->sbdma_addptr;
913 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
916 * figure out if the ring is full - if the next descriptor
917 * is the same as the one that we're going to remove from
918 * the ring, the ring is full
921 if (nextdsc == d->sbdma_remptr) {
922 return -ENOSPC;
926 * Under Linux, it's not necessary to copy/coalesce buffers
927 * like it is on NetBSD. We think they're all contiguous,
928 * but that may not be true for GBE.
931 length = sb->len;
934 * fill in the descriptor. Note that the number of cache
935 * blocks in the descriptor is the number of blocks
936 * *spanned*, so we need to add in the offset (if any)
937 * while doing the calculation.
940 phys = virt_to_phys(sb->data);
941 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
943 dsc->dscr_a = phys |
944 V_DMA_DSCRA_A_SIZE(ncb) |
945 #ifndef CONFIG_SBMAC_COALESCE
946 M_DMA_DSCRA_INTERRUPT |
947 #endif
948 M_DMA_ETHTX_SOP;
950 /* transmitting: set outbound options and length */
952 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
953 V_DMA_DSCRB_PKT_SIZE(length);
956 * fill in the context
959 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
962 * point at next packet
965 d->sbdma_addptr = nextdsc;
968 * Give the buffer to the DMA engine.
971 __raw_writeq(1, d->sbdma_dscrcnt);
973 return 0; /* we did it */
979 /**********************************************************************
980 * SBDMA_EMPTYRING(d)
982 * Free all allocated sk_buffs on the specified DMA channel;
984 * Input parameters:
985 * d - DMA channel
987 * Return value:
988 * nothing
989 ********************************************************************* */
991 static void sbdma_emptyring(struct sbmacdma *d)
993 int idx;
994 struct sk_buff *sb;
996 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
997 sb = d->sbdma_ctxtable[idx];
998 if (sb) {
999 dev_kfree_skb(sb);
1000 d->sbdma_ctxtable[idx] = NULL;
1006 /**********************************************************************
1007 * SBDMA_FILLRING(d)
1009 * Fill the specified DMA channel (must be receive channel)
1010 * with sk_buffs
1012 * Input parameters:
1013 * sc - softc structure
1014 * d - DMA channel
1016 * Return value:
1017 * nothing
1018 ********************************************************************* */
1020 static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
1022 int idx;
1024 for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
1025 if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
1026 break;
1030 #ifdef CONFIG_NET_POLL_CONTROLLER
1031 static void sbmac_netpoll(struct net_device *netdev)
1033 struct sbmac_softc *sc = netdev_priv(netdev);
1034 int irq = sc->sbm_dev->irq;
1036 __raw_writeq(0, sc->sbm_imr);
1038 sbmac_intr(irq, netdev);
1040 #ifdef CONFIG_SBMAC_COALESCE
1041 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1042 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1043 sc->sbm_imr);
1044 #else
1045 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1046 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1047 #endif
1049 #endif
1051 /**********************************************************************
1052 * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
1054 * Process "completed" receive buffers on the specified DMA channel.
1056 * Input parameters:
1057 * sc - softc structure
1058 * d - DMA channel context
1059 * work_to_do - no. of packets to process before enabling interrupt
1060 * again (for NAPI)
1061 * poll - 1: using polling (for NAPI)
1063 * Return value:
1064 * nothing
1065 ********************************************************************* */
1067 static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1068 int work_to_do, int poll)
1070 struct net_device *dev = sc->sbm_dev;
1071 int curidx;
1072 int hwidx;
1073 struct sbdmadscr *dsc;
1074 struct sk_buff *sb;
1075 int len;
1076 int work_done = 0;
1077 int dropped = 0;
1079 prefetch(d);
1081 again:
1082 /* Check if the HW dropped any frames */
1083 dev->stats.rx_fifo_errors
1084 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1085 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1087 while (work_to_do-- > 0) {
1089 * figure out where we are (as an index) and where
1090 * the hardware is (also as an index)
1092 * This could be done faster if (for example) the
1093 * descriptor table was page-aligned and contiguous in
1094 * both virtual and physical memory -- you could then
1095 * just compare the low-order bits of the virtual address
1096 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1099 dsc = d->sbdma_remptr;
1100 curidx = dsc - d->sbdma_dscrtable;
1102 prefetch(dsc);
1103 prefetch(&d->sbdma_ctxtable[curidx]);
1105 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1106 d->sbdma_dscrtable_phys) /
1107 sizeof(*d->sbdma_dscrtable);
1110 * If they're the same, that means we've processed all
1111 * of the descriptors up to (but not including) the one that
1112 * the hardware is working on right now.
1115 if (curidx == hwidx)
1116 goto done;
1119 * Otherwise, get the packet's sk_buff ptr back
1122 sb = d->sbdma_ctxtable[curidx];
1123 d->sbdma_ctxtable[curidx] = NULL;
1125 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
1128 * Check packet status. If good, process it.
1129 * If not, silently drop it and put it back on the
1130 * receive ring.
1133 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
1136 * Add a new buffer to replace the old one. If we fail
1137 * to allocate a buffer, we're going to drop this
1138 * packet and put it right back on the receive ring.
1141 if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
1142 -ENOBUFS)) {
1143 dev->stats.rx_dropped++;
1144 /* Re-add old buffer */
1145 sbdma_add_rcvbuffer(sc, d, sb);
1146 /* No point in continuing at the moment */
1147 printk(KERN_ERR "dropped packet (1)\n");
1148 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1149 goto done;
1150 } else {
1152 * Set length into the packet
1154 skb_put(sb,len);
1157 * Buffer has been replaced on the
1158 * receive ring. Pass the buffer to
1159 * the kernel
1161 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1162 /* Check hw IPv4/TCP checksum if supported */
1163 if (sc->rx_hw_checksum == ENABLE) {
1164 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1165 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1166 sb->ip_summed = CHECKSUM_UNNECESSARY;
1167 /* don't need to set sb->csum */
1168 } else {
1169 sb->ip_summed = CHECKSUM_NONE;
1172 prefetch(sb->data);
1173 prefetch((const void *)(((char *)sb->data)+32));
1174 if (poll)
1175 dropped = netif_receive_skb(sb);
1176 else
1177 dropped = netif_rx(sb);
1179 if (dropped == NET_RX_DROP) {
1180 dev->stats.rx_dropped++;
1181 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1182 goto done;
1184 else {
1185 dev->stats.rx_bytes += len;
1186 dev->stats.rx_packets++;
1189 } else {
1191 * Packet was mangled somehow. Just drop it and
1192 * put it back on the receive ring.
1194 dev->stats.rx_errors++;
1195 sbdma_add_rcvbuffer(sc, d, sb);
1200 * .. and advance to the next buffer.
1203 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1204 work_done++;
1206 if (!poll) {
1207 work_to_do = 32;
1208 goto again; /* collect fifo drop statistics again */
1210 done:
1211 return work_done;
1214 /**********************************************************************
1215 * SBDMA_TX_PROCESS(sc,d)
1217 * Process "completed" transmit buffers on the specified DMA channel.
1218 * This is normally called within the interrupt service routine.
1219 * Note that this isn't really ideal for priority channels, since
1220 * it processes all of the packets on a given channel before
1221 * returning.
1223 * Input parameters:
1224 * sc - softc structure
1225 * d - DMA channel context
1226 * poll - 1: using polling (for NAPI)
1228 * Return value:
1229 * nothing
1230 ********************************************************************* */
1232 static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1233 int poll)
1235 struct net_device *dev = sc->sbm_dev;
1236 int curidx;
1237 int hwidx;
1238 struct sbdmadscr *dsc;
1239 struct sk_buff *sb;
1240 unsigned long flags;
1241 int packets_handled = 0;
1243 spin_lock_irqsave(&(sc->sbm_lock), flags);
1245 if (d->sbdma_remptr == d->sbdma_addptr)
1246 goto end_unlock;
1248 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1249 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
1251 for (;;) {
1253 * figure out where we are (as an index) and where
1254 * the hardware is (also as an index)
1256 * This could be done faster if (for example) the
1257 * descriptor table was page-aligned and contiguous in
1258 * both virtual and physical memory -- you could then
1259 * just compare the low-order bits of the virtual address
1260 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1263 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
1266 * If they're the same, that means we've processed all
1267 * of the descriptors up to (but not including) the one that
1268 * the hardware is working on right now.
1271 if (curidx == hwidx)
1272 break;
1275 * Otherwise, get the packet's sk_buff ptr back
1278 dsc = &(d->sbdma_dscrtable[curidx]);
1279 sb = d->sbdma_ctxtable[curidx];
1280 d->sbdma_ctxtable[curidx] = NULL;
1283 * Stats
1286 dev->stats.tx_bytes += sb->len;
1287 dev->stats.tx_packets++;
1290 * for transmits, we just free buffers.
1293 dev_kfree_skb_irq(sb);
1296 * .. and advance to the next buffer.
1299 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1301 packets_handled++;
1306 * Decide if we should wake up the protocol or not.
1307 * Other drivers seem to do this when we reach a low
1308 * watermark on the transmit queue.
1311 if (packets_handled)
1312 netif_wake_queue(d->sbdma_eth->sbm_dev);
1314 end_unlock:
1315 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
1321 /**********************************************************************
1322 * SBMAC_INITCTX(s)
1324 * Initialize an Ethernet context structure - this is called
1325 * once per MAC on the 1250. Memory is allocated here, so don't
1326 * call it again from inside the ioctl routines that bring the
1327 * interface up/down
1329 * Input parameters:
1330 * s - sbmac context structure
1332 * Return value:
1334 ********************************************************************* */
1336 static int sbmac_initctx(struct sbmac_softc *s)
1340 * figure out the addresses of some ports
1343 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1344 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1345 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1346 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1347 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1348 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1349 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1350 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1353 * Initialize the DMA channels. Right now, only one per MAC is used
1354 * Note: Only do this _once_, as it allocates memory from the kernel!
1357 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1358 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
1361 * initial state is OFF
1364 s->sbm_state = sbmac_state_off;
1366 return 0;
1370 static void sbdma_uninitctx(struct sbmacdma *d)
1372 if (d->sbdma_dscrtable_unaligned) {
1373 kfree(d->sbdma_dscrtable_unaligned);
1374 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
1377 if (d->sbdma_ctxtable) {
1378 kfree(d->sbdma_ctxtable);
1379 d->sbdma_ctxtable = NULL;
1384 static void sbmac_uninitctx(struct sbmac_softc *sc)
1386 sbdma_uninitctx(&(sc->sbm_txdma));
1387 sbdma_uninitctx(&(sc->sbm_rxdma));
1391 /**********************************************************************
1392 * SBMAC_CHANNEL_START(s)
1394 * Start packet processing on this MAC.
1396 * Input parameters:
1397 * s - sbmac structure
1399 * Return value:
1400 * nothing
1401 ********************************************************************* */
1403 static void sbmac_channel_start(struct sbmac_softc *s)
1405 uint64_t reg;
1406 void __iomem *port;
1407 uint64_t cfg,fifo,framecfg;
1408 int idx, th_value;
1411 * Don't do this if running
1414 if (s->sbm_state == sbmac_state_on)
1415 return;
1418 * Bring the controller out of reset, but leave it off.
1421 __raw_writeq(0, s->sbm_macenable);
1424 * Ignore all received packets
1427 __raw_writeq(0, s->sbm_rxfilter);
1430 * Calculate values for various control registers.
1433 cfg = M_MAC_RETRY_EN |
1434 M_MAC_TX_HOLD_SOP_EN |
1435 V_MAC_TX_PAUSE_CNT_16K |
1436 M_MAC_AP_STAT_EN |
1437 M_MAC_FAST_SYNC |
1438 M_MAC_SS_EN |
1442 * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1443 * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1444 * Use a larger RD_THRSH for gigabit
1446 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
1447 th_value = 28;
1448 else
1449 th_value = 64;
1451 fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1452 ((s->sbm_speed == sbmac_speed_1000)
1453 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1454 V_MAC_TX_RL_THRSH(4) |
1455 V_MAC_RX_PL_THRSH(4) |
1456 V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
1457 V_MAC_RX_RL_THRSH(8) |
1460 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1461 V_MAC_MAX_FRAMESZ_DEFAULT |
1462 V_MAC_BACKOFF_SEL(1);
1465 * Clear out the hash address map
1468 port = s->sbm_base + R_MAC_HASH_BASE;
1469 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
1470 __raw_writeq(0, port);
1471 port += sizeof(uint64_t);
1475 * Clear out the exact-match table
1478 port = s->sbm_base + R_MAC_ADDR_BASE;
1479 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
1480 __raw_writeq(0, port);
1481 port += sizeof(uint64_t);
1485 * Clear out the DMA Channel mapping table registers
1488 port = s->sbm_base + R_MAC_CHUP0_BASE;
1489 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1490 __raw_writeq(0, port);
1491 port += sizeof(uint64_t);
1495 port = s->sbm_base + R_MAC_CHLO0_BASE;
1496 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1497 __raw_writeq(0, port);
1498 port += sizeof(uint64_t);
1502 * Program the hardware address. It goes into the hardware-address
1503 * register as well as the first filter register.
1506 reg = sbmac_addr2reg(s->sbm_hwaddr);
1508 port = s->sbm_base + R_MAC_ADDR_BASE;
1509 __raw_writeq(reg, port);
1510 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1512 #ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
1514 * Pass1 SOCs do not receive packets addressed to the
1515 * destination address in the R_MAC_ETHERNET_ADDR register.
1516 * Set the value to zero.
1518 __raw_writeq(0, port);
1519 #else
1520 __raw_writeq(reg, port);
1521 #endif
1524 * Set the receive filter for no packets, and write values
1525 * to the various config registers
1528 __raw_writeq(0, s->sbm_rxfilter);
1529 __raw_writeq(0, s->sbm_imr);
1530 __raw_writeq(framecfg, s->sbm_framecfg);
1531 __raw_writeq(fifo, s->sbm_fifocfg);
1532 __raw_writeq(cfg, s->sbm_maccfg);
1535 * Initialize DMA channels (rings should be ok now)
1538 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1539 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
1542 * Configure the speed, duplex, and flow control
1545 sbmac_set_speed(s,s->sbm_speed);
1546 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
1549 * Fill the receive ring
1552 sbdma_fillring(s, &(s->sbm_rxdma));
1555 * Turn on the rest of the bits in the enable register
1558 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1559 __raw_writeq(M_MAC_RXDMA_EN0 |
1560 M_MAC_TXDMA_EN0, s->sbm_macenable);
1561 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1562 __raw_writeq(M_MAC_RXDMA_EN0 |
1563 M_MAC_TXDMA_EN0 |
1564 M_MAC_RX_ENABLE |
1565 M_MAC_TX_ENABLE, s->sbm_macenable);
1566 #else
1567 #error invalid SiByte MAC configuation
1568 #endif
1570 #ifdef CONFIG_SBMAC_COALESCE
1571 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1572 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
1573 #else
1574 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1575 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
1576 #endif
1579 * Enable receiving unicasts and broadcasts
1582 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1585 * we're running now.
1588 s->sbm_state = sbmac_state_on;
1591 * Program multicast addresses
1594 sbmac_setmulti(s);
1597 * If channel was in promiscuous mode before, turn that on
1600 if (s->sbm_devflags & IFF_PROMISC) {
1601 sbmac_promiscuous_mode(s,1);
1607 /**********************************************************************
1608 * SBMAC_CHANNEL_STOP(s)
1610 * Stop packet processing on this MAC.
1612 * Input parameters:
1613 * s - sbmac structure
1615 * Return value:
1616 * nothing
1617 ********************************************************************* */
1619 static void sbmac_channel_stop(struct sbmac_softc *s)
1621 /* don't do this if already stopped */
1623 if (s->sbm_state == sbmac_state_off)
1624 return;
1626 /* don't accept any packets, disable all interrupts */
1628 __raw_writeq(0, s->sbm_rxfilter);
1629 __raw_writeq(0, s->sbm_imr);
1631 /* Turn off ticker */
1634 /* turn off receiver and transmitter */
1636 __raw_writeq(0, s->sbm_macenable);
1638 /* We're stopped now. */
1640 s->sbm_state = sbmac_state_off;
1643 * Stop DMA channels (rings should be ok now)
1646 sbdma_channel_stop(&(s->sbm_rxdma));
1647 sbdma_channel_stop(&(s->sbm_txdma));
1649 /* Empty the receive and transmit rings */
1651 sbdma_emptyring(&(s->sbm_rxdma));
1652 sbdma_emptyring(&(s->sbm_txdma));
1656 /**********************************************************************
1657 * SBMAC_SET_CHANNEL_STATE(state)
1659 * Set the channel's state ON or OFF
1661 * Input parameters:
1662 * state - new state
1664 * Return value:
1665 * old state
1666 ********************************************************************* */
1667 static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1668 enum sbmac_state state)
1670 enum sbmac_state oldstate = sc->sbm_state;
1673 * If same as previous state, return
1676 if (state == oldstate) {
1677 return oldstate;
1681 * If new state is ON, turn channel on
1684 if (state == sbmac_state_on) {
1685 sbmac_channel_start(sc);
1687 else {
1688 sbmac_channel_stop(sc);
1692 * Return previous state
1695 return oldstate;
1699 /**********************************************************************
1700 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
1702 * Turn on or off promiscuous mode
1704 * Input parameters:
1705 * sc - softc
1706 * onoff - 1 to turn on, 0 to turn off
1708 * Return value:
1709 * nothing
1710 ********************************************************************* */
1712 static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1714 uint64_t reg;
1716 if (sc->sbm_state != sbmac_state_on)
1717 return;
1719 if (onoff) {
1720 reg = __raw_readq(sc->sbm_rxfilter);
1721 reg |= M_MAC_ALLPKT_EN;
1722 __raw_writeq(reg, sc->sbm_rxfilter);
1724 else {
1725 reg = __raw_readq(sc->sbm_rxfilter);
1726 reg &= ~M_MAC_ALLPKT_EN;
1727 __raw_writeq(reg, sc->sbm_rxfilter);
1731 /**********************************************************************
1732 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
1734 * Set the iphdr offset as 15 assuming ethernet encapsulation
1736 * Input parameters:
1737 * sc - softc
1739 * Return value:
1740 * nothing
1741 ********************************************************************* */
1743 static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1745 uint64_t reg;
1747 /* Hard code the off set to 15 for now */
1748 reg = __raw_readq(sc->sbm_rxfilter);
1749 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
1750 __raw_writeq(reg, sc->sbm_rxfilter);
1752 /* BCM1250 pass1 didn't have hardware checksum. Everything
1753 later does. */
1754 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
1755 sc->rx_hw_checksum = DISABLE;
1756 } else {
1757 sc->rx_hw_checksum = ENABLE;
1762 /**********************************************************************
1763 * SBMAC_ADDR2REG(ptr)
1765 * Convert six bytes into the 64-bit register value that
1766 * we typically write into the SBMAC's address/mcast registers
1768 * Input parameters:
1769 * ptr - pointer to 6 bytes
1771 * Return value:
1772 * register value
1773 ********************************************************************* */
1775 static uint64_t sbmac_addr2reg(unsigned char *ptr)
1777 uint64_t reg = 0;
1779 ptr += 6;
1781 reg |= (uint64_t) *(--ptr);
1782 reg <<= 8;
1783 reg |= (uint64_t) *(--ptr);
1784 reg <<= 8;
1785 reg |= (uint64_t) *(--ptr);
1786 reg <<= 8;
1787 reg |= (uint64_t) *(--ptr);
1788 reg <<= 8;
1789 reg |= (uint64_t) *(--ptr);
1790 reg <<= 8;
1791 reg |= (uint64_t) *(--ptr);
1793 return reg;
1797 /**********************************************************************
1798 * SBMAC_SET_SPEED(s,speed)
1800 * Configure LAN speed for the specified MAC.
1801 * Warning: must be called when MAC is off!
1803 * Input parameters:
1804 * s - sbmac structure
1805 * speed - speed to set MAC to (see enum sbmac_speed)
1807 * Return value:
1808 * 1 if successful
1809 * 0 indicates invalid parameters
1810 ********************************************************************* */
1812 static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
1814 uint64_t cfg;
1815 uint64_t framecfg;
1818 * Save new current values
1821 s->sbm_speed = speed;
1823 if (s->sbm_state == sbmac_state_on)
1824 return 0; /* save for next restart */
1827 * Read current register values
1830 cfg = __raw_readq(s->sbm_maccfg);
1831 framecfg = __raw_readq(s->sbm_framecfg);
1834 * Mask out the stuff we want to change
1837 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1838 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1839 M_MAC_SLOT_SIZE);
1842 * Now add in the new bits
1845 switch (speed) {
1846 case sbmac_speed_10:
1847 framecfg |= V_MAC_IFG_RX_10 |
1848 V_MAC_IFG_TX_10 |
1849 K_MAC_IFG_THRSH_10 |
1850 V_MAC_SLOT_SIZE_10;
1851 cfg |= V_MAC_SPEED_SEL_10MBPS;
1852 break;
1854 case sbmac_speed_100:
1855 framecfg |= V_MAC_IFG_RX_100 |
1856 V_MAC_IFG_TX_100 |
1857 V_MAC_IFG_THRSH_100 |
1858 V_MAC_SLOT_SIZE_100;
1859 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1860 break;
1862 case sbmac_speed_1000:
1863 framecfg |= V_MAC_IFG_RX_1000 |
1864 V_MAC_IFG_TX_1000 |
1865 V_MAC_IFG_THRSH_1000 |
1866 V_MAC_SLOT_SIZE_1000;
1867 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1868 break;
1870 default:
1871 return 0;
1875 * Send the bits back to the hardware
1878 __raw_writeq(framecfg, s->sbm_framecfg);
1879 __raw_writeq(cfg, s->sbm_maccfg);
1881 return 1;
1884 /**********************************************************************
1885 * SBMAC_SET_DUPLEX(s,duplex,fc)
1887 * Set Ethernet duplex and flow control options for this MAC
1888 * Warning: must be called when MAC is off!
1890 * Input parameters:
1891 * s - sbmac structure
1892 * duplex - duplex setting (see enum sbmac_duplex)
1893 * fc - flow control setting (see enum sbmac_fc)
1895 * Return value:
1896 * 1 if ok
1897 * 0 if an invalid parameter combination was specified
1898 ********************************************************************* */
1900 static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1901 enum sbmac_fc fc)
1903 uint64_t cfg;
1906 * Save new current values
1909 s->sbm_duplex = duplex;
1910 s->sbm_fc = fc;
1912 if (s->sbm_state == sbmac_state_on)
1913 return 0; /* save for next restart */
1916 * Read current register values
1919 cfg = __raw_readq(s->sbm_maccfg);
1922 * Mask off the stuff we're about to change
1925 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
1928 switch (duplex) {
1929 case sbmac_duplex_half:
1930 switch (fc) {
1931 case sbmac_fc_disabled:
1932 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1933 break;
1935 case sbmac_fc_collision:
1936 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1937 break;
1939 case sbmac_fc_carrier:
1940 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1941 break;
1943 case sbmac_fc_frame: /* not valid in half duplex */
1944 default: /* invalid selection */
1945 return 0;
1947 break;
1949 case sbmac_duplex_full:
1950 switch (fc) {
1951 case sbmac_fc_disabled:
1952 cfg |= V_MAC_FC_CMD_DISABLED;
1953 break;
1955 case sbmac_fc_frame:
1956 cfg |= V_MAC_FC_CMD_ENABLED;
1957 break;
1959 case sbmac_fc_collision: /* not valid in full duplex */
1960 case sbmac_fc_carrier: /* not valid in full duplex */
1961 default:
1962 return 0;
1964 break;
1965 default:
1966 return 0;
1970 * Send the bits back to the hardware
1973 __raw_writeq(cfg, s->sbm_maccfg);
1975 return 1;
1981 /**********************************************************************
1982 * SBMAC_INTR()
1984 * Interrupt handler for MAC interrupts
1986 * Input parameters:
1987 * MAC structure
1989 * Return value:
1990 * nothing
1991 ********************************************************************* */
1992 static irqreturn_t sbmac_intr(int irq,void *dev_instance)
1994 struct net_device *dev = (struct net_device *) dev_instance;
1995 struct sbmac_softc *sc = netdev_priv(dev);
1996 uint64_t isr;
1997 int handled = 0;
2000 * Read the ISR (this clears the bits in the real
2001 * register, except for counter addr)
2004 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
2006 if (isr == 0)
2007 return IRQ_RETVAL(0);
2008 handled = 1;
2011 * Transmits on channel 0
2014 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
2015 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
2017 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
2018 if (napi_schedule_prep(&sc->napi)) {
2019 __raw_writeq(0, sc->sbm_imr);
2020 __napi_schedule(&sc->napi);
2021 /* Depend on the exit from poll to reenable intr */
2023 else {
2024 /* may leave some packets behind */
2025 sbdma_rx_process(sc,&(sc->sbm_rxdma),
2026 SBMAC_MAX_RXDESCR * 2, 0);
2029 return IRQ_RETVAL(handled);
2032 /**********************************************************************
2033 * SBMAC_START_TX(skb,dev)
2035 * Start output on the specified interface. Basically, we
2036 * queue as many buffers as we can until the ring fills up, or
2037 * we run off the end of the queue, whichever comes first.
2039 * Input parameters:
2042 * Return value:
2043 * nothing
2044 ********************************************************************* */
2045 static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2047 struct sbmac_softc *sc = netdev_priv(dev);
2048 unsigned long flags;
2050 /* lock eth irq */
2051 spin_lock_irqsave(&sc->sbm_lock, flags);
2054 * Put the buffer on the transmit ring. If we
2055 * don't have room, stop the queue.
2058 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2059 netif_stop_queue(dev);
2060 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2062 return NETDEV_TX_BUSY;
2065 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2067 return NETDEV_TX_OK;
2070 /**********************************************************************
2071 * SBMAC_SETMULTI(sc)
2073 * Reprogram the multicast table into the hardware, given
2074 * the list of multicasts associated with the interface
2075 * structure.
2077 * Input parameters:
2078 * sc - softc
2080 * Return value:
2081 * nothing
2082 ********************************************************************* */
2084 static void sbmac_setmulti(struct sbmac_softc *sc)
2086 uint64_t reg;
2087 void __iomem *port;
2088 int idx;
2089 struct netdev_hw_addr *ha;
2090 struct net_device *dev = sc->sbm_dev;
2093 * Clear out entire multicast table. We do this by nuking
2094 * the entire hash table and all the direct matches except
2095 * the first one, which is used for our station address
2098 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2099 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
2100 __raw_writeq(0, port);
2103 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2104 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
2105 __raw_writeq(0, port);
2109 * Clear the filter to say we don't want any multicasts.
2112 reg = __raw_readq(sc->sbm_rxfilter);
2113 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2114 __raw_writeq(reg, sc->sbm_rxfilter);
2116 if (dev->flags & IFF_ALLMULTI) {
2118 * Enable ALL multicasts. Do this by inverting the
2119 * multicast enable bit.
2121 reg = __raw_readq(sc->sbm_rxfilter);
2122 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2123 __raw_writeq(reg, sc->sbm_rxfilter);
2124 return;
2129 * Progam new multicast entries. For now, only use the
2130 * perfect filter. In the future we'll need to use the
2131 * hash filter if the perfect filter overflows
2135 idx = 1; /* skip station address */
2136 netdev_for_each_mc_addr(ha, dev) {
2137 if (idx == MAC_ADDR_COUNT)
2138 break;
2139 reg = sbmac_addr2reg(ha->addr);
2140 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
2141 __raw_writeq(reg, port);
2142 idx++;
2146 * Enable the "accept multicast bits" if we programmed at least one
2147 * multicast.
2150 if (idx > 1) {
2151 reg = __raw_readq(sc->sbm_rxfilter);
2152 reg |= M_MAC_MCAST_EN;
2153 __raw_writeq(reg, sc->sbm_rxfilter);
2157 static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2159 if (new_mtu > ENET_PACKET_SIZE)
2160 return -EINVAL;
2161 _dev->mtu = new_mtu;
2162 pr_info("changing the mtu to %d\n", new_mtu);
2163 return 0;
2166 static const struct net_device_ops sbmac_netdev_ops = {
2167 .ndo_open = sbmac_open,
2168 .ndo_stop = sbmac_close,
2169 .ndo_start_xmit = sbmac_start_tx,
2170 .ndo_set_multicast_list = sbmac_set_rx_mode,
2171 .ndo_tx_timeout = sbmac_tx_timeout,
2172 .ndo_do_ioctl = sbmac_mii_ioctl,
2173 .ndo_change_mtu = sb1250_change_mtu,
2174 .ndo_validate_addr = eth_validate_addr,
2175 .ndo_set_mac_address = eth_mac_addr,
2176 #ifdef CONFIG_NET_POLL_CONTROLLER
2177 .ndo_poll_controller = sbmac_netpoll,
2178 #endif
2181 /**********************************************************************
2182 * SBMAC_INIT(dev)
2184 * Attach routine - init hardware and hook ourselves into linux
2186 * Input parameters:
2187 * dev - net_device structure
2189 * Return value:
2190 * status
2191 ********************************************************************* */
2193 static int sbmac_init(struct platform_device *pldev, long long base)
2195 struct net_device *dev = dev_get_drvdata(&pldev->dev);
2196 int idx = pldev->id;
2197 struct sbmac_softc *sc = netdev_priv(dev);
2198 unsigned char *eaddr;
2199 uint64_t ea_reg;
2200 int i;
2201 int err;
2203 sc->sbm_dev = dev;
2204 sc->sbe_idx = idx;
2206 eaddr = sc->sbm_hwaddr;
2209 * Read the ethernet address. The firmware left this programmed
2210 * for us in the ethernet address register for each mac.
2213 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2214 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
2215 for (i = 0; i < 6; i++) {
2216 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2217 ea_reg >>= 8;
2220 for (i = 0; i < 6; i++) {
2221 dev->dev_addr[i] = eaddr[i];
2225 * Initialize context (get pointers to registers and stuff), then
2226 * allocate the memory for the descriptor tables.
2229 sbmac_initctx(sc);
2232 * Set up Linux device callins
2235 spin_lock_init(&(sc->sbm_lock));
2237 dev->netdev_ops = &sbmac_netdev_ops;
2238 dev->watchdog_timeo = TX_TIMEOUT;
2240 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
2242 dev->irq = UNIT_INT(idx);
2244 /* This is needed for PASS2 for Rx H/W checksum feature */
2245 sbmac_set_iphdr_offset(sc);
2247 sc->mii_bus = mdiobus_alloc();
2248 if (sc->mii_bus == NULL) {
2249 err = -ENOMEM;
2250 goto uninit_ctx;
2253 sc->mii_bus->name = sbmac_mdio_string;
2254 snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%x", idx);
2255 sc->mii_bus->priv = sc;
2256 sc->mii_bus->read = sbmac_mii_read;
2257 sc->mii_bus->write = sbmac_mii_write;
2258 sc->mii_bus->irq = sc->phy_irq;
2259 for (i = 0; i < PHY_MAX_ADDR; ++i)
2260 sc->mii_bus->irq[i] = SBMAC_PHY_INT;
2262 sc->mii_bus->parent = &pldev->dev;
2264 * Probe PHY address
2266 err = mdiobus_register(sc->mii_bus);
2267 if (err) {
2268 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2269 dev->name);
2270 goto free_mdio;
2272 dev_set_drvdata(&pldev->dev, sc->mii_bus);
2274 err = register_netdev(dev);
2275 if (err) {
2276 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2277 sbmac_string, idx);
2278 goto unreg_mdio;
2281 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2283 if (sc->rx_hw_checksum == ENABLE)
2284 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2287 * Display Ethernet address (this is called during the config
2288 * process so we need to finish off the config message that
2289 * was being displayed)
2291 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
2292 dev->name, base, eaddr);
2294 return 0;
2295 unreg_mdio:
2296 mdiobus_unregister(sc->mii_bus);
2297 dev_set_drvdata(&pldev->dev, NULL);
2298 free_mdio:
2299 mdiobus_free(sc->mii_bus);
2300 uninit_ctx:
2301 sbmac_uninitctx(sc);
2302 return err;
2306 static int sbmac_open(struct net_device *dev)
2308 struct sbmac_softc *sc = netdev_priv(dev);
2309 int err;
2311 if (debug > 1)
2312 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2315 * map/route interrupt (clear status first, in case something
2316 * weird is pending; we haven't initialized the mac registers
2317 * yet)
2320 __raw_readq(sc->sbm_isr);
2321 err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
2322 if (err) {
2323 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2324 dev->irq);
2325 goto out_err;
2328 sc->sbm_speed = sbmac_speed_none;
2329 sc->sbm_duplex = sbmac_duplex_none;
2330 sc->sbm_fc = sbmac_fc_none;
2331 sc->sbm_pause = -1;
2332 sc->sbm_link = 0;
2335 * Attach to the PHY
2337 err = sbmac_mii_probe(dev);
2338 if (err)
2339 goto out_unregister;
2342 * Turn on the channel
2345 sbmac_set_channel_state(sc,sbmac_state_on);
2347 netif_start_queue(dev);
2349 sbmac_set_rx_mode(dev);
2351 phy_start(sc->phy_dev);
2353 napi_enable(&sc->napi);
2355 return 0;
2357 out_unregister:
2358 free_irq(dev->irq, dev);
2359 out_err:
2360 return err;
2363 static int sbmac_mii_probe(struct net_device *dev)
2365 struct sbmac_softc *sc = netdev_priv(dev);
2366 struct phy_device *phy_dev;
2367 int i;
2369 for (i = 0; i < PHY_MAX_ADDR; i++) {
2370 phy_dev = sc->mii_bus->phy_map[i];
2371 if (phy_dev)
2372 break;
2374 if (!phy_dev) {
2375 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2376 return -ENXIO;
2379 phy_dev = phy_connect(dev, dev_name(&phy_dev->dev), &sbmac_mii_poll, 0,
2380 PHY_INTERFACE_MODE_GMII);
2381 if (IS_ERR(phy_dev)) {
2382 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2383 return PTR_ERR(phy_dev);
2386 /* Remove any features not supported by the controller */
2387 phy_dev->supported &= SUPPORTED_10baseT_Half |
2388 SUPPORTED_10baseT_Full |
2389 SUPPORTED_100baseT_Half |
2390 SUPPORTED_100baseT_Full |
2391 SUPPORTED_1000baseT_Half |
2392 SUPPORTED_1000baseT_Full |
2393 SUPPORTED_Autoneg |
2394 SUPPORTED_MII |
2395 SUPPORTED_Pause |
2396 SUPPORTED_Asym_Pause;
2397 phy_dev->advertising = phy_dev->supported;
2399 pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2400 dev->name, phy_dev->drv->name,
2401 dev_name(&phy_dev->dev), phy_dev->irq);
2403 sc->phy_dev = phy_dev;
2405 return 0;
2409 static void sbmac_mii_poll(struct net_device *dev)
2411 struct sbmac_softc *sc = netdev_priv(dev);
2412 struct phy_device *phy_dev = sc->phy_dev;
2413 unsigned long flags;
2414 enum sbmac_fc fc;
2415 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2417 link_chg = (sc->sbm_link != phy_dev->link);
2418 speed_chg = (sc->sbm_speed != phy_dev->speed);
2419 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2420 pause_chg = (sc->sbm_pause != phy_dev->pause);
2422 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2423 return; /* Hmmm... */
2425 if (!phy_dev->link) {
2426 if (link_chg) {
2427 sc->sbm_link = phy_dev->link;
2428 sc->sbm_speed = sbmac_speed_none;
2429 sc->sbm_duplex = sbmac_duplex_none;
2430 sc->sbm_fc = sbmac_fc_disabled;
2431 sc->sbm_pause = -1;
2432 pr_info("%s: link unavailable\n", dev->name);
2434 return;
2437 if (phy_dev->duplex == DUPLEX_FULL) {
2438 if (phy_dev->pause)
2439 fc = sbmac_fc_frame;
2440 else
2441 fc = sbmac_fc_disabled;
2442 } else
2443 fc = sbmac_fc_collision;
2444 fc_chg = (sc->sbm_fc != fc);
2446 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2447 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2449 spin_lock_irqsave(&sc->sbm_lock, flags);
2451 sc->sbm_speed = phy_dev->speed;
2452 sc->sbm_duplex = phy_dev->duplex;
2453 sc->sbm_fc = fc;
2454 sc->sbm_pause = phy_dev->pause;
2455 sc->sbm_link = phy_dev->link;
2457 if ((speed_chg || duplex_chg || fc_chg) &&
2458 sc->sbm_state != sbmac_state_off) {
2460 * something changed, restart the channel
2462 if (debug > 1)
2463 pr_debug("%s: restarting channel "
2464 "because PHY state changed\n", dev->name);
2465 sbmac_channel_stop(sc);
2466 sbmac_channel_start(sc);
2469 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2473 static void sbmac_tx_timeout (struct net_device *dev)
2475 struct sbmac_softc *sc = netdev_priv(dev);
2476 unsigned long flags;
2478 spin_lock_irqsave(&sc->sbm_lock, flags);
2481 dev->trans_start = jiffies; /* prevent tx timeout */
2482 dev->stats.tx_errors++;
2484 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2486 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2492 static void sbmac_set_rx_mode(struct net_device *dev)
2494 unsigned long flags;
2495 struct sbmac_softc *sc = netdev_priv(dev);
2497 spin_lock_irqsave(&sc->sbm_lock, flags);
2498 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2500 * Promiscuous changed.
2503 if (dev->flags & IFF_PROMISC) {
2504 sbmac_promiscuous_mode(sc,1);
2506 else {
2507 sbmac_promiscuous_mode(sc,0);
2510 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2513 * Program the multicasts. Do this every time.
2516 sbmac_setmulti(sc);
2520 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2522 struct sbmac_softc *sc = netdev_priv(dev);
2524 if (!netif_running(dev) || !sc->phy_dev)
2525 return -EINVAL;
2527 return phy_mii_ioctl(sc->phy_dev, rq, cmd);
2530 static int sbmac_close(struct net_device *dev)
2532 struct sbmac_softc *sc = netdev_priv(dev);
2534 napi_disable(&sc->napi);
2536 phy_stop(sc->phy_dev);
2538 sbmac_set_channel_state(sc, sbmac_state_off);
2540 netif_stop_queue(dev);
2542 if (debug > 1)
2543 pr_debug("%s: Shutting down ethercard\n", dev->name);
2545 phy_disconnect(sc->phy_dev);
2546 sc->phy_dev = NULL;
2547 free_irq(dev->irq, dev);
2549 sbdma_emptyring(&(sc->sbm_txdma));
2550 sbdma_emptyring(&(sc->sbm_rxdma));
2552 return 0;
2555 static int sbmac_poll(struct napi_struct *napi, int budget)
2557 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2558 int work_done;
2560 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
2561 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2563 if (work_done < budget) {
2564 napi_complete(napi);
2566 #ifdef CONFIG_SBMAC_COALESCE
2567 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2568 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2569 sc->sbm_imr);
2570 #else
2571 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2572 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2573 #endif
2576 return work_done;
2580 static int __devinit sbmac_probe(struct platform_device *pldev)
2582 struct net_device *dev;
2583 struct sbmac_softc *sc;
2584 void __iomem *sbm_base;
2585 struct resource *res;
2586 u64 sbmac_orig_hwaddr;
2587 int err;
2589 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2590 BUG_ON(!res);
2591 sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
2592 if (!sbm_base) {
2593 printk(KERN_ERR "%s: unable to map device registers\n",
2594 dev_name(&pldev->dev));
2595 err = -ENOMEM;
2596 goto out_out;
2600 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2601 * value for us by the firmware if we're going to use this MAC.
2602 * If we find a zero, skip this MAC.
2604 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2605 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
2606 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2607 if (sbmac_orig_hwaddr == 0) {
2608 err = 0;
2609 goto out_unmap;
2613 * Okay, cool. Initialize this MAC.
2615 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2616 if (!dev) {
2617 printk(KERN_ERR "%s: unable to allocate etherdev\n",
2618 dev_name(&pldev->dev));
2619 err = -ENOMEM;
2620 goto out_unmap;
2623 dev_set_drvdata(&pldev->dev, dev);
2624 SET_NETDEV_DEV(dev, &pldev->dev);
2626 sc = netdev_priv(dev);
2627 sc->sbm_base = sbm_base;
2629 err = sbmac_init(pldev, res->start);
2630 if (err)
2631 goto out_kfree;
2633 return 0;
2635 out_kfree:
2636 free_netdev(dev);
2637 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2639 out_unmap:
2640 iounmap(sbm_base);
2642 out_out:
2643 return err;
2646 static int __exit sbmac_remove(struct platform_device *pldev)
2648 struct net_device *dev = dev_get_drvdata(&pldev->dev);
2649 struct sbmac_softc *sc = netdev_priv(dev);
2651 unregister_netdev(dev);
2652 sbmac_uninitctx(sc);
2653 mdiobus_unregister(sc->mii_bus);
2654 mdiobus_free(sc->mii_bus);
2655 iounmap(sc->sbm_base);
2656 free_netdev(dev);
2658 return 0;
2661 static struct platform_driver sbmac_driver = {
2662 .probe = sbmac_probe,
2663 .remove = __exit_p(sbmac_remove),
2664 .driver = {
2665 .name = sbmac_string,
2666 .owner = THIS_MODULE,
2670 static int __init sbmac_init_module(void)
2672 return platform_driver_register(&sbmac_driver);
2675 static void __exit sbmac_cleanup_module(void)
2677 platform_driver_unregister(&sbmac_driver);
2680 module_init(sbmac_init_module);
2681 module_exit(sbmac_cleanup_module);