net/sb1250: remove CONFIG_SIBYTE_STANDALONE
[deliverable/linux.git] / drivers / net / sb1250-mac.c
CommitLineData
1da177e4 1/*
f90fdc3c 2 * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
f5279ffd 3 * Copyright (c) 2006, 2007 Maciej W. Rozycki
1da177e4
LT
4 *
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.
9 *
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.
74b0247f 14 *
1da177e4
LT
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.
18 *
19 *
20 * This driver is designed for the Broadcom SiByte SOC built-in
21 * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
f5279ffd
MR
22 *
23 * Updated to the driver model and the PHY abstraction layer
24 * by Maciej W. Rozycki.
1da177e4 25 */
f5279ffd
MR
26
27#include <linux/bug.h>
1da177e4
LT
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>
1da177e4 40#include <linux/bitops.h>
f5279ffd
MR
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>
46
1da177e4 47#include <asm/cache.h>
f5279ffd
MR
48#include <asm/io.h>
49#include <asm/processor.h> /* Processor type for cache alignment. */
1da177e4 50
1da177e4
LT
51/* Operational parameters that usually are not changed. */
52
53#define CONFIG_SBMAC_COALESCE
54
1da177e4
LT
55/* Time in jiffies before concluding the transmitter is hung. */
56#define TX_TIMEOUT (2*HZ)
57
58
59MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
60MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
61
62/* A few user-configurable values which may be modified when a driver
63 module is loaded. */
64
65/* 1 normal messages, 0 quiet .. 7 verbose. */
66static int debug = 1;
67module_param(debug, int, S_IRUGO);
68MODULE_PARM_DESC(debug, "Debug messages");
69
1da177e4 70#ifdef CONFIG_SBMAC_COALESCE
693aa947
MM
71static int int_pktcnt_tx = 255;
72module_param(int_pktcnt_tx, int, S_IRUGO);
73MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
1da177e4 74
693aa947
MM
75static int int_timeout_tx = 255;
76module_param(int_timeout_tx, int, S_IRUGO);
77MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
78
79static int int_pktcnt_rx = 64;
80module_param(int_pktcnt_rx, int, S_IRUGO);
81MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
82
83static int int_timeout_rx = 64;
84module_param(int_timeout_rx, int, S_IRUGO);
85MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
1da177e4
LT
86#endif
87
f5279ffd 88#include <asm/sibyte/board.h>
1da177e4 89#include <asm/sibyte/sb1250.h>
f90fdc3c
RB
90#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
91#include <asm/sibyte/bcm1480_regs.h>
92#include <asm/sibyte/bcm1480_int.h>
693aa947 93#define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
f90fdc3c 94#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1da177e4 95#include <asm/sibyte/sb1250_regs.h>
1da177e4 96#include <asm/sibyte/sb1250_int.h>
f90fdc3c
RB
97#else
98#error invalid SiByte MAC configuation
99#endif
1da177e4 100#include <asm/sibyte/sb1250_scd.h>
f90fdc3c
RB
101#include <asm/sibyte/sb1250_mac.h>
102#include <asm/sibyte/sb1250_dma.h>
1da177e4 103
f90fdc3c
RB
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
1da177e4 111
f5279ffd
MR
112#ifdef K_INT_PHY
113#define SBMAC_PHY_INT K_INT_PHY
114#else
115#define SBMAC_PHY_INT PHY_POLL
116#endif
117
1da177e4
LT
118/**********************************************************************
119 * Simple types
120 ********************************************************************* */
121
f5279ffd
MR
122enum 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,
127};
1da177e4 128
f5279ffd
MR
129enum sbmac_duplex {
130 sbmac_duplex_none = -1,
131 sbmac_duplex_half = DUPLEX_HALF,
132 sbmac_duplex_full = DUPLEX_FULL,
133};
1da177e4 134
f5279ffd
MR
135enum sbmac_fc {
136 sbmac_fc_none,
137 sbmac_fc_disabled,
138 sbmac_fc_frame,
139 sbmac_fc_collision,
140 sbmac_fc_carrier,
141};
1da177e4 142
f5279ffd
MR
143enum sbmac_state {
144 sbmac_state_uninit,
145 sbmac_state_off,
146 sbmac_state_on,
147 sbmac_state_broken,
148};
1da177e4
LT
149
150
151/**********************************************************************
152 * Macros
153 ********************************************************************* */
154
155
156#define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
157 (d)->sbdma_dscrtable : (d)->f+1)
158
159
160#define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
161
693aa947
MM
162#define SBMAC_MAX_TXDESCR 256
163#define SBMAC_MAX_RXDESCR 256
1da177e4 164
789585e9 165#define ETHER_ADDR_LEN 6
74b0247f
RB
166#define ENET_PACKET_SIZE 1518
167/*#define ENET_PACKET_SIZE 9216 */
1da177e4
LT
168
169/**********************************************************************
170 * DMA Descriptor structure
171 ********************************************************************* */
172
73d73969 173struct sbdmadscr {
1da177e4
LT
174 uint64_t dscr_a;
175 uint64_t dscr_b;
73d73969 176};
1da177e4
LT
177
178/**********************************************************************
179 * DMA Controller structure
180 ********************************************************************* */
181
73d73969 182struct sbmacdma {
74b0247f
RB
183
184 /*
1da177e4
LT
185 * This stuff is used to identify the channel and the registers
186 * associated with it.
187 */
73d73969
MR
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 */
1da177e4 194#ifdef CONFIG_SBMAC_COALESCE
73d73969
MR
195 int sbdma_int_pktcnt;
196 /* # descriptors rx/tx
197 before interrupt */
198 int sbdma_int_timeout;
199 /* # usec rx/tx interrupt */
1da177e4 200#endif
73d73969
MR
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) */
74b0247f 210
1da177e4
LT
211 /*
212 * This stuff is for maintenance of the ring
213 */
73d73969
MR
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 */
227};
1da177e4
LT
228
229
230/**********************************************************************
231 * Ethernet softc structure
232 ********************************************************************* */
233
234struct sbmac_softc {
74b0247f 235
1da177e4
LT
236 /*
237 * Linux-specific things
238 */
73d73969
MR
239 struct net_device *sbm_dev; /* pointer to linux device */
240 struct napi_struct napi;
f5279ffd 241 struct phy_device *phy_dev; /* the associated PHY device */
298cf9be 242 struct mii_bus *mii_bus; /* the MII bus */
f5279ffd 243 int phy_irq[PHY_MAX_ADDR];
73d73969 244 spinlock_t sbm_lock; /* spin lock */
73d73969 245 int sbm_devflags; /* current device flags */
74b0247f 246
1da177e4
LT
247 /*
248 * Controller-specific things
249 */
73d73969
MR
250 void __iomem *sbm_base; /* MAC's base address */
251 enum sbmac_state sbm_state; /* current state */
74b0247f 252
73d73969
MR
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 */
74b0247f 261
73d73969
MR
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 */
f5279ffd
MR
265 int sbm_pause; /* current pause setting */
266 int sbm_link; /* current link state */
74b0247f 267
73d73969 268 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
74b0247f 269
73d73969
MR
270 struct sbmacdma sbm_txdma; /* only channel 0 for now */
271 struct sbmacdma sbm_rxdma;
272 int rx_hw_checksum;
273 int sbe_idx;
1da177e4
LT
274};
275
276
277/**********************************************************************
278 * Externs
279 ********************************************************************* */
280
281/**********************************************************************
282 * Prototypes
283 ********************************************************************* */
284
73d73969
MR
285static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
286 int txrx, int maxdescr);
287static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
789585e9
SH
288static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
289 struct sk_buff *m);
73d73969
MR
290static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
291static void sbdma_emptyring(struct sbmacdma *d);
789585e9 292static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
73d73969
MR
293static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
294 int work_to_do, int poll);
295static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
296 int poll);
1da177e4
LT
297static int sbmac_initctx(struct sbmac_softc *s);
298static void sbmac_channel_start(struct sbmac_softc *s);
299static void sbmac_channel_stop(struct sbmac_softc *s);
73d73969
MR
300static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
301 enum sbmac_state);
302static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
1da177e4 303static uint64_t sbmac_addr2reg(unsigned char *ptr);
73d73969 304static irqreturn_t sbmac_intr(int irq, void *dev_instance);
1da177e4
LT
305static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
306static void sbmac_setmulti(struct sbmac_softc *sc);
f5279ffd 307static int sbmac_init(struct platform_device *pldev, long long base);
73d73969
MR
308static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
309static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
310 enum sbmac_fc fc);
1da177e4
LT
311
312static int sbmac_open(struct net_device *dev);
1da177e4 313static void sbmac_tx_timeout (struct net_device *dev);
1da177e4
LT
314static void sbmac_set_rx_mode(struct net_device *dev);
315static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
316static int sbmac_close(struct net_device *dev);
bea3348e 317static int sbmac_poll(struct napi_struct *napi, int budget);
693aa947 318
f5279ffd 319static void sbmac_mii_poll(struct net_device *dev);
59b81827 320static int sbmac_mii_probe(struct net_device *dev);
1da177e4 321
f5279ffd
MR
322static void sbmac_mii_sync(void __iomem *sbm_mdio);
323static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
73d73969 324 int bitcnt);
f5279ffd
MR
325static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
326static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
327 u16 val);
1da177e4
LT
328
329
330/**********************************************************************
331 * Globals
332 ********************************************************************* */
333
f5279ffd
MR
334static char sbmac_string[] = "sb1250-mac";
335static char sbmac_pretty[] = "SB1250 MAC";
336
337static char sbmac_mdio_string[] = "sb1250-mac-mdio";
1da177e4
LT
338
339
340/**********************************************************************
341 * MDIO constants
342 ********************************************************************* */
343
344#define MII_COMMAND_START 0x01
345#define MII_COMMAND_READ 0x02
346#define MII_COMMAND_WRITE 0x01
347#define MII_COMMAND_ACK 0x02
348
1da177e4
LT
349#define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
350
351#define ENABLE 1
352#define DISABLE 0
353
354/**********************************************************************
f5279ffd 355 * SBMAC_MII_SYNC(sbm_mdio)
74b0247f 356 *
1da177e4
LT
357 * Synchronize with the MII - send a pattern of bits to the MII
358 * that will guarantee that it is ready to accept a command.
74b0247f
RB
359 *
360 * Input parameters:
f5279ffd 361 * sbm_mdio - address of the MAC's MDIO register
74b0247f 362 *
1da177e4
LT
363 * Return value:
364 * nothing
365 ********************************************************************* */
366
f5279ffd 367static void sbmac_mii_sync(void __iomem *sbm_mdio)
1da177e4
LT
368{
369 int cnt;
370 uint64_t bits;
371 int mac_mdio_genc;
372
f5279ffd 373 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
74b0247f 374
1da177e4 375 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
74b0247f 376
f5279ffd 377 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
74b0247f 378
1da177e4 379 for (cnt = 0; cnt < 32; cnt++) {
f5279ffd
MR
380 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
381 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
1da177e4
LT
382 }
383}
384
385/**********************************************************************
f5279ffd 386 * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
74b0247f 387 *
1da177e4
LT
388 * Send some bits to the MII. The bits to be sent are right-
389 * justified in the 'data' parameter.
74b0247f
RB
390 *
391 * Input parameters:
f5279ffd
MR
392 * sbm_mdio - address of the MAC's MDIO register
393 * data - data to send
394 * bitcnt - number of bits to send
1da177e4
LT
395 ********************************************************************* */
396
f5279ffd
MR
397static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
398 int bitcnt)
1da177e4
LT
399{
400 int i;
401 uint64_t bits;
402 unsigned int curmask;
403 int mac_mdio_genc;
404
f5279ffd 405 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
74b0247f 406
1da177e4 407 bits = M_MAC_MDIO_DIR_OUTPUT;
f5279ffd 408 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
74b0247f 409
1da177e4 410 curmask = 1 << (bitcnt - 1);
74b0247f 411
1da177e4
LT
412 for (i = 0; i < bitcnt; i++) {
413 if (data & curmask)
414 bits |= M_MAC_MDIO_OUT;
415 else bits &= ~M_MAC_MDIO_OUT;
f5279ffd
MR
416 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
417 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
418 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
1da177e4
LT
419 curmask >>= 1;
420 }
421}
422
423
424
425/**********************************************************************
f5279ffd 426 * SBMAC_MII_READ(bus, phyaddr, regidx)
1da177e4 427 * Read a PHY register.
74b0247f
RB
428 *
429 * Input parameters:
f5279ffd 430 * bus - MDIO bus handle
1da177e4 431 * phyaddr - PHY's address
f5279ffd 432 * regnum - index of register to read
74b0247f 433 *
1da177e4 434 * Return value:
f5279ffd 435 * value read, or 0xffff if an error occurred.
1da177e4
LT
436 ********************************************************************* */
437
f5279ffd 438static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
1da177e4 439{
f5279ffd
MR
440 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
441 void __iomem *sbm_mdio = sc->sbm_mdio;
1da177e4
LT
442 int idx;
443 int error;
444 int regval;
445 int mac_mdio_genc;
446
447 /*
448 * Synchronize ourselves so that the PHY knows the next
449 * thing coming down is a command
450 */
f5279ffd 451 sbmac_mii_sync(sbm_mdio);
74b0247f 452
1da177e4
LT
453 /*
454 * Send the data to the PHY. The sequence is
455 * a "start" command (2 bits)
456 * a "read" command (2 bits)
457 * the PHY addr (5 bits)
458 * the register index (5 bits)
459 */
f5279ffd
MR
460 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
461 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
462 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
463 sbmac_mii_senddata(sbm_mdio, regidx, 5);
74b0247f 464
f5279ffd 465 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
74b0247f
RB
466
467 /*
1da177e4
LT
468 * Switch the port around without a clock transition.
469 */
f5279ffd 470 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
74b0247f 471
1da177e4
LT
472 /*
473 * Send out a clock pulse to signal we want the status
474 */
f5279ffd
MR
475 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
476 sbm_mdio);
477 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
74b0247f
RB
478
479 /*
1da177e4
LT
480 * If an error occurred, the PHY will signal '1' back
481 */
f5279ffd 482 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
74b0247f
RB
483
484 /*
1da177e4
LT
485 * Issue an 'idle' clock pulse, but keep the direction
486 * the same.
487 */
f5279ffd
MR
488 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
489 sbm_mdio);
490 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
74b0247f 491
1da177e4 492 regval = 0;
74b0247f 493
1da177e4
LT
494 for (idx = 0; idx < 16; idx++) {
495 regval <<= 1;
74b0247f 496
1da177e4 497 if (error == 0) {
f5279ffd 498 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
1da177e4
LT
499 regval |= 1;
500 }
74b0247f 501
f5279ffd
MR
502 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
503 sbm_mdio);
504 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
1da177e4 505 }
74b0247f 506
1da177e4 507 /* Switch back to output */
f5279ffd 508 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
74b0247f 509
1da177e4
LT
510 if (error == 0)
511 return regval;
f5279ffd 512 return 0xffff;
1da177e4
LT
513}
514
515
516/**********************************************************************
f5279ffd 517 * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
74b0247f 518 *
1da177e4 519 * Write a value to a PHY register.
74b0247f
RB
520 *
521 * Input parameters:
f5279ffd 522 * bus - MDIO bus handle
1da177e4 523 * phyaddr - PHY to use
f5279ffd
MR
524 * regidx - register within the PHY
525 * regval - data to write to register
74b0247f 526 *
1da177e4 527 * Return value:
f5279ffd 528 * 0 for success
1da177e4
LT
529 ********************************************************************* */
530
f5279ffd
MR
531static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
532 u16 regval)
1da177e4 533{
f5279ffd
MR
534 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
535 void __iomem *sbm_mdio = sc->sbm_mdio;
1da177e4
LT
536 int mac_mdio_genc;
537
f5279ffd 538 sbmac_mii_sync(sbm_mdio);
74b0247f 539
f5279ffd
MR
540 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
541 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
542 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
543 sbmac_mii_senddata(sbm_mdio, regidx, 5);
544 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
545 sbmac_mii_senddata(sbm_mdio, regval, 16);
1da177e4 546
f5279ffd 547 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
1da177e4 548
f5279ffd
MR
549 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
550
551 return 0;
1da177e4
LT
552}
553
554
555
556/**********************************************************************
557 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
74b0247f 558 *
1da177e4
LT
559 * Initialize a DMA channel context. Since there are potentially
560 * eight DMA channels per MAC, it's nice to do this in a standard
74b0247f
RB
561 * way.
562 *
563 * Input parameters:
73d73969
MR
564 * d - struct sbmacdma (DMA channel context)
565 * s - struct sbmac_softc (pointer to a MAC)
1da177e4
LT
566 * chan - channel number (0..1 right now)
567 * txrx - Identifies DMA_TX or DMA_RX for channel direction
568 * maxdescr - number of descriptors
74b0247f 569 *
1da177e4
LT
570 * Return value:
571 * nothing
572 ********************************************************************* */
573
73d73969
MR
574static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
575 int txrx, int maxdescr)
1da177e4 576{
693aa947
MM
577#ifdef CONFIG_SBMAC_COALESCE
578 int int_pktcnt, int_timeout;
579#endif
580
74b0247f
RB
581 /*
582 * Save away interesting stuff in the structure
1da177e4 583 */
74b0247f 584
1da177e4
LT
585 d->sbdma_eth = s;
586 d->sbdma_channel = chan;
587 d->sbdma_txdir = txrx;
74b0247f 588
1da177e4
LT
589#if 0
590 /* RMON clearing */
591 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
592#endif
593
f5279ffd
MR
594 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
595 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
596 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
597 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
598 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
599 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
600 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
601 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
602 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
603 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
604 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
605 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
606 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
607 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
608 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
609 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
610 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
611 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
612 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
613 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
614 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
1da177e4 615
74b0247f
RB
616 /*
617 * initialize register pointers
1da177e4 618 */
74b0247f
RB
619
620 d->sbdma_config0 =
1da177e4 621 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
74b0247f 622 d->sbdma_config1 =
1da177e4 623 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
74b0247f 624 d->sbdma_dscrbase =
1da177e4 625 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
74b0247f 626 d->sbdma_dscrcnt =
1da177e4 627 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
74b0247f 628 d->sbdma_curdscr =
1da177e4 629 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
693aa947
MM
630 if (d->sbdma_txdir)
631 d->sbdma_oodpktlost = NULL;
632 else
633 d->sbdma_oodpktlost =
634 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
74b0247f 635
1da177e4
LT
636 /*
637 * Allocate memory for the ring
638 */
74b0247f 639
1da177e4 640 d->sbdma_maxdescr = maxdescr;
74b0247f 641
73d73969
MR
642 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
643 sizeof(*d->sbdma_dscrtable),
644 GFP_KERNEL);
04115def
RB
645
646 /*
647 * The descriptor table must be aligned to at least 16 bytes or the
648 * MAC will corrupt it.
649 */
73d73969
MR
650 d->sbdma_dscrtable = (struct sbdmadscr *)
651 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
652 sizeof(*d->sbdma_dscrtable));
74b0247f 653
1da177e4 654 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
74b0247f 655
1da177e4 656 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
74b0247f 657
1da177e4
LT
658 /*
659 * And context table
660 */
74b0247f 661
c477f334 662 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
73d73969 663 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
74b0247f 664
1da177e4
LT
665#ifdef CONFIG_SBMAC_COALESCE
666 /*
667 * Setup Rx/Tx DMA coalescing defaults
668 */
669
693aa947 670 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
1da177e4
LT
671 if ( int_pktcnt ) {
672 d->sbdma_int_pktcnt = int_pktcnt;
673 } else {
674 d->sbdma_int_pktcnt = 1;
675 }
74b0247f 676
693aa947 677 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
1da177e4
LT
678 if ( int_timeout ) {
679 d->sbdma_int_timeout = int_timeout;
680 } else {
681 d->sbdma_int_timeout = 0;
682 }
683#endif
684
685}
686
687/**********************************************************************
688 * SBDMA_CHANNEL_START(d)
74b0247f 689 *
1da177e4 690 * Initialize the hardware registers for a DMA channel.
74b0247f
RB
691 *
692 * Input parameters:
1da177e4
LT
693 * d - DMA channel to init (context must be previously init'd
694 * rxtx - DMA_RX or DMA_TX depending on what type of channel
74b0247f 695 *
1da177e4
LT
696 * Return value:
697 * nothing
698 ********************************************************************* */
699
73d73969 700static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
1da177e4
LT
701{
702 /*
703 * Turn on the DMA channel
704 */
74b0247f 705
1da177e4 706#ifdef CONFIG_SBMAC_COALESCE
2039973a
RB
707 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
708 0, d->sbdma_config1);
709 __raw_writeq(M_DMA_EOP_INT_EN |
1da177e4
LT
710 V_DMA_RINGSZ(d->sbdma_maxdescr) |
711 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
2039973a 712 0, d->sbdma_config0);
1da177e4 713#else
2039973a
RB
714 __raw_writeq(0, d->sbdma_config1);
715 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
716 0, d->sbdma_config0);
1da177e4
LT
717#endif
718
2039973a 719 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
1da177e4
LT
720
721 /*
722 * Initialize ring pointers
723 */
724
725 d->sbdma_addptr = d->sbdma_dscrtable;
726 d->sbdma_remptr = d->sbdma_dscrtable;
727}
728
729/**********************************************************************
730 * SBDMA_CHANNEL_STOP(d)
74b0247f 731 *
1da177e4 732 * Initialize the hardware registers for a DMA channel.
74b0247f
RB
733 *
734 * Input parameters:
1da177e4 735 * d - DMA channel to init (context must be previously init'd
74b0247f 736 *
1da177e4
LT
737 * Return value:
738 * nothing
739 ********************************************************************* */
740
73d73969 741static void sbdma_channel_stop(struct sbmacdma *d)
1da177e4
LT
742{
743 /*
744 * Turn off the DMA channel
745 */
74b0247f 746
2039973a 747 __raw_writeq(0, d->sbdma_config1);
74b0247f 748
2039973a 749 __raw_writeq(0, d->sbdma_dscrbase);
74b0247f 750
2039973a 751 __raw_writeq(0, d->sbdma_config0);
74b0247f 752
1da177e4
LT
753 /*
754 * Zero ring pointers
755 */
74b0247f 756
2039973a
RB
757 d->sbdma_addptr = NULL;
758 d->sbdma_remptr = NULL;
1da177e4
LT
759}
760
789585e9
SH
761static inline void sbdma_align_skb(struct sk_buff *skb,
762 unsigned int power2, unsigned int offset)
1da177e4 763{
789585e9
SH
764 unsigned char *addr = skb->data;
765 unsigned char *newaddr = PTR_ALIGN(addr, power2);
74b0247f 766
789585e9 767 skb_reserve(skb, newaddr - addr + offset);
1da177e4
LT
768}
769
770
771/**********************************************************************
772 * SBDMA_ADD_RCVBUFFER(d,sb)
74b0247f 773 *
1da177e4
LT
774 * Add a buffer to the specified DMA channel. For receive channels,
775 * this queues a buffer for inbound packets.
74b0247f
RB
776 *
777 * Input parameters:
789585e9
SH
778 * sc - softc structure
779 * d - DMA channel descriptor
1da177e4 780 * sb - sk_buff to add, or NULL if we should allocate one
74b0247f 781 *
1da177e4
LT
782 * Return value:
783 * 0 if buffer could not be added (ring is full)
784 * 1 if buffer added successfully
785 ********************************************************************* */
786
787
789585e9
SH
788static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
789 struct sk_buff *sb)
1da177e4 790{
789585e9 791 struct net_device *dev = sc->sbm_dev;
73d73969
MR
792 struct sbdmadscr *dsc;
793 struct sbdmadscr *nextdsc;
1da177e4
LT
794 struct sk_buff *sb_new = NULL;
795 int pktsize = ENET_PACKET_SIZE;
74b0247f 796
1da177e4 797 /* get pointer to our current place in the ring */
74b0247f 798
1da177e4
LT
799 dsc = d->sbdma_addptr;
800 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
74b0247f 801
1da177e4
LT
802 /*
803 * figure out if the ring is full - if the next descriptor
804 * is the same as the one that we're going to remove from
805 * the ring, the ring is full
806 */
74b0247f 807
1da177e4
LT
808 if (nextdsc == d->sbdma_remptr) {
809 return -ENOSPC;
810 }
811
74b0247f
RB
812 /*
813 * Allocate a sk_buff if we don't already have one.
1da177e4
LT
814 * If we do have an sk_buff, reset it so that it's empty.
815 *
816 * Note: sk_buffs don't seem to be guaranteed to have any sort
817 * of alignment when they are allocated. Therefore, allocate enough
818 * extra space to make sure that:
819 *
820 * 1. the data does not start in the middle of a cache line.
821 * 2. The data does not end in the middle of a cache line
74b0247f 822 * 3. The buffer can be aligned such that the IP addresses are
1da177e4
LT
823 * naturally aligned.
824 *
825 * Remember, the SOCs MAC writes whole cache lines at a time,
826 * without reading the old contents first. So, if the sk_buff's
827 * data portion starts in the middle of a cache line, the SOC
828 * DMA will trash the beginning (and ending) portions.
829 */
74b0247f 830
1da177e4 831 if (sb == NULL) {
789585e9
SH
832 sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
833 SMP_CACHE_BYTES * 2 +
834 NET_IP_ALIGN);
1da177e4 835 if (sb_new == NULL) {
f5279ffd 836 pr_info("%s: sk_buff allocation failed\n",
1da177e4
LT
837 d->sbdma_eth->sbm_dev->name);
838 return -ENOBUFS;
839 }
840
789585e9 841 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
1da177e4
LT
842 }
843 else {
844 sb_new = sb;
74b0247f 845 /*
1da177e4
LT
846 * nothing special to reinit buffer, it's already aligned
847 * and sb->data already points to a good place.
848 */
849 }
74b0247f 850
1da177e4 851 /*
74b0247f 852 * fill in the descriptor
1da177e4 853 */
74b0247f 854
1da177e4
LT
855#ifdef CONFIG_SBMAC_COALESCE
856 /*
857 * Do not interrupt per DMA transfer.
858 */
689be439 859 dsc->dscr_a = virt_to_phys(sb_new->data) |
789585e9 860 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
1da177e4 861#else
689be439 862 dsc->dscr_a = virt_to_phys(sb_new->data) |
789585e9 863 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
1da177e4
LT
864 M_DMA_DSCRA_INTERRUPT;
865#endif
866
867 /* receiving: no options */
868 dsc->dscr_b = 0;
74b0247f 869
1da177e4 870 /*
74b0247f 871 * fill in the context
1da177e4 872 */
74b0247f 873
1da177e4 874 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
74b0247f
RB
875
876 /*
877 * point at next packet
1da177e4 878 */
74b0247f 879
1da177e4 880 d->sbdma_addptr = nextdsc;
74b0247f
RB
881
882 /*
1da177e4
LT
883 * Give the buffer to the DMA engine.
884 */
74b0247f 885
2039973a 886 __raw_writeq(1, d->sbdma_dscrcnt);
74b0247f 887
1da177e4
LT
888 return 0; /* we did it */
889}
890
891/**********************************************************************
892 * SBDMA_ADD_TXBUFFER(d,sb)
74b0247f 893 *
1da177e4
LT
894 * Add a transmit buffer to the specified DMA channel, causing a
895 * transmit to start.
74b0247f
RB
896 *
897 * Input parameters:
1da177e4
LT
898 * d - DMA channel descriptor
899 * sb - sk_buff to add
74b0247f 900 *
1da177e4
LT
901 * Return value:
902 * 0 transmit queued successfully
903 * otherwise error code
904 ********************************************************************* */
905
906
73d73969 907static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
1da177e4 908{
73d73969
MR
909 struct sbdmadscr *dsc;
910 struct sbdmadscr *nextdsc;
1da177e4
LT
911 uint64_t phys;
912 uint64_t ncb;
913 int length;
74b0247f 914
1da177e4 915 /* get pointer to our current place in the ring */
74b0247f 916
1da177e4
LT
917 dsc = d->sbdma_addptr;
918 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
74b0247f 919
1da177e4
LT
920 /*
921 * figure out if the ring is full - if the next descriptor
922 * is the same as the one that we're going to remove from
923 * the ring, the ring is full
924 */
74b0247f 925
1da177e4
LT
926 if (nextdsc == d->sbdma_remptr) {
927 return -ENOSPC;
928 }
74b0247f 929
1da177e4
LT
930 /*
931 * Under Linux, it's not necessary to copy/coalesce buffers
932 * like it is on NetBSD. We think they're all contiguous,
933 * but that may not be true for GBE.
934 */
74b0247f 935
1da177e4 936 length = sb->len;
74b0247f 937
1da177e4
LT
938 /*
939 * fill in the descriptor. Note that the number of cache
940 * blocks in the descriptor is the number of blocks
941 * *spanned*, so we need to add in the offset (if any)
942 * while doing the calculation.
943 */
74b0247f 944
1da177e4
LT
945 phys = virt_to_phys(sb->data);
946 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
947
74b0247f 948 dsc->dscr_a = phys |
1da177e4
LT
949 V_DMA_DSCRA_A_SIZE(ncb) |
950#ifndef CONFIG_SBMAC_COALESCE
951 M_DMA_DSCRA_INTERRUPT |
952#endif
953 M_DMA_ETHTX_SOP;
74b0247f 954
1da177e4
LT
955 /* transmitting: set outbound options and length */
956
957 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
958 V_DMA_DSCRB_PKT_SIZE(length);
74b0247f 959
1da177e4 960 /*
74b0247f 961 * fill in the context
1da177e4 962 */
74b0247f 963
1da177e4 964 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
74b0247f
RB
965
966 /*
967 * point at next packet
1da177e4 968 */
74b0247f 969
1da177e4 970 d->sbdma_addptr = nextdsc;
74b0247f
RB
971
972 /*
1da177e4
LT
973 * Give the buffer to the DMA engine.
974 */
74b0247f 975
2039973a 976 __raw_writeq(1, d->sbdma_dscrcnt);
74b0247f 977
1da177e4
LT
978 return 0; /* we did it */
979}
980
981
982
983
984/**********************************************************************
985 * SBDMA_EMPTYRING(d)
74b0247f 986 *
1da177e4 987 * Free all allocated sk_buffs on the specified DMA channel;
74b0247f
RB
988 *
989 * Input parameters:
1da177e4 990 * d - DMA channel
74b0247f 991 *
1da177e4
LT
992 * Return value:
993 * nothing
994 ********************************************************************* */
995
73d73969 996static void sbdma_emptyring(struct sbmacdma *d)
1da177e4
LT
997{
998 int idx;
999 struct sk_buff *sb;
74b0247f 1000
1da177e4
LT
1001 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
1002 sb = d->sbdma_ctxtable[idx];
1003 if (sb) {
1004 dev_kfree_skb(sb);
1005 d->sbdma_ctxtable[idx] = NULL;
1006 }
1007 }
1008}
1009
1010
1011/**********************************************************************
1012 * SBDMA_FILLRING(d)
74b0247f 1013 *
1da177e4
LT
1014 * Fill the specified DMA channel (must be receive channel)
1015 * with sk_buffs
74b0247f
RB
1016 *
1017 * Input parameters:
789585e9
SH
1018 * sc - softc structure
1019 * d - DMA channel
74b0247f 1020 *
1da177e4
LT
1021 * Return value:
1022 * nothing
1023 ********************************************************************* */
1024
789585e9 1025static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
1da177e4
LT
1026{
1027 int idx;
74b0247f 1028
789585e9
SH
1029 for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
1030 if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
1da177e4
LT
1031 break;
1032 }
1033}
1034
d6830018
DS
1035#ifdef CONFIG_NET_POLL_CONTROLLER
1036static void sbmac_netpoll(struct net_device *netdev)
1037{
1038 struct sbmac_softc *sc = netdev_priv(netdev);
1039 int irq = sc->sbm_dev->irq;
1040
1041 __raw_writeq(0, sc->sbm_imr);
1042
0da2f0f1 1043 sbmac_intr(irq, netdev);
d6830018
DS
1044
1045#ifdef CONFIG_SBMAC_COALESCE
1046 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1047 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1048 sc->sbm_imr);
1049#else
7d2e3cb7 1050 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
d6830018
DS
1051 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1052#endif
1053}
1054#endif
1da177e4
LT
1055
1056/**********************************************************************
693aa947 1057 * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
74b0247f
RB
1058 *
1059 * Process "completed" receive buffers on the specified DMA channel.
1da177e4 1060 *
74b0247f 1061 * Input parameters:
693aa947
MM
1062 * sc - softc structure
1063 * d - DMA channel context
1064 * work_to_do - no. of packets to process before enabling interrupt
1065 * again (for NAPI)
1066 * poll - 1: using polling (for NAPI)
74b0247f 1067 *
1da177e4
LT
1068 * Return value:
1069 * nothing
1070 ********************************************************************* */
1071
73d73969
MR
1072static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1073 int work_to_do, int poll)
1da177e4 1074{
09f75cd7 1075 struct net_device *dev = sc->sbm_dev;
1da177e4
LT
1076 int curidx;
1077 int hwidx;
73d73969 1078 struct sbdmadscr *dsc;
1da177e4
LT
1079 struct sk_buff *sb;
1080 int len;
693aa947
MM
1081 int work_done = 0;
1082 int dropped = 0;
74b0247f 1083
693aa947
MM
1084 prefetch(d);
1085
1086again:
1087 /* Check if the HW dropped any frames */
09f75cd7 1088 dev->stats.rx_fifo_errors
693aa947
MM
1089 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1090 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1091
1092 while (work_to_do-- > 0) {
74b0247f 1093 /*
1da177e4
LT
1094 * figure out where we are (as an index) and where
1095 * the hardware is (also as an index)
1096 *
74b0247f 1097 * This could be done faster if (for example) the
1da177e4
LT
1098 * descriptor table was page-aligned and contiguous in
1099 * both virtual and physical memory -- you could then
1100 * just compare the low-order bits of the virtual address
1101 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1102 */
74b0247f 1103
693aa947
MM
1104 dsc = d->sbdma_remptr;
1105 curidx = dsc - d->sbdma_dscrtable;
1106
1107 prefetch(dsc);
1108 prefetch(&d->sbdma_ctxtable[curidx]);
1109
73d73969
MR
1110 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1111 d->sbdma_dscrtable_phys) /
1112 sizeof(*d->sbdma_dscrtable);
74b0247f 1113
1da177e4
LT
1114 /*
1115 * If they're the same, that means we've processed all
1116 * of the descriptors up to (but not including) the one that
1117 * the hardware is working on right now.
1118 */
74b0247f 1119
1da177e4 1120 if (curidx == hwidx)
693aa947 1121 goto done;
74b0247f 1122
1da177e4
LT
1123 /*
1124 * Otherwise, get the packet's sk_buff ptr back
1125 */
74b0247f 1126
1da177e4
LT
1127 sb = d->sbdma_ctxtable[curidx];
1128 d->sbdma_ctxtable[curidx] = NULL;
74b0247f 1129
1da177e4 1130 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
74b0247f 1131
1da177e4
LT
1132 /*
1133 * Check packet status. If good, process it.
1134 * If not, silently drop it and put it back on the
1135 * receive ring.
1136 */
74b0247f 1137
693aa947 1138 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
74b0247f 1139
1da177e4
LT
1140 /*
1141 * Add a new buffer to replace the old one. If we fail
1142 * to allocate a buffer, we're going to drop this
1143 * packet and put it right back on the receive ring.
1144 */
74b0247f 1145
789585e9
SH
1146 if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
1147 -ENOBUFS)) {
09f75cd7 1148 dev->stats.rx_dropped++;
789585e9
SH
1149 /* Re-add old buffer */
1150 sbdma_add_rcvbuffer(sc, d, sb);
693aa947
MM
1151 /* No point in continuing at the moment */
1152 printk(KERN_ERR "dropped packet (1)\n");
1153 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1154 goto done;
1da177e4
LT
1155 } else {
1156 /*
1157 * Set length into the packet
1158 */
1159 skb_put(sb,len);
74b0247f 1160
1da177e4
LT
1161 /*
1162 * Buffer has been replaced on the
1163 * receive ring. Pass the buffer to
1164 * the kernel
1165 */
1da177e4
LT
1166 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1167 /* Check hw IPv4/TCP checksum if supported */
1168 if (sc->rx_hw_checksum == ENABLE) {
1169 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1170 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1171 sb->ip_summed = CHECKSUM_UNNECESSARY;
1172 /* don't need to set sb->csum */
1173 } else {
1174 sb->ip_summed = CHECKSUM_NONE;
1175 }
1176 }
693aa947
MM
1177 prefetch(sb->data);
1178 prefetch((const void *)(((char *)sb->data)+32));
1179 if (poll)
1180 dropped = netif_receive_skb(sb);
1181 else
1182 dropped = netif_rx(sb);
1183
1184 if (dropped == NET_RX_DROP) {
09f75cd7 1185 dev->stats.rx_dropped++;
693aa947
MM
1186 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1187 goto done;
1188 }
1189 else {
09f75cd7
JG
1190 dev->stats.rx_bytes += len;
1191 dev->stats.rx_packets++;
693aa947 1192 }
1da177e4
LT
1193 }
1194 } else {
1195 /*
1196 * Packet was mangled somehow. Just drop it and
1197 * put it back on the receive ring.
1198 */
09f75cd7 1199 dev->stats.rx_errors++;
789585e9 1200 sbdma_add_rcvbuffer(sc, d, sb);
1da177e4 1201 }
74b0247f
RB
1202
1203
1204 /*
1da177e4
LT
1205 * .. and advance to the next buffer.
1206 */
74b0247f 1207
1da177e4 1208 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
693aa947
MM
1209 work_done++;
1210 }
1211 if (!poll) {
1212 work_to_do = 32;
1213 goto again; /* collect fifo drop statistics again */
1da177e4 1214 }
693aa947
MM
1215done:
1216 return work_done;
1da177e4
LT
1217}
1218
1da177e4
LT
1219/**********************************************************************
1220 * SBDMA_TX_PROCESS(sc,d)
74b0247f
RB
1221 *
1222 * Process "completed" transmit buffers on the specified DMA channel.
1da177e4
LT
1223 * This is normally called within the interrupt service routine.
1224 * Note that this isn't really ideal for priority channels, since
74b0247f
RB
1225 * it processes all of the packets on a given channel before
1226 * returning.
1da177e4 1227 *
74b0247f 1228 * Input parameters:
1da177e4 1229 * sc - softc structure
693aa947
MM
1230 * d - DMA channel context
1231 * poll - 1: using polling (for NAPI)
74b0247f 1232 *
1da177e4
LT
1233 * Return value:
1234 * nothing
1235 ********************************************************************* */
1236
73d73969
MR
1237static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1238 int poll)
1da177e4 1239{
09f75cd7 1240 struct net_device *dev = sc->sbm_dev;
1da177e4
LT
1241 int curidx;
1242 int hwidx;
73d73969 1243 struct sbdmadscr *dsc;
1da177e4
LT
1244 struct sk_buff *sb;
1245 unsigned long flags;
693aa947 1246 int packets_handled = 0;
1da177e4
LT
1247
1248 spin_lock_irqsave(&(sc->sbm_lock), flags);
74b0247f 1249
693aa947
MM
1250 if (d->sbdma_remptr == d->sbdma_addptr)
1251 goto end_unlock;
1252
73d73969
MR
1253 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1254 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
693aa947 1255
1da177e4 1256 for (;;) {
74b0247f 1257 /*
1da177e4
LT
1258 * figure out where we are (as an index) and where
1259 * the hardware is (also as an index)
1260 *
74b0247f 1261 * This could be done faster if (for example) the
1da177e4
LT
1262 * descriptor table was page-aligned and contiguous in
1263 * both virtual and physical memory -- you could then
1264 * just compare the low-order bits of the virtual address
1265 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1266 */
74b0247f 1267
1da177e4 1268 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
1da177e4
LT
1269
1270 /*
1271 * If they're the same, that means we've processed all
1272 * of the descriptors up to (but not including) the one that
1273 * the hardware is working on right now.
1274 */
74b0247f 1275
1da177e4
LT
1276 if (curidx == hwidx)
1277 break;
74b0247f 1278
1da177e4
LT
1279 /*
1280 * Otherwise, get the packet's sk_buff ptr back
1281 */
74b0247f 1282
1da177e4
LT
1283 dsc = &(d->sbdma_dscrtable[curidx]);
1284 sb = d->sbdma_ctxtable[curidx];
1285 d->sbdma_ctxtable[curidx] = NULL;
74b0247f 1286
1da177e4
LT
1287 /*
1288 * Stats
1289 */
74b0247f 1290
09f75cd7
JG
1291 dev->stats.tx_bytes += sb->len;
1292 dev->stats.tx_packets++;
74b0247f 1293
1da177e4
LT
1294 /*
1295 * for transmits, we just free buffers.
1296 */
74b0247f 1297
1da177e4 1298 dev_kfree_skb_irq(sb);
74b0247f
RB
1299
1300 /*
1da177e4
LT
1301 * .. and advance to the next buffer.
1302 */
1303
1304 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
74b0247f 1305
693aa947
MM
1306 packets_handled++;
1307
1da177e4 1308 }
74b0247f 1309
1da177e4
LT
1310 /*
1311 * Decide if we should wake up the protocol or not.
1312 * Other drivers seem to do this when we reach a low
1313 * watermark on the transmit queue.
1314 */
74b0247f 1315
693aa947
MM
1316 if (packets_handled)
1317 netif_wake_queue(d->sbdma_eth->sbm_dev);
74b0247f 1318
693aa947 1319end_unlock:
1da177e4 1320 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
74b0247f 1321
1da177e4
LT
1322}
1323
1324
1325
1326/**********************************************************************
1327 * SBMAC_INITCTX(s)
74b0247f 1328 *
1da177e4
LT
1329 * Initialize an Ethernet context structure - this is called
1330 * once per MAC on the 1250. Memory is allocated here, so don't
1331 * call it again from inside the ioctl routines that bring the
1332 * interface up/down
74b0247f
RB
1333 *
1334 * Input parameters:
1da177e4 1335 * s - sbmac context structure
74b0247f 1336 *
1da177e4
LT
1337 * Return value:
1338 * 0
1339 ********************************************************************* */
1340
1341static int sbmac_initctx(struct sbmac_softc *s)
1342{
74b0247f
RB
1343
1344 /*
1345 * figure out the addresses of some ports
1da177e4 1346 */
74b0247f 1347
1da177e4
LT
1348 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1349 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1350 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1351 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1352 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1353 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1354 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1355 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1356
1da177e4
LT
1357 /*
1358 * Initialize the DMA channels. Right now, only one per MAC is used
1359 * Note: Only do this _once_, as it allocates memory from the kernel!
1360 */
74b0247f 1361
1da177e4
LT
1362 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1363 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
74b0247f 1364
1da177e4
LT
1365 /*
1366 * initial state is OFF
1367 */
74b0247f 1368
1da177e4 1369 s->sbm_state = sbmac_state_off;
74b0247f 1370
1da177e4
LT
1371 return 0;
1372}
1373
1374
73d73969 1375static void sbdma_uninitctx(struct sbmacdma *d)
1da177e4 1376{
693aa947
MM
1377 if (d->sbdma_dscrtable_unaligned) {
1378 kfree(d->sbdma_dscrtable_unaligned);
1379 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
1da177e4 1380 }
74b0247f 1381
1da177e4
LT
1382 if (d->sbdma_ctxtable) {
1383 kfree(d->sbdma_ctxtable);
1384 d->sbdma_ctxtable = NULL;
1385 }
1386}
1387
1388
1389static void sbmac_uninitctx(struct sbmac_softc *sc)
1390{
1391 sbdma_uninitctx(&(sc->sbm_txdma));
1392 sbdma_uninitctx(&(sc->sbm_rxdma));
1393}
1394
1395
1396/**********************************************************************
1397 * SBMAC_CHANNEL_START(s)
74b0247f 1398 *
1da177e4 1399 * Start packet processing on this MAC.
74b0247f
RB
1400 *
1401 * Input parameters:
1da177e4 1402 * s - sbmac structure
74b0247f 1403 *
1da177e4
LT
1404 * Return value:
1405 * nothing
1406 ********************************************************************* */
1407
1408static void sbmac_channel_start(struct sbmac_softc *s)
1409{
1410 uint64_t reg;
73d73969 1411 void __iomem *port;
1da177e4
LT
1412 uint64_t cfg,fifo,framecfg;
1413 int idx, th_value;
74b0247f 1414
1da177e4
LT
1415 /*
1416 * Don't do this if running
1417 */
1418
1419 if (s->sbm_state == sbmac_state_on)
1420 return;
74b0247f 1421
1da177e4
LT
1422 /*
1423 * Bring the controller out of reset, but leave it off.
1424 */
74b0247f 1425
2039973a 1426 __raw_writeq(0, s->sbm_macenable);
74b0247f 1427
1da177e4
LT
1428 /*
1429 * Ignore all received packets
1430 */
74b0247f 1431
2039973a 1432 __raw_writeq(0, s->sbm_rxfilter);
74b0247f
RB
1433
1434 /*
1da177e4
LT
1435 * Calculate values for various control registers.
1436 */
74b0247f 1437
1da177e4 1438 cfg = M_MAC_RETRY_EN |
74b0247f 1439 M_MAC_TX_HOLD_SOP_EN |
1da177e4
LT
1440 V_MAC_TX_PAUSE_CNT_16K |
1441 M_MAC_AP_STAT_EN |
1442 M_MAC_FAST_SYNC |
1443 M_MAC_SS_EN |
1444 0;
74b0247f
RB
1445
1446 /*
1da177e4
LT
1447 * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1448 * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1449 * Use a larger RD_THRSH for gigabit
1450 */
f90fdc3c 1451 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
1da177e4 1452 th_value = 28;
f90fdc3c
RB
1453 else
1454 th_value = 64;
1da177e4
LT
1455
1456 fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1457 ((s->sbm_speed == sbmac_speed_1000)
1458 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1459 V_MAC_TX_RL_THRSH(4) |
1460 V_MAC_RX_PL_THRSH(4) |
1461 V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
1da177e4
LT
1462 V_MAC_RX_RL_THRSH(8) |
1463 0;
1464
1465 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1466 V_MAC_MAX_FRAMESZ_DEFAULT |
1467 V_MAC_BACKOFF_SEL(1);
1468
1469 /*
74b0247f 1470 * Clear out the hash address map
1da177e4 1471 */
74b0247f 1472
1da177e4
LT
1473 port = s->sbm_base + R_MAC_HASH_BASE;
1474 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2039973a 1475 __raw_writeq(0, port);
1da177e4
LT
1476 port += sizeof(uint64_t);
1477 }
74b0247f 1478
1da177e4
LT
1479 /*
1480 * Clear out the exact-match table
1481 */
74b0247f 1482
1da177e4
LT
1483 port = s->sbm_base + R_MAC_ADDR_BASE;
1484 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
2039973a 1485 __raw_writeq(0, port);
1da177e4
LT
1486 port += sizeof(uint64_t);
1487 }
74b0247f 1488
1da177e4
LT
1489 /*
1490 * Clear out the DMA Channel mapping table registers
1491 */
74b0247f 1492
1da177e4
LT
1493 port = s->sbm_base + R_MAC_CHUP0_BASE;
1494 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
2039973a 1495 __raw_writeq(0, port);
1da177e4
LT
1496 port += sizeof(uint64_t);
1497 }
1498
1499
1500 port = s->sbm_base + R_MAC_CHLO0_BASE;
1501 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
2039973a 1502 __raw_writeq(0, port);
1da177e4
LT
1503 port += sizeof(uint64_t);
1504 }
74b0247f 1505
1da177e4
LT
1506 /*
1507 * Program the hardware address. It goes into the hardware-address
1508 * register as well as the first filter register.
1509 */
74b0247f 1510
1da177e4 1511 reg = sbmac_addr2reg(s->sbm_hwaddr);
74b0247f 1512
1da177e4 1513 port = s->sbm_base + R_MAC_ADDR_BASE;
2039973a 1514 __raw_writeq(reg, port);
1da177e4
LT
1515 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1516
1517#ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
1518 /*
1519 * Pass1 SOCs do not receive packets addressed to the
1520 * destination address in the R_MAC_ETHERNET_ADDR register.
1521 * Set the value to zero.
1522 */
2039973a 1523 __raw_writeq(0, port);
1da177e4 1524#else
2039973a 1525 __raw_writeq(reg, port);
1da177e4 1526#endif
74b0247f 1527
1da177e4
LT
1528 /*
1529 * Set the receive filter for no packets, and write values
1530 * to the various config registers
1531 */
74b0247f 1532
2039973a
RB
1533 __raw_writeq(0, s->sbm_rxfilter);
1534 __raw_writeq(0, s->sbm_imr);
1535 __raw_writeq(framecfg, s->sbm_framecfg);
1536 __raw_writeq(fifo, s->sbm_fifocfg);
1537 __raw_writeq(cfg, s->sbm_maccfg);
74b0247f 1538
1da177e4
LT
1539 /*
1540 * Initialize DMA channels (rings should be ok now)
1541 */
74b0247f 1542
1da177e4
LT
1543 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1544 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
74b0247f 1545
1da177e4
LT
1546 /*
1547 * Configure the speed, duplex, and flow control
1548 */
1549
1550 sbmac_set_speed(s,s->sbm_speed);
1551 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
74b0247f 1552
1da177e4
LT
1553 /*
1554 * Fill the receive ring
1555 */
74b0247f 1556
789585e9 1557 sbdma_fillring(s, &(s->sbm_rxdma));
74b0247f
RB
1558
1559 /*
1da177e4 1560 * Turn on the rest of the bits in the enable register
74b0247f
RB
1561 */
1562
f90fdc3c
RB
1563#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1564 __raw_writeq(M_MAC_RXDMA_EN0 |
1565 M_MAC_TXDMA_EN0, s->sbm_macenable);
1566#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
2039973a 1567 __raw_writeq(M_MAC_RXDMA_EN0 |
1da177e4
LT
1568 M_MAC_TXDMA_EN0 |
1569 M_MAC_RX_ENABLE |
2039973a 1570 M_MAC_TX_ENABLE, s->sbm_macenable);
f90fdc3c
RB
1571#else
1572#error invalid SiByte MAC configuation
1573#endif
1da177e4
LT
1574
1575#ifdef CONFIG_SBMAC_COALESCE
2039973a
RB
1576 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1577 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
1da177e4 1578#else
2039973a
RB
1579 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1580 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
1da177e4 1581#endif
74b0247f
RB
1582
1583 /*
1584 * Enable receiving unicasts and broadcasts
1da177e4 1585 */
74b0247f 1586
2039973a 1587 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
74b0247f 1588
1da177e4 1589 /*
74b0247f 1590 * we're running now.
1da177e4 1591 */
74b0247f 1592
1da177e4 1593 s->sbm_state = sbmac_state_on;
74b0247f
RB
1594
1595 /*
1596 * Program multicast addresses
1da177e4 1597 */
74b0247f 1598
1da177e4 1599 sbmac_setmulti(s);
74b0247f
RB
1600
1601 /*
1602 * If channel was in promiscuous mode before, turn that on
1da177e4 1603 */
74b0247f 1604
1da177e4
LT
1605 if (s->sbm_devflags & IFF_PROMISC) {
1606 sbmac_promiscuous_mode(s,1);
1607 }
74b0247f 1608
1da177e4
LT
1609}
1610
1611
1612/**********************************************************************
1613 * SBMAC_CHANNEL_STOP(s)
74b0247f 1614 *
1da177e4 1615 * Stop packet processing on this MAC.
74b0247f
RB
1616 *
1617 * Input parameters:
1da177e4 1618 * s - sbmac structure
74b0247f 1619 *
1da177e4
LT
1620 * Return value:
1621 * nothing
1622 ********************************************************************* */
1623
1624static void sbmac_channel_stop(struct sbmac_softc *s)
1625{
1626 /* don't do this if already stopped */
74b0247f 1627
1da177e4
LT
1628 if (s->sbm_state == sbmac_state_off)
1629 return;
74b0247f 1630
1da177e4 1631 /* don't accept any packets, disable all interrupts */
74b0247f 1632
2039973a
RB
1633 __raw_writeq(0, s->sbm_rxfilter);
1634 __raw_writeq(0, s->sbm_imr);
74b0247f 1635
1da177e4 1636 /* Turn off ticker */
74b0247f 1637
1da177e4 1638 /* XXX */
74b0247f 1639
1da177e4 1640 /* turn off receiver and transmitter */
74b0247f 1641
2039973a 1642 __raw_writeq(0, s->sbm_macenable);
74b0247f 1643
1da177e4 1644 /* We're stopped now. */
74b0247f 1645
1da177e4 1646 s->sbm_state = sbmac_state_off;
74b0247f 1647
1da177e4
LT
1648 /*
1649 * Stop DMA channels (rings should be ok now)
1650 */
74b0247f 1651
1da177e4
LT
1652 sbdma_channel_stop(&(s->sbm_rxdma));
1653 sbdma_channel_stop(&(s->sbm_txdma));
74b0247f 1654
1da177e4 1655 /* Empty the receive and transmit rings */
74b0247f 1656
1da177e4
LT
1657 sbdma_emptyring(&(s->sbm_rxdma));
1658 sbdma_emptyring(&(s->sbm_txdma));
74b0247f 1659
1da177e4
LT
1660}
1661
1662/**********************************************************************
1663 * SBMAC_SET_CHANNEL_STATE(state)
74b0247f 1664 *
1da177e4 1665 * Set the channel's state ON or OFF
74b0247f
RB
1666 *
1667 * Input parameters:
1da177e4 1668 * state - new state
74b0247f 1669 *
1da177e4
LT
1670 * Return value:
1671 * old state
1672 ********************************************************************* */
73d73969
MR
1673static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1674 enum sbmac_state state)
1da177e4 1675{
73d73969 1676 enum sbmac_state oldstate = sc->sbm_state;
74b0247f 1677
1da177e4
LT
1678 /*
1679 * If same as previous state, return
1680 */
74b0247f 1681
1da177e4
LT
1682 if (state == oldstate) {
1683 return oldstate;
1684 }
74b0247f 1685
1da177e4 1686 /*
74b0247f 1687 * If new state is ON, turn channel on
1da177e4 1688 */
74b0247f 1689
1da177e4
LT
1690 if (state == sbmac_state_on) {
1691 sbmac_channel_start(sc);
1692 }
1693 else {
1694 sbmac_channel_stop(sc);
1695 }
74b0247f 1696
1da177e4
LT
1697 /*
1698 * Return previous state
1699 */
74b0247f 1700
1da177e4
LT
1701 return oldstate;
1702}
1703
1704
1705/**********************************************************************
1706 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
74b0247f 1707 *
1da177e4 1708 * Turn on or off promiscuous mode
74b0247f
RB
1709 *
1710 * Input parameters:
1da177e4
LT
1711 * sc - softc
1712 * onoff - 1 to turn on, 0 to turn off
74b0247f 1713 *
1da177e4
LT
1714 * Return value:
1715 * nothing
1716 ********************************************************************* */
1717
1718static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1719{
1720 uint64_t reg;
74b0247f 1721
1da177e4
LT
1722 if (sc->sbm_state != sbmac_state_on)
1723 return;
74b0247f 1724
1da177e4 1725 if (onoff) {
2039973a 1726 reg = __raw_readq(sc->sbm_rxfilter);
1da177e4 1727 reg |= M_MAC_ALLPKT_EN;
2039973a 1728 __raw_writeq(reg, sc->sbm_rxfilter);
74b0247f 1729 }
1da177e4 1730 else {
2039973a 1731 reg = __raw_readq(sc->sbm_rxfilter);
1da177e4 1732 reg &= ~M_MAC_ALLPKT_EN;
2039973a 1733 __raw_writeq(reg, sc->sbm_rxfilter);
1da177e4
LT
1734 }
1735}
1736
1737/**********************************************************************
1738 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
74b0247f 1739 *
1da177e4 1740 * Set the iphdr offset as 15 assuming ethernet encapsulation
74b0247f
RB
1741 *
1742 * Input parameters:
1da177e4 1743 * sc - softc
74b0247f 1744 *
1da177e4
LT
1745 * Return value:
1746 * nothing
1747 ********************************************************************* */
1748
1749static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1750{
1751 uint64_t reg;
74b0247f 1752
1da177e4 1753 /* Hard code the off set to 15 for now */
2039973a 1754 reg = __raw_readq(sc->sbm_rxfilter);
1da177e4 1755 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
2039973a 1756 __raw_writeq(reg, sc->sbm_rxfilter);
74b0247f 1757
f90fdc3c
RB
1758 /* BCM1250 pass1 didn't have hardware checksum. Everything
1759 later does. */
1760 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
1da177e4 1761 sc->rx_hw_checksum = DISABLE;
f90fdc3c
RB
1762 } else {
1763 sc->rx_hw_checksum = ENABLE;
1da177e4
LT
1764 }
1765}
1766
1767
1768/**********************************************************************
1769 * SBMAC_ADDR2REG(ptr)
74b0247f 1770 *
1da177e4
LT
1771 * Convert six bytes into the 64-bit register value that
1772 * we typically write into the SBMAC's address/mcast registers
74b0247f
RB
1773 *
1774 * Input parameters:
1da177e4 1775 * ptr - pointer to 6 bytes
74b0247f 1776 *
1da177e4
LT
1777 * Return value:
1778 * register value
1779 ********************************************************************* */
1780
1781static uint64_t sbmac_addr2reg(unsigned char *ptr)
1782{
1783 uint64_t reg = 0;
74b0247f 1784
1da177e4 1785 ptr += 6;
74b0247f
RB
1786
1787 reg |= (uint64_t) *(--ptr);
1da177e4 1788 reg <<= 8;
74b0247f 1789 reg |= (uint64_t) *(--ptr);
1da177e4 1790 reg <<= 8;
74b0247f 1791 reg |= (uint64_t) *(--ptr);
1da177e4 1792 reg <<= 8;
74b0247f 1793 reg |= (uint64_t) *(--ptr);
1da177e4 1794 reg <<= 8;
74b0247f 1795 reg |= (uint64_t) *(--ptr);
1da177e4 1796 reg <<= 8;
74b0247f
RB
1797 reg |= (uint64_t) *(--ptr);
1798
1da177e4
LT
1799 return reg;
1800}
1801
1802
1803/**********************************************************************
1804 * SBMAC_SET_SPEED(s,speed)
74b0247f 1805 *
1da177e4
LT
1806 * Configure LAN speed for the specified MAC.
1807 * Warning: must be called when MAC is off!
74b0247f
RB
1808 *
1809 * Input parameters:
1da177e4 1810 * s - sbmac structure
73d73969 1811 * speed - speed to set MAC to (see enum sbmac_speed)
74b0247f 1812 *
1da177e4
LT
1813 * Return value:
1814 * 1 if successful
1815 * 0 indicates invalid parameters
1816 ********************************************************************* */
1817
73d73969 1818static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
1da177e4
LT
1819{
1820 uint64_t cfg;
1821 uint64_t framecfg;
1822
1823 /*
1824 * Save new current values
1825 */
74b0247f 1826
1da177e4 1827 s->sbm_speed = speed;
74b0247f 1828
1da177e4
LT
1829 if (s->sbm_state == sbmac_state_on)
1830 return 0; /* save for next restart */
1831
1832 /*
74b0247f 1833 * Read current register values
1da177e4 1834 */
74b0247f 1835
2039973a
RB
1836 cfg = __raw_readq(s->sbm_maccfg);
1837 framecfg = __raw_readq(s->sbm_framecfg);
74b0247f 1838
1da177e4
LT
1839 /*
1840 * Mask out the stuff we want to change
1841 */
74b0247f 1842
1da177e4
LT
1843 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1844 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1845 M_MAC_SLOT_SIZE);
74b0247f 1846
1da177e4
LT
1847 /*
1848 * Now add in the new bits
1849 */
74b0247f 1850
1da177e4
LT
1851 switch (speed) {
1852 case sbmac_speed_10:
1853 framecfg |= V_MAC_IFG_RX_10 |
1854 V_MAC_IFG_TX_10 |
1855 K_MAC_IFG_THRSH_10 |
1856 V_MAC_SLOT_SIZE_10;
1857 cfg |= V_MAC_SPEED_SEL_10MBPS;
1858 break;
74b0247f 1859
1da177e4
LT
1860 case sbmac_speed_100:
1861 framecfg |= V_MAC_IFG_RX_100 |
1862 V_MAC_IFG_TX_100 |
1863 V_MAC_IFG_THRSH_100 |
1864 V_MAC_SLOT_SIZE_100;
1865 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1866 break;
74b0247f 1867
1da177e4
LT
1868 case sbmac_speed_1000:
1869 framecfg |= V_MAC_IFG_RX_1000 |
1870 V_MAC_IFG_TX_1000 |
1871 V_MAC_IFG_THRSH_1000 |
1872 V_MAC_SLOT_SIZE_1000;
1873 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1874 break;
74b0247f 1875
1da177e4
LT
1876 default:
1877 return 0;
1878 }
74b0247f 1879
1da177e4 1880 /*
74b0247f 1881 * Send the bits back to the hardware
1da177e4 1882 */
74b0247f 1883
2039973a
RB
1884 __raw_writeq(framecfg, s->sbm_framecfg);
1885 __raw_writeq(cfg, s->sbm_maccfg);
74b0247f 1886
1da177e4
LT
1887 return 1;
1888}
1889
1890/**********************************************************************
1891 * SBMAC_SET_DUPLEX(s,duplex,fc)
74b0247f 1892 *
1da177e4
LT
1893 * Set Ethernet duplex and flow control options for this MAC
1894 * Warning: must be called when MAC is off!
74b0247f
RB
1895 *
1896 * Input parameters:
1da177e4 1897 * s - sbmac structure
73d73969
MR
1898 * duplex - duplex setting (see enum sbmac_duplex)
1899 * fc - flow control setting (see enum sbmac_fc)
74b0247f 1900 *
1da177e4
LT
1901 * Return value:
1902 * 1 if ok
1903 * 0 if an invalid parameter combination was specified
1904 ********************************************************************* */
1905
73d73969
MR
1906static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1907 enum sbmac_fc fc)
1da177e4
LT
1908{
1909 uint64_t cfg;
74b0247f 1910
1da177e4
LT
1911 /*
1912 * Save new current values
1913 */
74b0247f 1914
1da177e4
LT
1915 s->sbm_duplex = duplex;
1916 s->sbm_fc = fc;
74b0247f 1917
1da177e4
LT
1918 if (s->sbm_state == sbmac_state_on)
1919 return 0; /* save for next restart */
74b0247f 1920
1da177e4 1921 /*
74b0247f 1922 * Read current register values
1da177e4 1923 */
74b0247f 1924
2039973a 1925 cfg = __raw_readq(s->sbm_maccfg);
74b0247f 1926
1da177e4
LT
1927 /*
1928 * Mask off the stuff we're about to change
1929 */
74b0247f 1930
1da177e4 1931 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
74b0247f
RB
1932
1933
1da177e4
LT
1934 switch (duplex) {
1935 case sbmac_duplex_half:
1936 switch (fc) {
1937 case sbmac_fc_disabled:
1938 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1939 break;
74b0247f 1940
1da177e4
LT
1941 case sbmac_fc_collision:
1942 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1943 break;
74b0247f 1944
1da177e4
LT
1945 case sbmac_fc_carrier:
1946 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1947 break;
74b0247f 1948
1da177e4
LT
1949 case sbmac_fc_frame: /* not valid in half duplex */
1950 default: /* invalid selection */
1951 return 0;
1952 }
1953 break;
74b0247f 1954
1da177e4
LT
1955 case sbmac_duplex_full:
1956 switch (fc) {
1957 case sbmac_fc_disabled:
1958 cfg |= V_MAC_FC_CMD_DISABLED;
1959 break;
74b0247f 1960
1da177e4
LT
1961 case sbmac_fc_frame:
1962 cfg |= V_MAC_FC_CMD_ENABLED;
1963 break;
74b0247f 1964
1da177e4
LT
1965 case sbmac_fc_collision: /* not valid in full duplex */
1966 case sbmac_fc_carrier: /* not valid in full duplex */
1da177e4
LT
1967 default:
1968 return 0;
1969 }
1970 break;
f5279ffd
MR
1971 default:
1972 return 0;
1da177e4 1973 }
74b0247f 1974
1da177e4 1975 /*
74b0247f 1976 * Send the bits back to the hardware
1da177e4 1977 */
74b0247f 1978
2039973a 1979 __raw_writeq(cfg, s->sbm_maccfg);
74b0247f 1980
1da177e4
LT
1981 return 1;
1982}
1983
1984
1985
1986
1987/**********************************************************************
1988 * SBMAC_INTR()
74b0247f 1989 *
1da177e4 1990 * Interrupt handler for MAC interrupts
74b0247f
RB
1991 *
1992 * Input parameters:
1da177e4 1993 * MAC structure
74b0247f 1994 *
1da177e4
LT
1995 * Return value:
1996 * nothing
1997 ********************************************************************* */
7d12e780 1998static irqreturn_t sbmac_intr(int irq,void *dev_instance)
1da177e4
LT
1999{
2000 struct net_device *dev = (struct net_device *) dev_instance;
2001 struct sbmac_softc *sc = netdev_priv(dev);
2002 uint64_t isr;
2003 int handled = 0;
2004
693aa947
MM
2005 /*
2006 * Read the ISR (this clears the bits in the real
2007 * register, except for counter addr)
2008 */
74b0247f 2009
693aa947 2010 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
1da177e4 2011
693aa947
MM
2012 if (isr == 0)
2013 return IRQ_RETVAL(0);
2014 handled = 1;
74b0247f 2015
693aa947
MM
2016 /*
2017 * Transmits on channel 0
2018 */
74b0247f 2019
bea3348e 2020 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
693aa947 2021 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
74b0247f 2022
693aa947 2023 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
288379f0 2024 if (napi_schedule_prep(&sc->napi)) {
693aa947 2025 __raw_writeq(0, sc->sbm_imr);
288379f0 2026 __napi_schedule(&sc->napi);
693aa947
MM
2027 /* Depend on the exit from poll to reenable intr */
2028 }
2029 else {
2030 /* may leave some packets behind */
2031 sbdma_rx_process(sc,&(sc->sbm_rxdma),
2032 SBMAC_MAX_RXDESCR * 2, 0);
1da177e4
LT
2033 }
2034 }
2035 return IRQ_RETVAL(handled);
2036}
2037
1da177e4
LT
2038/**********************************************************************
2039 * SBMAC_START_TX(skb,dev)
74b0247f
RB
2040 *
2041 * Start output on the specified interface. Basically, we
1da177e4
LT
2042 * queue as many buffers as we can until the ring fills up, or
2043 * we run off the end of the queue, whichever comes first.
74b0247f
RB
2044 *
2045 * Input parameters:
2046 *
2047 *
1da177e4
LT
2048 * Return value:
2049 * nothing
2050 ********************************************************************* */
2051static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2052{
2053 struct sbmac_softc *sc = netdev_priv(dev);
be61ea52 2054 unsigned long flags;
74b0247f 2055
1da177e4 2056 /* lock eth irq */
be61ea52 2057 spin_lock_irqsave(&sc->sbm_lock, flags);
74b0247f 2058
1da177e4 2059 /*
74b0247f 2060 * Put the buffer on the transmit ring. If we
1da177e4
LT
2061 * don't have room, stop the queue.
2062 */
74b0247f 2063
1da177e4
LT
2064 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2065 /* XXX save skb that we could not send */
2066 netif_stop_queue(dev);
be61ea52 2067 spin_unlock_irqrestore(&sc->sbm_lock, flags);
1da177e4 2068
5b548140 2069 return NETDEV_TX_BUSY;
1da177e4 2070 }
74b0247f 2071
1da177e4 2072 dev->trans_start = jiffies;
74b0247f 2073
be61ea52 2074 spin_unlock_irqrestore(&sc->sbm_lock, flags);
74b0247f 2075
6ed10654 2076 return NETDEV_TX_OK;
1da177e4
LT
2077}
2078
2079/**********************************************************************
2080 * SBMAC_SETMULTI(sc)
74b0247f 2081 *
1da177e4
LT
2082 * Reprogram the multicast table into the hardware, given
2083 * the list of multicasts associated with the interface
2084 * structure.
74b0247f
RB
2085 *
2086 * Input parameters:
1da177e4 2087 * sc - softc
74b0247f 2088 *
1da177e4
LT
2089 * Return value:
2090 * nothing
2091 ********************************************************************* */
2092
2093static void sbmac_setmulti(struct sbmac_softc *sc)
2094{
2095 uint64_t reg;
73d73969 2096 void __iomem *port;
1da177e4 2097 int idx;
22bedad3 2098 struct netdev_hw_addr *ha;
1da177e4 2099 struct net_device *dev = sc->sbm_dev;
74b0247f
RB
2100
2101 /*
1da177e4
LT
2102 * Clear out entire multicast table. We do this by nuking
2103 * the entire hash table and all the direct matches except
74b0247f 2104 * the first one, which is used for our station address
1da177e4 2105 */
74b0247f 2106
1da177e4
LT
2107 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2108 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
2039973a 2109 __raw_writeq(0, port);
1da177e4 2110 }
74b0247f 2111
1da177e4
LT
2112 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2113 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
2039973a 2114 __raw_writeq(0, port);
1da177e4 2115 }
74b0247f 2116
1da177e4
LT
2117 /*
2118 * Clear the filter to say we don't want any multicasts.
2119 */
74b0247f 2120
2039973a 2121 reg = __raw_readq(sc->sbm_rxfilter);
1da177e4 2122 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2039973a 2123 __raw_writeq(reg, sc->sbm_rxfilter);
74b0247f 2124
1da177e4 2125 if (dev->flags & IFF_ALLMULTI) {
74b0247f
RB
2126 /*
2127 * Enable ALL multicasts. Do this by inverting the
2128 * multicast enable bit.
1da177e4 2129 */
2039973a 2130 reg = __raw_readq(sc->sbm_rxfilter);
1da177e4 2131 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2039973a 2132 __raw_writeq(reg, sc->sbm_rxfilter);
1da177e4
LT
2133 return;
2134 }
1da177e4 2135
74b0247f
RB
2136
2137 /*
1da177e4
LT
2138 * Progam new multicast entries. For now, only use the
2139 * perfect filter. In the future we'll need to use the
2140 * hash filter if the perfect filter overflows
2141 */
74b0247f 2142
1da177e4
LT
2143 /* XXX only using perfect filter for now, need to use hash
2144 * XXX if the table overflows */
74b0247f 2145
1da177e4 2146 idx = 1; /* skip station address */
22bedad3 2147 netdev_for_each_mc_addr(ha, dev) {
5508590c
JP
2148 if (idx == MAC_ADDR_COUNT)
2149 break;
22bedad3 2150 reg = sbmac_addr2reg(ha->addr);
1da177e4 2151 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
2039973a 2152 __raw_writeq(reg, port);
1da177e4 2153 idx++;
1da177e4 2154 }
74b0247f
RB
2155
2156 /*
1da177e4 2157 * Enable the "accept multicast bits" if we programmed at least one
74b0247f 2158 * multicast.
1da177e4 2159 */
74b0247f 2160
1da177e4 2161 if (idx > 1) {
2039973a 2162 reg = __raw_readq(sc->sbm_rxfilter);
1da177e4 2163 reg |= M_MAC_MCAST_EN;
2039973a 2164 __raw_writeq(reg, sc->sbm_rxfilter);
1da177e4
LT
2165 }
2166}
2167
1da177e4
LT
2168static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2169{
2170 if (new_mtu > ENET_PACKET_SIZE)
2171 return -EINVAL;
2172 _dev->mtu = new_mtu;
f5279ffd 2173 pr_info("changing the mtu to %d\n", new_mtu);
1da177e4
LT
2174 return 0;
2175}
2176
b4cf3421
AB
2177static const struct net_device_ops sbmac_netdev_ops = {
2178 .ndo_open = sbmac_open,
2179 .ndo_stop = sbmac_close,
2180 .ndo_start_xmit = sbmac_start_tx,
2181 .ndo_set_multicast_list = sbmac_set_rx_mode,
2182 .ndo_tx_timeout = sbmac_tx_timeout,
2183 .ndo_do_ioctl = sbmac_mii_ioctl,
2184 .ndo_change_mtu = sb1250_change_mtu,
2185 .ndo_validate_addr = eth_validate_addr,
2186 .ndo_set_mac_address = eth_mac_addr,
2187#ifdef CONFIG_NET_POLL_CONTROLLER
2188 .ndo_poll_controller = sbmac_netpoll,
2189#endif
2190};
2191
1da177e4
LT
2192/**********************************************************************
2193 * SBMAC_INIT(dev)
74b0247f 2194 *
1da177e4 2195 * Attach routine - init hardware and hook ourselves into linux
74b0247f
RB
2196 *
2197 * Input parameters:
1da177e4 2198 * dev - net_device structure
74b0247f 2199 *
1da177e4
LT
2200 * Return value:
2201 * status
2202 ********************************************************************* */
2203
f5279ffd 2204static int sbmac_init(struct platform_device *pldev, long long base)
1da177e4 2205{
c7ae011d 2206 struct net_device *dev = dev_get_drvdata(&pldev->dev);
f5279ffd
MR
2207 int idx = pldev->id;
2208 struct sbmac_softc *sc = netdev_priv(dev);
1da177e4
LT
2209 unsigned char *eaddr;
2210 uint64_t ea_reg;
2211 int i;
2212 int err;
74b0247f 2213
1da177e4
LT
2214 sc->sbm_dev = dev;
2215 sc->sbe_idx = idx;
74b0247f 2216
1da177e4 2217 eaddr = sc->sbm_hwaddr;
74b0247f
RB
2218
2219 /*
877d0310 2220 * Read the ethernet address. The firmware left this programmed
1da177e4
LT
2221 * for us in the ethernet address register for each mac.
2222 */
74b0247f 2223
2039973a
RB
2224 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2225 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
1da177e4
LT
2226 for (i = 0; i < 6; i++) {
2227 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2228 ea_reg >>= 8;
2229 }
74b0247f 2230
1da177e4
LT
2231 for (i = 0; i < 6; i++) {
2232 dev->dev_addr[i] = eaddr[i];
2233 }
74b0247f 2234
74b0247f 2235 /*
1da177e4
LT
2236 * Initialize context (get pointers to registers and stuff), then
2237 * allocate the memory for the descriptor tables.
2238 */
74b0247f 2239
1da177e4 2240 sbmac_initctx(sc);
74b0247f 2241
1da177e4
LT
2242 /*
2243 * Set up Linux device callins
2244 */
74b0247f 2245
1da177e4 2246 spin_lock_init(&(sc->sbm_lock));
74b0247f 2247
b4cf3421
AB
2248 dev->netdev_ops = &sbmac_netdev_ops;
2249 dev->watchdog_timeo = TX_TIMEOUT;
bea3348e
SH
2250
2251 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
1da177e4 2252
f5279ffd
MR
2253 dev->irq = UNIT_INT(idx);
2254
1da177e4
LT
2255 /* This is needed for PASS2 for Rx H/W checksum feature */
2256 sbmac_set_iphdr_offset(sc);
2257
298cf9be
LB
2258 sc->mii_bus = mdiobus_alloc();
2259 if (sc->mii_bus == NULL) {
2260 sbmac_uninitctx(sc);
2261 return -ENOMEM;
2262 }
2263
1da177e4 2264 err = register_netdev(dev);
f5279ffd
MR
2265 if (err) {
2266 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2267 sbmac_string, idx);
298cf9be 2268 mdiobus_free(sc->mii_bus);
f5279ffd
MR
2269 sbmac_uninitctx(sc);
2270 return err;
1da177e4
LT
2271 }
2272
f5279ffd
MR
2273 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2274
2275 if (sc->rx_hw_checksum == ENABLE)
2276 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2277
1da177e4
LT
2278 /*
2279 * Display Ethernet address (this is called during the config
2280 * process so we need to finish off the config message that
2281 * was being displayed)
2282 */
e174961c
JB
2283 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
2284 dev->name, base, eaddr);
1da177e4 2285
298cf9be
LB
2286 sc->mii_bus->name = sbmac_mdio_string;
2287 snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%x", idx);
2288 sc->mii_bus->priv = sc;
2289 sc->mii_bus->read = sbmac_mii_read;
2290 sc->mii_bus->write = sbmac_mii_write;
2291 sc->mii_bus->irq = sc->phy_irq;
f5279ffd 2292 for (i = 0; i < PHY_MAX_ADDR; ++i)
298cf9be 2293 sc->mii_bus->irq[i] = SBMAC_PHY_INT;
1da177e4 2294
298cf9be
LB
2295 sc->mii_bus->parent = &pldev->dev;
2296 dev_set_drvdata(&pldev->dev, sc->mii_bus);
1da177e4 2297
f5279ffd 2298 return 0;
1da177e4
LT
2299}
2300
2301
2302static int sbmac_open(struct net_device *dev)
2303{
2304 struct sbmac_softc *sc = netdev_priv(dev);
f5279ffd 2305 int err;
74b0247f 2306
f5279ffd
MR
2307 if (debug > 1)
2308 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
74b0247f
RB
2309
2310 /*
1da177e4
LT
2311 * map/route interrupt (clear status first, in case something
2312 * weird is pending; we haven't initialized the mac registers
2313 * yet)
2314 */
2315
2039973a 2316 __raw_readq(sc->sbm_isr);
a0607fd3 2317 err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
f5279ffd
MR
2318 if (err) {
2319 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2320 dev->irq);
2321 goto out_err;
2322 }
1da177e4 2323
59b81827 2324 /*
f5279ffd 2325 * Probe PHY address
59b81827 2326 */
298cf9be 2327 err = mdiobus_register(sc->mii_bus);
f5279ffd
MR
2328 if (err) {
2329 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2330 dev->name);
2331 goto out_unirq;
59b81827
RB
2332 }
2333
f5279ffd
MR
2334 sc->sbm_speed = sbmac_speed_none;
2335 sc->sbm_duplex = sbmac_duplex_none;
2336 sc->sbm_fc = sbmac_fc_none;
2337 sc->sbm_pause = -1;
2338 sc->sbm_link = 0;
bea3348e 2339
1da177e4 2340 /*
f5279ffd 2341 * Attach to the PHY
1da177e4 2342 */
f5279ffd
MR
2343 err = sbmac_mii_probe(dev);
2344 if (err)
2345 goto out_unregister;
74b0247f 2346
1da177e4
LT
2347 /*
2348 * Turn on the channel
2349 */
2350
2351 sbmac_set_channel_state(sc,sbmac_state_on);
74b0247f 2352
1da177e4 2353 netif_start_queue(dev);
74b0247f 2354
1da177e4 2355 sbmac_set_rx_mode(dev);
74b0247f 2356
f5279ffd
MR
2357 phy_start(sc->phy_dev);
2358
2359 napi_enable(&sc->napi);
74b0247f 2360
1da177e4 2361 return 0;
f5279ffd
MR
2362
2363out_unregister:
298cf9be 2364 mdiobus_unregister(sc->mii_bus);
f5279ffd
MR
2365
2366out_unirq:
2367 free_irq(dev->irq, dev);
2368
2369out_err:
2370 return err;
1da177e4
LT
2371}
2372
59b81827
RB
2373static int sbmac_mii_probe(struct net_device *dev)
2374{
f5279ffd
MR
2375 struct sbmac_softc *sc = netdev_priv(dev);
2376 struct phy_device *phy_dev;
59b81827 2377 int i;
1da177e4 2378
f5279ffd 2379 for (i = 0; i < PHY_MAX_ADDR; i++) {
298cf9be 2380 phy_dev = sc->mii_bus->phy_map[i];
f5279ffd
MR
2381 if (phy_dev)
2382 break;
1da177e4 2383 }
f5279ffd
MR
2384 if (!phy_dev) {
2385 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2386 return -ENXIO;
1da177e4
LT
2387 }
2388
db1d7bf7 2389 phy_dev = phy_connect(dev, dev_name(&phy_dev->dev), &sbmac_mii_poll, 0,
f5279ffd
MR
2390 PHY_INTERFACE_MODE_GMII);
2391 if (IS_ERR(phy_dev)) {
2392 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2393 return PTR_ERR(phy_dev);
1da177e4
LT
2394 }
2395
f5279ffd
MR
2396 /* Remove any features not supported by the controller */
2397 phy_dev->supported &= SUPPORTED_10baseT_Half |
2398 SUPPORTED_10baseT_Full |
2399 SUPPORTED_100baseT_Half |
2400 SUPPORTED_100baseT_Full |
2401 SUPPORTED_1000baseT_Half |
2402 SUPPORTED_1000baseT_Full |
2403 SUPPORTED_Autoneg |
2404 SUPPORTED_MII |
2405 SUPPORTED_Pause |
2406 SUPPORTED_Asym_Pause;
2407 phy_dev->advertising = phy_dev->supported;
2408
2409 pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2410 dev->name, phy_dev->drv->name,
db1d7bf7 2411 dev_name(&phy_dev->dev), phy_dev->irq);
f5279ffd
MR
2412
2413 sc->phy_dev = phy_dev;
1da177e4 2414
f5279ffd 2415 return 0;
1da177e4
LT
2416}
2417
2418
f5279ffd 2419static void sbmac_mii_poll(struct net_device *dev)
1da177e4 2420{
1da177e4 2421 struct sbmac_softc *sc = netdev_priv(dev);
f5279ffd
MR
2422 struct phy_device *phy_dev = sc->phy_dev;
2423 unsigned long flags;
2424 enum sbmac_fc fc;
2425 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2426
2427 link_chg = (sc->sbm_link != phy_dev->link);
2428 speed_chg = (sc->sbm_speed != phy_dev->speed);
2429 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2430 pause_chg = (sc->sbm_pause != phy_dev->pause);
2431
2432 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2433 return; /* Hmmm... */
2434
2435 if (!phy_dev->link) {
2436 if (link_chg) {
2437 sc->sbm_link = phy_dev->link;
2438 sc->sbm_speed = sbmac_speed_none;
2439 sc->sbm_duplex = sbmac_duplex_none;
2440 sc->sbm_fc = sbmac_fc_disabled;
2441 sc->sbm_pause = -1;
2442 pr_info("%s: link unavailable\n", dev->name);
2443 }
2444 return;
2445 }
1da177e4 2446
f5279ffd
MR
2447 if (phy_dev->duplex == DUPLEX_FULL) {
2448 if (phy_dev->pause)
2449 fc = sbmac_fc_frame;
2450 else
2451 fc = sbmac_fc_disabled;
2452 } else
2453 fc = sbmac_fc_collision;
2454 fc_chg = (sc->sbm_fc != fc);
74b0247f 2455
f5279ffd
MR
2456 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2457 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
74b0247f 2458
f5279ffd 2459 spin_lock_irqsave(&sc->sbm_lock, flags);
74b0247f 2460
f5279ffd
MR
2461 sc->sbm_speed = phy_dev->speed;
2462 sc->sbm_duplex = phy_dev->duplex;
2463 sc->sbm_fc = fc;
2464 sc->sbm_pause = phy_dev->pause;
2465 sc->sbm_link = phy_dev->link;
1da177e4 2466
f5279ffd
MR
2467 if ((speed_chg || duplex_chg || fc_chg) &&
2468 sc->sbm_state != sbmac_state_off) {
2469 /*
2470 * something changed, restart the channel
2471 */
2472 if (debug > 1)
2473 pr_debug("%s: restarting channel "
2474 "because PHY state changed\n", dev->name);
2475 sbmac_channel_stop(sc);
2476 sbmac_channel_start(sc);
1da177e4 2477 }
74b0247f 2478
f5279ffd 2479 spin_unlock_irqrestore(&sc->sbm_lock, flags);
1da177e4
LT
2480}
2481
2482
2483static void sbmac_tx_timeout (struct net_device *dev)
2484{
2485 struct sbmac_softc *sc = netdev_priv(dev);
be61ea52 2486 unsigned long flags;
74b0247f 2487
be61ea52 2488 spin_lock_irqsave(&sc->sbm_lock, flags);
74b0247f
RB
2489
2490
1da177e4 2491 dev->trans_start = jiffies;
09f75cd7 2492 dev->stats.tx_errors++;
74b0247f 2493
be61ea52 2494 spin_unlock_irqrestore(&sc->sbm_lock, flags);
1da177e4
LT
2495
2496 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2497}
2498
2499
2500
2501
1da177e4
LT
2502static void sbmac_set_rx_mode(struct net_device *dev)
2503{
2504 unsigned long flags;
1da177e4
LT
2505 struct sbmac_softc *sc = netdev_priv(dev);
2506
2507 spin_lock_irqsave(&sc->sbm_lock, flags);
2508 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2509 /*
2510 * Promiscuous changed.
2511 */
74b0247f
RB
2512
2513 if (dev->flags & IFF_PROMISC) {
1da177e4
LT
2514 sbmac_promiscuous_mode(sc,1);
2515 }
2516 else {
1da177e4
LT
2517 sbmac_promiscuous_mode(sc,0);
2518 }
2519 }
2520 spin_unlock_irqrestore(&sc->sbm_lock, flags);
74b0247f 2521
1da177e4
LT
2522 /*
2523 * Program the multicasts. Do this every time.
2524 */
74b0247f 2525
1da177e4 2526 sbmac_setmulti(sc);
74b0247f 2527
1da177e4
LT
2528}
2529
2530static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2531{
2532 struct sbmac_softc *sc = netdev_priv(dev);
74b0247f 2533
f5279ffd
MR
2534 if (!netif_running(dev) || !sc->phy_dev)
2535 return -EINVAL;
74b0247f 2536
f5279ffd 2537 return phy_mii_ioctl(sc->phy_dev, if_mii(rq), cmd);
1da177e4
LT
2538}
2539
2540static int sbmac_close(struct net_device *dev)
2541{
2542 struct sbmac_softc *sc = netdev_priv(dev);
1da177e4 2543
bea3348e
SH
2544 napi_disable(&sc->napi);
2545
f5279ffd 2546 phy_stop(sc->phy_dev);
1da177e4 2547
f5279ffd 2548 sbmac_set_channel_state(sc, sbmac_state_off);
1da177e4
LT
2549
2550 netif_stop_queue(dev);
2551
f5279ffd
MR
2552 if (debug > 1)
2553 pr_debug("%s: Shutting down ethercard\n", dev->name);
1da177e4 2554
f5279ffd
MR
2555 phy_disconnect(sc->phy_dev);
2556 sc->phy_dev = NULL;
2557
298cf9be 2558 mdiobus_unregister(sc->mii_bus);
1da177e4 2559
f5279ffd 2560 free_irq(dev->irq, dev);
1da177e4
LT
2561
2562 sbdma_emptyring(&(sc->sbm_txdma));
2563 sbdma_emptyring(&(sc->sbm_rxdma));
74b0247f 2564
1da177e4
LT
2565 return 0;
2566}
2567
bea3348e 2568static int sbmac_poll(struct napi_struct *napi, int budget)
693aa947 2569{
bea3348e 2570 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
693aa947 2571 int work_done;
1da177e4 2572
bea3348e 2573 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
693aa947
MM
2574 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2575
bea3348e 2576 if (work_done < budget) {
288379f0 2577 napi_complete(napi);
693aa947
MM
2578
2579#ifdef CONFIG_SBMAC_COALESCE
2580 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2581 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2582 sc->sbm_imr);
2583#else
2584 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2585 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2586#endif
2587 }
2588
bea3348e 2589 return work_done;
693aa947 2590}
1da177e4 2591
f5279ffd 2592
ffe8553f 2593static int __devinit sbmac_probe(struct platform_device *pldev)
f5279ffd
MR
2594{
2595 struct net_device *dev;
2596 struct sbmac_softc *sc;
2597 void __iomem *sbm_base;
2598 struct resource *res;
2599 u64 sbmac_orig_hwaddr;
2600 int err;
2601
2602 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2603 BUG_ON(!res);
2604 sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
2605 if (!sbm_base) {
2606 printk(KERN_ERR "%s: unable to map device registers\n",
db1d7bf7 2607 dev_name(&pldev->dev));
f5279ffd
MR
2608 err = -ENOMEM;
2609 goto out_out;
2610 }
2611
2612 /*
2613 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2614 * value for us by the firmware if we're going to use this MAC.
2615 * If we find a zero, skip this MAC.
2616 */
2617 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
db1d7bf7 2618 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
f5279ffd
MR
2619 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2620 if (sbmac_orig_hwaddr == 0) {
2621 err = 0;
2622 goto out_unmap;
2623 }
2624
2625 /*
2626 * Okay, cool. Initialize this MAC.
2627 */
2628 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2629 if (!dev) {
2630 printk(KERN_ERR "%s: unable to allocate etherdev\n",
db1d7bf7 2631 dev_name(&pldev->dev));
f5279ffd
MR
2632 err = -ENOMEM;
2633 goto out_unmap;
2634 }
2635
c7ae011d 2636 dev_set_drvdata(&pldev->dev, dev);
f5279ffd
MR
2637 SET_NETDEV_DEV(dev, &pldev->dev);
2638
2639 sc = netdev_priv(dev);
2640 sc->sbm_base = sbm_base;
2641
2642 err = sbmac_init(pldev, res->start);
2643 if (err)
2644 goto out_kfree;
2645
2646 return 0;
2647
2648out_kfree:
2649 free_netdev(dev);
2650 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2651
2652out_unmap:
2653 iounmap(sbm_base);
2654
2655out_out:
2656 return err;
2657}
2658
2659static int __exit sbmac_remove(struct platform_device *pldev)
2660{
c7ae011d 2661 struct net_device *dev = dev_get_drvdata(&pldev->dev);
f5279ffd
MR
2662 struct sbmac_softc *sc = netdev_priv(dev);
2663
2664 unregister_netdev(dev);
2665 sbmac_uninitctx(sc);
298cf9be 2666 mdiobus_free(sc->mii_bus);
f5279ffd
MR
2667 iounmap(sc->sbm_base);
2668 free_netdev(dev);
2669
2670 return 0;
2671}
2672
2673
2674static struct platform_device **sbmac_pldev;
2675static int sbmac_max_units;
2676
f5279ffd
MR
2677static int __init sbmac_platform_probe_one(int idx)
2678{
2679 struct platform_device *pldev;
2680 struct {
2681 struct resource r;
2682 char name[strlen(sbmac_pretty) + 4];
2683 } *res;
2684 int err;
2685
2686 res = kzalloc(sizeof(*res), GFP_KERNEL);
2687 if (!res) {
2688 printk(KERN_ERR "%s.%d: unable to allocate memory\n",
2689 sbmac_string, idx);
2690 err = -ENOMEM;
2691 goto out_err;
2692 }
2693
2694 /*
2695 * This is the base address of the MAC.
2696 */
2697 snprintf(res->name, sizeof(res->name), "%s %d", sbmac_pretty, idx);
2698 res->r.name = res->name;
2699 res->r.flags = IORESOURCE_MEM;
2700 res->r.start = A_MAC_CHANNEL_BASE(idx);
2701 res->r.end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2702
2703 pldev = platform_device_register_simple(sbmac_string, idx, &res->r, 1);
2704 if (IS_ERR(pldev)) {
2705 printk(KERN_ERR "%s.%d: unable to register platform device\n",
2706 sbmac_string, idx);
2707 err = PTR_ERR(pldev);
2708 goto out_kfree;
2709 }
2710
2711 if (!pldev->dev.driver) {
2712 err = 0; /* No hardware at this address. */
2713 goto out_unregister;
2714 }
2715
2716 sbmac_pldev[idx] = pldev;
2717 return 0;
2718
2719out_unregister:
2720 platform_device_unregister(pldev);
1da177e4 2721
f5279ffd
MR
2722out_kfree:
2723 kfree(res);
2724
2725out_err:
2726 return err;
2727}
2728
2729static void __init sbmac_platform_probe(void)
1da177e4 2730{
f5279ffd 2731 int i;
74b0247f 2732
f90fdc3c 2733 /* Set the number of available units based on the SOC type. */
1da177e4
LT
2734 switch (soc_type) {
2735 case K_SYS_SOC_TYPE_BCM1250:
2736 case K_SYS_SOC_TYPE_BCM1250_ALT:
f5279ffd 2737 sbmac_max_units = 3;
1da177e4
LT
2738 break;
2739 case K_SYS_SOC_TYPE_BCM1120:
2740 case K_SYS_SOC_TYPE_BCM1125:
2741 case K_SYS_SOC_TYPE_BCM1125H:
f5279ffd
MR
2742 case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
2743 sbmac_max_units = 2;
1da177e4 2744 break;
f90fdc3c
RB
2745 case K_SYS_SOC_TYPE_BCM1x55:
2746 case K_SYS_SOC_TYPE_BCM1x80:
f5279ffd 2747 sbmac_max_units = 4;
f90fdc3c 2748 break;
1da177e4 2749 default:
f5279ffd 2750 return; /* none */
1da177e4 2751 }
1da177e4 2752
f5279ffd
MR
2753 sbmac_pldev = kcalloc(sbmac_max_units, sizeof(*sbmac_pldev),
2754 GFP_KERNEL);
2755 if (!sbmac_pldev) {
2756 printk(KERN_ERR "%s: unable to allocate memory\n",
2757 sbmac_string);
2758 return;
2759 }
2760
f90fdc3c
RB
2761 /*
2762 * Walk through the Ethernet controllers and find
2763 * those who have their MAC addresses set.
2764 */
f5279ffd
MR
2765 for (i = 0; i < sbmac_max_units; i++)
2766 if (sbmac_platform_probe_one(i))
2767 break;
2768}
1da177e4 2769
1da177e4 2770
f5279ffd
MR
2771static void __exit sbmac_platform_cleanup(void)
2772{
2773 int i;
1da177e4 2774
f5279ffd
MR
2775 for (i = 0; i < sbmac_max_units; i++)
2776 platform_device_unregister(sbmac_pldev[i]);
2777 kfree(sbmac_pldev);
2778}
1da177e4 2779
1da177e4 2780
f5279ffd
MR
2781static struct platform_driver sbmac_driver = {
2782 .probe = sbmac_probe,
2783 .remove = __exit_p(sbmac_remove),
2784 .driver = {
2785 .name = sbmac_string,
2786 },
2787};
1da177e4 2788
f5279ffd
MR
2789static int __init sbmac_init_module(void)
2790{
2791 int err;
1da177e4 2792
f5279ffd
MR
2793 err = platform_driver_register(&sbmac_driver);
2794 if (err)
2795 return err;
1da177e4 2796
f5279ffd 2797 sbmac_platform_probe();
1da177e4 2798
f5279ffd
MR
2799 return err;
2800}
1da177e4 2801
f5279ffd 2802static void __exit sbmac_cleanup_module(void)
1da177e4 2803{
f5279ffd
MR
2804 sbmac_platform_cleanup();
2805 platform_driver_unregister(&sbmac_driver);
1da177e4
LT
2806}
2807
2808module_init(sbmac_init_module);
2809module_exit(sbmac_cleanup_module);
This page took 0.797302 seconds and 5 git commands to generate.