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