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