blob: b6cafac972009cfb31f8b66bca06f83fbdea8095 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002 * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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.
Ralf Baechle74b02472005-10-19 15:40:02 +010013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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"
Ralf Baechlef90fdc32006-02-08 23:23:26 +000045#define SBMAC_ETH3_HWADDR "40:00:00:00:01:03"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#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
Ralf Baechlef90fdc32006-02-08 23:23:26 +000060#define MAX_UNITS 4 /* More are supported, limit only on options */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
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
Ralf Baechlef90fdc32006-02-08 23:23:26 +000088static int options[MAX_UNITS] = {-1, -1, -1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070089module_param_array(options, int, NULL, S_IRUGO);
90MODULE_PARM_DESC(options, "1-" __MODULE_STRING(MAX_UNITS));
91
Ralf Baechlef90fdc32006-02-08 23:23:26 +000092static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_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
Mark Mason693aa942007-04-26 00:23:22 -070098static int int_pktcnt_tx = 255;
99module_param(int_pktcnt_tx, int, S_IRUGO);
100MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Mark Mason693aa942007-04-26 00:23:22 -0700102static 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#endif
114
115#include <asm/sibyte/sb1250.h>
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000116#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
117#include <asm/sibyte/bcm1480_regs.h>
118#include <asm/sibyte/bcm1480_int.h>
Mark Mason693aa942007-04-26 00:23:22 -0700119#define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000120#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#include <asm/sibyte/sb1250_regs.h>
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000122#include <asm/sibyte/sb1250_int.h>
123#else
124#error invalid SiByte MAC configuation
125#endif
126#include <asm/sibyte/sb1250_scd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#include <asm/sibyte/sb1250_mac.h>
128#include <asm/sibyte/sb1250_dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000130#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/**********************************************************************
139 * Simple types
140 ********************************************************************* */
141
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143typedef 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
Ralf Baechle74b02472005-10-19 15:40:02 +0100152typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 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
Mark Mason693aa942007-04-26 00:23:22 -0700167#define SBMAC_MAX_TXDESCR 256
168#define SBMAC_MAX_RXDESCR 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170#define ETHER_ALIGN 2
171#define ETHER_ADDR_LEN 6
Ralf Baechle74b02472005-10-19 15:40:02 +0100172#define ENET_PACKET_SIZE 1518
173/*#define ENET_PACKET_SIZE 9216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
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 {
Ralf Baechle74b02472005-10-19 15:40:02 +0100191
192 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * This stuff is used to identify the channel and the registers
194 * associated with it.
195 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100196
Mark Mason693aa942007-04-26 00:23:22 -0700197 struct sbmac_softc *sbdma_eth; /* back pointer to associated MAC */
198 int sbdma_channel; /* channel number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int sbdma_txdir; /* direction (1=transmit) */
Mark Mason693aa942007-04-26 00:23:22 -0700200 int sbdma_maxdescr; /* total # of descriptors in ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#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
Ralf Baechle20399732005-10-19 15:39:05 +0100206 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 */
Mark Mason693aa942007-04-26 00:23:22 -0700209 volatile void __iomem *sbdma_dscrcnt; /* Descriptor count register */
Ralf Baechle20399732005-10-19 15:39:05 +0100210 volatile void __iomem *sbdma_curdscr; /* current descriptor address */
Mark Mason693aa942007-04-26 00:23:22 -0700211 volatile void __iomem *sbdma_oodpktlost;/* pkt drop (rx only) */
212
Ralf Baechle74b02472005-10-19 15:40:02 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /*
215 * This stuff is for maintenance of the ring
216 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100217
Mark Mason693aa942007-04-26 00:23:22 -0700218 sbdmadscr_t *sbdma_dscrtable_unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 sbdmadscr_t *sbdma_dscrtable; /* base of descriptor table */
220 sbdmadscr_t *sbdma_dscrtable_end; /* end of descriptor table */
Ralf Baechle74b02472005-10-19 15:40:02 +0100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct sk_buff **sbdma_ctxtable; /* context table, one per descr */
Ralf Baechle74b02472005-10-19 15:40:02 +0100223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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 {
Ralf Baechle74b02472005-10-19 15:40:02 +0100235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /*
237 * Linux-specific things
238 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct net_device *sbm_dev; /* pointer to linux device */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700241 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spinlock_t sbm_lock; /* spin lock */
243 struct timer_list sbm_timer; /* for monitoring MII */
Ralf Baechle74b02472005-10-19 15:40:02 +0100244 struct net_device_stats sbm_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int sbm_devflags; /* current device flags */
246
247 int sbm_phy_oldbmsr;
248 int sbm_phy_oldanlpar;
249 int sbm_phy_oldk1stsr;
250 int sbm_phy_oldlinkstat;
251 int sbm_buffersize;
Ralf Baechle74b02472005-10-19 15:40:02 +0100252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned char sbm_phys[2];
Ralf Baechle74b02472005-10-19 15:40:02 +0100254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /*
256 * Controller-specific things
257 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100258
Ralf Baechle8fb303c2007-03-24 14:26:13 +0000259 void __iomem *sbm_base; /* MAC's base address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 sbmac_state_t sbm_state; /* current state */
Ralf Baechle74b02472005-10-19 15:40:02 +0100261
Ralf Baechle20399732005-10-19 15:39:05 +0100262 volatile void __iomem *sbm_macenable; /* MAC Enable Register */
263 volatile void __iomem *sbm_maccfg; /* MAC Configuration Register */
264 volatile void __iomem *sbm_fifocfg; /* FIFO configuration register */
265 volatile void __iomem *sbm_framecfg; /* Frame configuration register */
266 volatile void __iomem *sbm_rxfilter; /* receive filter register */
267 volatile void __iomem *sbm_isr; /* Interrupt status register */
268 volatile void __iomem *sbm_imr; /* Interrupt mask register */
269 volatile void __iomem *sbm_mdio; /* MDIO register */
Ralf Baechle74b02472005-10-19 15:40:02 +0100270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 sbmac_speed_t sbm_speed; /* current speed */
272 sbmac_duplex_t sbm_duplex; /* current duplex */
273 sbmac_fc_t sbm_fc; /* current flow control setting */
Ralf Baechle74b02472005-10-19 15:40:02 +0100274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
Ralf Baechle74b02472005-10-19 15:40:02 +0100276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 sbmacdma_t sbm_txdma; /* for now, only use channel 0 */
278 sbmacdma_t sbm_rxdma;
279 int rx_hw_checksum;
280 int sbe_idx;
281};
282
283
284/**********************************************************************
285 * Externs
286 ********************************************************************* */
287
288/**********************************************************************
289 * Prototypes
290 ********************************************************************* */
291
292static void sbdma_initctx(sbmacdma_t *d,
293 struct sbmac_softc *s,
294 int chan,
295 int txrx,
296 int maxdescr);
297static void sbdma_channel_start(sbmacdma_t *d, int rxtx);
298static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *m);
299static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *m);
300static void sbdma_emptyring(sbmacdma_t *d);
301static void sbdma_fillring(sbmacdma_t *d);
Mark Mason693aa942007-04-26 00:23:22 -0700302static int sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d, int work_to_do, int poll);
303static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d, int poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int sbmac_initctx(struct sbmac_softc *s);
305static void sbmac_channel_start(struct sbmac_softc *s);
306static void sbmac_channel_stop(struct sbmac_softc *s);
307static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *,sbmac_state_t);
308static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff);
309static uint64_t sbmac_addr2reg(unsigned char *ptr);
David Howells7d12e782006-10-05 14:55:46 +0100310static irqreturn_t sbmac_intr(int irq,void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
312static void sbmac_setmulti(struct sbmac_softc *sc);
313static int sbmac_init(struct net_device *dev, int idx);
314static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed);
315static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc);
316
317static int sbmac_open(struct net_device *dev);
318static void sbmac_timer(unsigned long data);
319static void sbmac_tx_timeout (struct net_device *dev);
320static struct net_device_stats *sbmac_get_stats(struct net_device *dev);
321static void sbmac_set_rx_mode(struct net_device *dev);
322static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
323static int sbmac_close(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700324static int sbmac_poll(struct napi_struct *napi, int budget);
Mark Mason693aa942007-04-26 00:23:22 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int sbmac_mii_poll(struct sbmac_softc *s,int noisy);
Ralf Baechle59b81822005-10-20 12:01:28 +0100327static int sbmac_mii_probe(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329static void sbmac_mii_sync(struct sbmac_softc *s);
330static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt);
331static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx);
332static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
333 unsigned int regval);
334
335
336/**********************************************************************
337 * Globals
338 ********************************************************************* */
339
340static uint64_t sbmac_orig_hwaddr[MAX_UNITS];
341
342
343/**********************************************************************
344 * MDIO constants
345 ********************************************************************* */
346
347#define MII_COMMAND_START 0x01
348#define MII_COMMAND_READ 0x02
349#define MII_COMMAND_WRITE 0x01
350#define MII_COMMAND_ACK 0x02
351
352#define BMCR_RESET 0x8000
353#define BMCR_LOOPBACK 0x4000
354#define BMCR_SPEED0 0x2000
355#define BMCR_ANENABLE 0x1000
356#define BMCR_POWERDOWN 0x0800
357#define BMCR_ISOLATE 0x0400
358#define BMCR_RESTARTAN 0x0200
359#define BMCR_DUPLEX 0x0100
360#define BMCR_COLTEST 0x0080
361#define BMCR_SPEED1 0x0040
362#define BMCR_SPEED1000 BMCR_SPEED1
363#define BMCR_SPEED100 BMCR_SPEED0
364#define BMCR_SPEED10 0
365
366#define BMSR_100BT4 0x8000
367#define BMSR_100BT_FDX 0x4000
368#define BMSR_100BT_HDX 0x2000
369#define BMSR_10BT_FDX 0x1000
370#define BMSR_10BT_HDX 0x0800
371#define BMSR_100BT2_FDX 0x0400
372#define BMSR_100BT2_HDX 0x0200
373#define BMSR_1000BT_XSR 0x0100
374#define BMSR_PRESUP 0x0040
375#define BMSR_ANCOMPLT 0x0020
376#define BMSR_REMFAULT 0x0010
377#define BMSR_AUTONEG 0x0008
378#define BMSR_LINKSTAT 0x0004
379#define BMSR_JABDETECT 0x0002
380#define BMSR_EXTCAPAB 0x0001
381
382#define PHYIDR1 0x2000
383#define PHYIDR2 0x5C60
384
385#define ANAR_NP 0x8000
386#define ANAR_RF 0x2000
387#define ANAR_ASYPAUSE 0x0800
388#define ANAR_PAUSE 0x0400
389#define ANAR_T4 0x0200
390#define ANAR_TXFD 0x0100
391#define ANAR_TXHD 0x0080
392#define ANAR_10FD 0x0040
393#define ANAR_10HD 0x0020
394#define ANAR_PSB 0x0001
395
396#define ANLPAR_NP 0x8000
397#define ANLPAR_ACK 0x4000
398#define ANLPAR_RF 0x2000
399#define ANLPAR_ASYPAUSE 0x0800
400#define ANLPAR_PAUSE 0x0400
401#define ANLPAR_T4 0x0200
402#define ANLPAR_TXFD 0x0100
403#define ANLPAR_TXHD 0x0080
404#define ANLPAR_10FD 0x0040
405#define ANLPAR_10HD 0x0020
406#define ANLPAR_PSB 0x0001 /* 802.3 */
407
408#define ANER_PDF 0x0010
409#define ANER_LPNPABLE 0x0008
410#define ANER_NPABLE 0x0004
411#define ANER_PAGERX 0x0002
412#define ANER_LPANABLE 0x0001
413
414#define ANNPTR_NP 0x8000
415#define ANNPTR_MP 0x2000
416#define ANNPTR_ACK2 0x1000
417#define ANNPTR_TOGTX 0x0800
418#define ANNPTR_CODE 0x0008
419
420#define ANNPRR_NP 0x8000
421#define ANNPRR_MP 0x2000
422#define ANNPRR_ACK3 0x1000
423#define ANNPRR_TOGTX 0x0800
424#define ANNPRR_CODE 0x0008
425
426#define K1TCR_TESTMODE 0x0000
427#define K1TCR_MSMCE 0x1000
428#define K1TCR_MSCV 0x0800
429#define K1TCR_RPTR 0x0400
430#define K1TCR_1000BT_FDX 0x200
431#define K1TCR_1000BT_HDX 0x100
432
433#define K1STSR_MSMCFLT 0x8000
434#define K1STSR_MSCFGRES 0x4000
435#define K1STSR_LRSTAT 0x2000
436#define K1STSR_RRSTAT 0x1000
437#define K1STSR_LP1KFD 0x0800
438#define K1STSR_LP1KHD 0x0400
439#define K1STSR_LPASMDIR 0x0200
440
441#define K1SCR_1KX_FDX 0x8000
442#define K1SCR_1KX_HDX 0x4000
443#define K1SCR_1KT_FDX 0x2000
444#define K1SCR_1KT_HDX 0x1000
445
446#define STRAP_PHY1 0x0800
447#define STRAP_NCMODE 0x0400
448#define STRAP_MANMSCFG 0x0200
449#define STRAP_ANENABLE 0x0100
450#define STRAP_MSVAL 0x0080
451#define STRAP_1KHDXADV 0x0010
452#define STRAP_1KFDXADV 0x0008
453#define STRAP_100ADV 0x0004
454#define STRAP_SPEEDSEL 0x0000
455#define STRAP_SPEED100 0x0001
456
457#define PHYSUP_SPEED1000 0x10
458#define PHYSUP_SPEED100 0x08
459#define PHYSUP_SPEED10 0x00
460#define PHYSUP_LINKUP 0x04
461#define PHYSUP_FDX 0x02
462
463#define MII_BMCR 0x00 /* Basic mode control register (rw) */
464#define MII_BMSR 0x01 /* Basic mode status register (ro) */
Ralf Baechle59b81822005-10-20 12:01:28 +0100465#define MII_PHYIDR1 0x02
466#define MII_PHYIDR2 0x03
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468#define MII_K1STSR 0x0A /* 1K Status Register (ro) */
469#define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
470
471
472#define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
473
474#define ENABLE 1
475#define DISABLE 0
476
477/**********************************************************************
478 * SBMAC_MII_SYNC(s)
Ralf Baechle74b02472005-10-19 15:40:02 +0100479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Synchronize with the MII - send a pattern of bits to the MII
481 * that will guarantee that it is ready to accept a command.
Ralf Baechle74b02472005-10-19 15:40:02 +0100482 *
483 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +0100485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * Return value:
487 * nothing
488 ********************************************************************* */
489
490static void sbmac_mii_sync(struct sbmac_softc *s)
491{
492 int cnt;
493 uint64_t bits;
494 int mac_mdio_genc;
495
Ralf Baechle20399732005-10-19 15:39:05 +0100496 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
Ralf Baechle74b02472005-10-19 15:40:02 +0100499
Ralf Baechle20399732005-10-19 15:39:05 +0100500 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 for (cnt = 0; cnt < 32; cnt++) {
Ralf Baechle20399732005-10-19 15:39:05 +0100503 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
504 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506}
507
508/**********************************************************************
509 * SBMAC_MII_SENDDATA(s,data,bitcnt)
Ralf Baechle74b02472005-10-19 15:40:02 +0100510 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * Send some bits to the MII. The bits to be sent are right-
512 * justified in the 'data' parameter.
Ralf Baechle74b02472005-10-19 15:40:02 +0100513 *
514 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * s - sbmac structure
516 * data - data to send
517 * bitcnt - number of bits to send
518 ********************************************************************* */
519
520static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt)
521{
522 int i;
523 uint64_t bits;
524 unsigned int curmask;
525 int mac_mdio_genc;
526
Ralf Baechle20399732005-10-19 15:39:05 +0100527 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 bits = M_MAC_MDIO_DIR_OUTPUT;
Ralf Baechle20399732005-10-19 15:39:05 +0100530 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 curmask = 1 << (bitcnt - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 for (i = 0; i < bitcnt; i++) {
535 if (data & curmask)
536 bits |= M_MAC_MDIO_OUT;
537 else bits &= ~M_MAC_MDIO_OUT;
Ralf Baechle20399732005-10-19 15:39:05 +0100538 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
539 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
540 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 curmask >>= 1;
542 }
543}
544
545
546
547/**********************************************************************
548 * SBMAC_MII_READ(s,phyaddr,regidx)
Ralf Baechle74b02472005-10-19 15:40:02 +0100549 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * Read a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100551 *
552 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * s - sbmac structure
554 * phyaddr - PHY's address
555 * regidx = index of register to read
Ralf Baechle74b02472005-10-19 15:40:02 +0100556 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Return value:
558 * value read, or 0 if an error occurred.
559 ********************************************************************* */
560
561static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
562{
563 int idx;
564 int error;
565 int regval;
566 int mac_mdio_genc;
567
568 /*
569 * Synchronize ourselves so that the PHY knows the next
570 * thing coming down is a command
571 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 sbmac_mii_sync(s);
Ralf Baechle74b02472005-10-19 15:40:02 +0100574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * Send the data to the PHY. The sequence is
577 * a "start" command (2 bits)
578 * a "read" command (2 bits)
579 * the PHY addr (5 bits)
580 * the register index (5 bits)
581 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sbmac_mii_senddata(s,MII_COMMAND_START, 2);
584 sbmac_mii_senddata(s,MII_COMMAND_READ, 2);
585 sbmac_mii_senddata(s,phyaddr, 5);
586 sbmac_mii_senddata(s,regidx, 5);
Ralf Baechle74b02472005-10-19 15:40:02 +0100587
Ralf Baechle20399732005-10-19 15:39:05 +0100588 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100589
590 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * Switch the port around without a clock transition.
592 */
Ralf Baechle20399732005-10-19 15:39:05 +0100593 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
596 * Send out a clock pulse to signal we want the status
597 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100598
Ralf Baechle20399732005-10-19 15:39:05 +0100599 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
600 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100601
602 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * If an error occurred, the PHY will signal '1' back
604 */
Ralf Baechle20399732005-10-19 15:39:05 +0100605 error = __raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN;
Ralf Baechle74b02472005-10-19 15:40:02 +0100606
607 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Issue an 'idle' clock pulse, but keep the direction
609 * the same.
610 */
Ralf Baechle20399732005-10-19 15:39:05 +0100611 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
612 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 regval = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 for (idx = 0; idx < 16; idx++) {
617 regval <<= 1;
Ralf Baechle74b02472005-10-19 15:40:02 +0100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (error == 0) {
Ralf Baechle20399732005-10-19 15:39:05 +0100620 if (__raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 regval |= 1;
622 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100623
Ralf Baechle20399732005-10-19 15:39:05 +0100624 __raw_writeq(M_MAC_MDIO_DIR_INPUT|M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
625 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* Switch back to output */
Ralf Baechle20399732005-10-19 15:39:05 +0100629 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (error == 0)
632 return regval;
633 return 0;
634}
635
636
637/**********************************************************************
638 * SBMAC_MII_WRITE(s,phyaddr,regidx,regval)
Ralf Baechle74b02472005-10-19 15:40:02 +0100639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Write a value to a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100641 *
642 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * s - sbmac structure
644 * phyaddr - PHY to use
645 * regidx - register within the PHY
646 * regval - data to write to register
Ralf Baechle74b02472005-10-19 15:40:02 +0100647 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * Return value:
649 * nothing
650 ********************************************************************* */
651
652static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
653 unsigned int regval)
654{
655 int mac_mdio_genc;
656
657 sbmac_mii_sync(s);
Ralf Baechle74b02472005-10-19 15:40:02 +0100658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 sbmac_mii_senddata(s,MII_COMMAND_START,2);
660 sbmac_mii_senddata(s,MII_COMMAND_WRITE,2);
661 sbmac_mii_senddata(s,phyaddr, 5);
662 sbmac_mii_senddata(s,regidx, 5);
663 sbmac_mii_senddata(s,MII_COMMAND_ACK,2);
664 sbmac_mii_senddata(s,regval,16);
665
Ralf Baechle20399732005-10-19 15:39:05 +0100666 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Ralf Baechle20399732005-10-19 15:39:05 +0100668 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671
672
673/**********************************************************************
674 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
Ralf Baechle74b02472005-10-19 15:40:02 +0100675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * Initialize a DMA channel context. Since there are potentially
677 * eight DMA channels per MAC, it's nice to do this in a standard
Ralf Baechle74b02472005-10-19 15:40:02 +0100678 * way.
679 *
680 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * d - sbmacdma_t structure (DMA channel context)
682 * s - sbmac_softc structure (pointer to a MAC)
683 * chan - channel number (0..1 right now)
684 * txrx - Identifies DMA_TX or DMA_RX for channel direction
685 * maxdescr - number of descriptors
Ralf Baechle74b02472005-10-19 15:40:02 +0100686 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * Return value:
688 * nothing
689 ********************************************************************* */
690
691static void sbdma_initctx(sbmacdma_t *d,
692 struct sbmac_softc *s,
693 int chan,
694 int txrx,
695 int maxdescr)
696{
Mark Mason693aa942007-04-26 00:23:22 -0700697#ifdef CONFIG_SBMAC_COALESCE
698 int int_pktcnt, int_timeout;
699#endif
700
Ralf Baechle74b02472005-10-19 15:40:02 +0100701 /*
702 * Save away interesting stuff in the structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 d->sbdma_eth = s;
706 d->sbdma_channel = chan;
707 d->sbdma_txdir = txrx;
Ralf Baechle74b02472005-10-19 15:40:02 +0100708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#if 0
710 /* RMON clearing */
711 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
712#endif
713
Ralf Baechle20399732005-10-19 15:39:05 +0100714 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BYTES)));
715 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_COLLISIONS)));
716 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_LATE_COL)));
717 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_EX_COL)));
718 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_FCS_ERROR)));
719 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_ABORT)));
720 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BAD)));
721 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_GOOD)));
722 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_RUNT)));
723 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_OVERSIZE)));
724 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BYTES)));
725 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_MCAST)));
726 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BCAST)));
727 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BAD)));
728 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_GOOD)));
729 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_RUNT)));
730 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_OVERSIZE)));
731 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_FCS_ERROR)));
732 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_LENGTH_ERROR)));
733 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_CODE_ERROR)));
734 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_ALIGN_ERROR)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Ralf Baechle74b02472005-10-19 15:40:02 +0100736 /*
737 * initialize register pointers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100739
740 d->sbdma_config0 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100742 d->sbdma_config1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100744 d->sbdma_dscrbase =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
Ralf Baechle74b02472005-10-19 15:40:02 +0100746 d->sbdma_dscrcnt =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
Ralf Baechle74b02472005-10-19 15:40:02 +0100748 d->sbdma_curdscr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
Mark Mason693aa942007-04-26 00:23:22 -0700750 if (d->sbdma_txdir)
751 d->sbdma_oodpktlost = NULL;
752 else
753 d->sbdma_oodpktlost =
754 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
Ralf Baechle74b02472005-10-19 15:40:02 +0100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * Allocate memory for the ring
758 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 d->sbdma_maxdescr = maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100761
Mark Mason693aa942007-04-26 00:23:22 -0700762 d->sbdma_dscrtable_unaligned =
Ralf Baechle74b02472005-10-19 15:40:02 +0100763 d->sbdma_dscrtable = (sbdmadscr_t *)
Ralf Baechle04115de2005-10-10 14:50:36 +0100764 kmalloc((d->sbdma_maxdescr+1)*sizeof(sbdmadscr_t), GFP_KERNEL);
765
766 /*
767 * The descriptor table must be aligned to at least 16 bytes or the
768 * MAC will corrupt it.
769 */
770 d->sbdma_dscrtable = (sbdmadscr_t *)
771 ALIGN((unsigned long)d->sbdma_dscrtable, sizeof(sbdmadscr_t));
Ralf Baechle74b02472005-10-19 15:40:02 +0100772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 memset(d->sbdma_dscrtable,0,d->sbdma_maxdescr*sizeof(sbdmadscr_t));
Ralf Baechle74b02472005-10-19 15:40:02 +0100774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
Ralf Baechle74b02472005-10-19 15:40:02 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /*
780 * And context table
781 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100782
Mariusz Kozlowskic477f332007-07-31 23:58:36 +0200783 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
784 sizeof(struct sk_buff *), GFP_KERNEL);
Ralf Baechle74b02472005-10-19 15:40:02 +0100785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef CONFIG_SBMAC_COALESCE
787 /*
788 * Setup Rx/Tx DMA coalescing defaults
789 */
790
Mark Mason693aa942007-04-26 00:23:22 -0700791 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if ( int_pktcnt ) {
793 d->sbdma_int_pktcnt = int_pktcnt;
794 } else {
795 d->sbdma_int_pktcnt = 1;
796 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100797
Mark Mason693aa942007-04-26 00:23:22 -0700798 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if ( int_timeout ) {
800 d->sbdma_int_timeout = int_timeout;
801 } else {
802 d->sbdma_int_timeout = 0;
803 }
804#endif
805
806}
807
808/**********************************************************************
809 * SBDMA_CHANNEL_START(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100812 *
813 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * d - DMA channel to init (context must be previously init'd
815 * rxtx - DMA_RX or DMA_TX depending on what type of channel
Ralf Baechle74b02472005-10-19 15:40:02 +0100816 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 * Return value:
818 * nothing
819 ********************************************************************* */
820
821static void sbdma_channel_start(sbmacdma_t *d, int rxtx )
822{
823 /*
824 * Turn on the DMA channel
825 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +0100828 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
829 0, d->sbdma_config1);
830 __raw_writeq(M_DMA_EOP_INT_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 V_DMA_RINGSZ(d->sbdma_maxdescr) |
832 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
Ralf Baechle20399732005-10-19 15:39:05 +0100833 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#else
Ralf Baechle20399732005-10-19 15:39:05 +0100835 __raw_writeq(0, d->sbdma_config1);
836 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
837 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838#endif
839
Ralf Baechle20399732005-10-19 15:39:05 +0100840 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /*
843 * Initialize ring pointers
844 */
845
846 d->sbdma_addptr = d->sbdma_dscrtable;
847 d->sbdma_remptr = d->sbdma_dscrtable;
848}
849
850/**********************************************************************
851 * SBDMA_CHANNEL_STOP(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100852 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100854 *
855 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * d - DMA channel to init (context must be previously init'd
Ralf Baechle74b02472005-10-19 15:40:02 +0100857 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * Return value:
859 * nothing
860 ********************************************************************* */
861
862static void sbdma_channel_stop(sbmacdma_t *d)
863{
864 /*
865 * Turn off the DMA channel
866 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100867
Ralf Baechle20399732005-10-19 15:39:05 +0100868 __raw_writeq(0, d->sbdma_config1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100869
Ralf Baechle20399732005-10-19 15:39:05 +0100870 __raw_writeq(0, d->sbdma_dscrbase);
Ralf Baechle74b02472005-10-19 15:40:02 +0100871
Ralf Baechle20399732005-10-19 15:39:05 +0100872 __raw_writeq(0, d->sbdma_config0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
875 * Zero ring pointers
876 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100877
Ralf Baechle20399732005-10-19 15:39:05 +0100878 d->sbdma_addptr = NULL;
879 d->sbdma_remptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
883{
884 unsigned long addr;
885 unsigned long newaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 addr = (unsigned long) skb->data;
Ralf Baechle74b02472005-10-19 15:40:02 +0100888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 newaddr = (addr + power2 - 1) & ~(power2 - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 skb_reserve(skb,newaddr-addr+offset);
892}
893
894
895/**********************************************************************
896 * SBDMA_ADD_RCVBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +0100897 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 * Add a buffer to the specified DMA channel. For receive channels,
899 * this queues a buffer for inbound packets.
Ralf Baechle74b02472005-10-19 15:40:02 +0100900 *
901 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * d - DMA channel descriptor
903 * sb - sk_buff to add, or NULL if we should allocate one
Ralf Baechle74b02472005-10-19 15:40:02 +0100904 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * Return value:
906 * 0 if buffer could not be added (ring is full)
907 * 1 if buffer added successfully
908 ********************************************************************* */
909
910
911static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
912{
913 sbdmadscr_t *dsc;
914 sbdmadscr_t *nextdsc;
915 struct sk_buff *sb_new = NULL;
916 int pktsize = ENET_PACKET_SIZE;
Ralf Baechle74b02472005-10-19 15:40:02 +0100917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +0100919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 dsc = d->sbdma_addptr;
921 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +0100922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /*
924 * figure out if the ring is full - if the next descriptor
925 * is the same as the one that we're going to remove from
926 * the ring, the ring is full
927 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (nextdsc == d->sbdma_remptr) {
930 return -ENOSPC;
931 }
932
Ralf Baechle74b02472005-10-19 15:40:02 +0100933 /*
934 * Allocate a sk_buff if we don't already have one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * If we do have an sk_buff, reset it so that it's empty.
936 *
937 * Note: sk_buffs don't seem to be guaranteed to have any sort
938 * of alignment when they are allocated. Therefore, allocate enough
939 * extra space to make sure that:
940 *
941 * 1. the data does not start in the middle of a cache line.
942 * 2. The data does not end in the middle of a cache line
Ralf Baechle74b02472005-10-19 15:40:02 +0100943 * 3. The buffer can be aligned such that the IP addresses are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * naturally aligned.
945 *
946 * Remember, the SOCs MAC writes whole cache lines at a time,
947 * without reading the old contents first. So, if the sk_buff's
948 * data portion starts in the middle of a cache line, the SOC
949 * DMA will trash the beginning (and ending) portions.
950 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (sb == NULL) {
953 sb_new = dev_alloc_skb(ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN);
954 if (sb_new == NULL) {
955 printk(KERN_INFO "%s: sk_buff allocation failed\n",
956 d->sbdma_eth->sbm_dev->name);
957 return -ENOBUFS;
958 }
959
960 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, ETHER_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 else {
963 sb_new = sb;
Ralf Baechle74b02472005-10-19 15:40:02 +0100964 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 * nothing special to reinit buffer, it's already aligned
966 * and sb->data already points to a good place.
967 */
968 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100971 * fill in the descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974#ifdef CONFIG_SBMAC_COALESCE
975 /*
976 * Do not interrupt per DMA transfer.
977 */
David S. Miller689be432005-06-28 15:25:31 -0700978 dsc->dscr_a = virt_to_phys(sb_new->data) |
Ralf Baechle20399732005-10-19 15:39:05 +0100979 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) | 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#else
David S. Miller689be432005-06-28 15:25:31 -0700981 dsc->dscr_a = virt_to_phys(sb_new->data) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
983 M_DMA_DSCRA_INTERRUPT;
984#endif
985
986 /* receiving: no options */
987 dsc->dscr_b = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100990 * fill in the context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
Ralf Baechle74b02472005-10-19 15:40:02 +0100994
995 /*
996 * point at next packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 d->sbdma_addptr = nextdsc;
Ralf Baechle74b02472005-10-19 15:40:02 +01001000
1001 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * Give the buffer to the DMA engine.
1003 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001004
Ralf Baechle20399732005-10-19 15:39:05 +01001005 __raw_writeq(1, d->sbdma_dscrcnt);
Ralf Baechle74b02472005-10-19 15:40:02 +01001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return 0; /* we did it */
1008}
1009
1010/**********************************************************************
1011 * SBDMA_ADD_TXBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +01001012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * Add a transmit buffer to the specified DMA channel, causing a
1014 * transmit to start.
Ralf Baechle74b02472005-10-19 15:40:02 +01001015 *
1016 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * d - DMA channel descriptor
1018 * sb - sk_buff to add
Ralf Baechle74b02472005-10-19 15:40:02 +01001019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * Return value:
1021 * 0 transmit queued successfully
1022 * otherwise error code
1023 ********************************************************************* */
1024
1025
1026static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *sb)
1027{
1028 sbdmadscr_t *dsc;
1029 sbdmadscr_t *nextdsc;
1030 uint64_t phys;
1031 uint64_t ncb;
1032 int length;
Ralf Baechle74b02472005-10-19 15:40:02 +01001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +01001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 dsc = d->sbdma_addptr;
1037 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /*
1040 * figure out if the ring is full - if the next descriptor
1041 * is the same as the one that we're going to remove from
1042 * the ring, the ring is full
1043 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (nextdsc == d->sbdma_remptr) {
1046 return -ENOSPC;
1047 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * Under Linux, it's not necessary to copy/coalesce buffers
1051 * like it is on NetBSD. We think they're all contiguous,
1052 * but that may not be true for GBE.
1053 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 length = sb->len;
Ralf Baechle74b02472005-10-19 15:40:02 +01001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /*
1058 * fill in the descriptor. Note that the number of cache
1059 * blocks in the descriptor is the number of blocks
1060 * *spanned*, so we need to add in the offset (if any)
1061 * while doing the calculation.
1062 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 phys = virt_to_phys(sb->data);
1065 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
1066
Ralf Baechle74b02472005-10-19 15:40:02 +01001067 dsc->dscr_a = phys |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 V_DMA_DSCRA_A_SIZE(ncb) |
1069#ifndef CONFIG_SBMAC_COALESCE
1070 M_DMA_DSCRA_INTERRUPT |
1071#endif
1072 M_DMA_ETHTX_SOP;
Ralf Baechle74b02472005-10-19 15:40:02 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* transmitting: set outbound options and length */
1075
1076 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
1077 V_DMA_DSCRB_PKT_SIZE(length);
Ralf Baechle74b02472005-10-19 15:40:02 +01001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001080 * fill in the context
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
Ralf Baechle74b02472005-10-19 15:40:02 +01001084
1085 /*
1086 * point at next packet
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 d->sbdma_addptr = nextdsc;
Ralf Baechle74b02472005-10-19 15:40:02 +01001090
1091 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 * Give the buffer to the DMA engine.
1093 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001094
Ralf Baechle20399732005-10-19 15:39:05 +01001095 __raw_writeq(1, d->sbdma_dscrcnt);
Ralf Baechle74b02472005-10-19 15:40:02 +01001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return 0; /* we did it */
1098}
1099
1100
1101
1102
1103/**********************************************************************
1104 * SBDMA_EMPTYRING(d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001105 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 * Free all allocated sk_buffs on the specified DMA channel;
Ralf Baechle74b02472005-10-19 15:40:02 +01001107 *
1108 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001110 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * Return value:
1112 * nothing
1113 ********************************************************************* */
1114
1115static void sbdma_emptyring(sbmacdma_t *d)
1116{
1117 int idx;
1118 struct sk_buff *sb;
Ralf Baechle74b02472005-10-19 15:40:02 +01001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
1121 sb = d->sbdma_ctxtable[idx];
1122 if (sb) {
1123 dev_kfree_skb(sb);
1124 d->sbdma_ctxtable[idx] = NULL;
1125 }
1126 }
1127}
1128
1129
1130/**********************************************************************
1131 * SBDMA_FILLRING(d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * Fill the specified DMA channel (must be receive channel)
1134 * with sk_buffs
Ralf Baechle74b02472005-10-19 15:40:02 +01001135 *
1136 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001138 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * Return value:
1140 * nothing
1141 ********************************************************************* */
1142
1143static void sbdma_fillring(sbmacdma_t *d)
1144{
1145 int idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
1148 if (sbdma_add_rcvbuffer(d,NULL) != 0)
1149 break;
1150 }
1151}
1152
Deepak Saxenad6830012007-03-19 15:43:11 -07001153#ifdef CONFIG_NET_POLL_CONTROLLER
1154static void sbmac_netpoll(struct net_device *netdev)
1155{
1156 struct sbmac_softc *sc = netdev_priv(netdev);
1157 int irq = sc->sbm_dev->irq;
1158
1159 __raw_writeq(0, sc->sbm_imr);
1160
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -07001161 sbmac_intr(irq, netdev);
Deepak Saxenad6830012007-03-19 15:43:11 -07001162
1163#ifdef CONFIG_SBMAC_COALESCE
1164 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1165 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1166 sc->sbm_imr);
1167#else
1168 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1169 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1170#endif
1171}
1172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174/**********************************************************************
Mark Mason693aa942007-04-26 00:23:22 -07001175 * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001177 * Process "completed" receive buffers on the specified DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +01001178 *
1179 * Input parameters:
Mark Mason693aa942007-04-26 00:23:22 -07001180 * sc - softc structure
1181 * d - DMA channel context
1182 * work_to_do - no. of packets to process before enabling interrupt
1183 * again (for NAPI)
1184 * poll - 1: using polling (for NAPI)
Ralf Baechle74b02472005-10-19 15:40:02 +01001185 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * Return value:
1187 * nothing
1188 ********************************************************************* */
1189
Mark Mason693aa942007-04-26 00:23:22 -07001190static int sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d,
1191 int work_to_do, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 int curidx;
1194 int hwidx;
1195 sbdmadscr_t *dsc;
1196 struct sk_buff *sb;
1197 int len;
Mark Mason693aa942007-04-26 00:23:22 -07001198 int work_done = 0;
1199 int dropped = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001200
Mark Mason693aa942007-04-26 00:23:22 -07001201 prefetch(d);
1202
1203again:
1204 /* Check if the HW dropped any frames */
1205 sc->sbm_stats.rx_fifo_errors
1206 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1207 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1208
1209 while (work_to_do-- > 0) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001210 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * figure out where we are (as an index) and where
1212 * the hardware is (also as an index)
1213 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001214 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 * descriptor table was page-aligned and contiguous in
1216 * both virtual and physical memory -- you could then
1217 * just compare the low-order bits of the virtual address
1218 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1219 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001220
Mark Mason693aa942007-04-26 00:23:22 -07001221 dsc = d->sbdma_remptr;
1222 curidx = dsc - d->sbdma_dscrtable;
1223
1224 prefetch(dsc);
1225 prefetch(&d->sbdma_ctxtable[curidx]);
1226
Ralf Baechle20399732005-10-19 15:39:05 +01001227 hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
Ralf Baechle74b02472005-10-19 15:40:02 +01001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /*
1231 * If they're the same, that means we've processed all
1232 * of the descriptors up to (but not including) the one that
1233 * the hardware is working on right now.
1234 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (curidx == hwidx)
Mark Mason693aa942007-04-26 00:23:22 -07001237 goto done;
Ralf Baechle74b02472005-10-19 15:40:02 +01001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * Otherwise, get the packet's sk_buff ptr back
1241 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sb = d->sbdma_ctxtable[curidx];
1244 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
Ralf Baechle74b02472005-10-19 15:40:02 +01001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Check packet status. If good, process it.
1250 * If not, silently drop it and put it back on the
1251 * receive ring.
1252 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001253
Mark Mason693aa942007-04-26 00:23:22 -07001254 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * Add a new buffer to replace the old one. If we fail
1258 * to allocate a buffer, we're going to drop this
1259 * packet and put it right back on the receive ring.
1260 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001261
Mark Mason693aa942007-04-26 00:23:22 -07001262 if (unlikely (sbdma_add_rcvbuffer(d,NULL) ==
1263 -ENOBUFS)) {
1264 sc->sbm_stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
Mark Mason693aa942007-04-26 00:23:22 -07001266 /* No point in continuing at the moment */
1267 printk(KERN_ERR "dropped packet (1)\n");
1268 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1269 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 } else {
1271 /*
1272 * Set length into the packet
1273 */
1274 skb_put(sb,len);
Ralf Baechle74b02472005-10-19 15:40:02 +01001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Buffer has been replaced on the
1278 * receive ring. Pass the buffer to
1279 * the kernel
1280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1282 /* Check hw IPv4/TCP checksum if supported */
1283 if (sc->rx_hw_checksum == ENABLE) {
1284 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1285 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1286 sb->ip_summed = CHECKSUM_UNNECESSARY;
1287 /* don't need to set sb->csum */
1288 } else {
1289 sb->ip_summed = CHECKSUM_NONE;
1290 }
1291 }
Mark Mason693aa942007-04-26 00:23:22 -07001292 prefetch(sb->data);
1293 prefetch((const void *)(((char *)sb->data)+32));
1294 if (poll)
1295 dropped = netif_receive_skb(sb);
1296 else
1297 dropped = netif_rx(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001298
Mark Mason693aa942007-04-26 00:23:22 -07001299 if (dropped == NET_RX_DROP) {
1300 sc->sbm_stats.rx_dropped++;
1301 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1302 goto done;
1303 }
1304 else {
1305 sc->sbm_stats.rx_bytes += len;
1306 sc->sbm_stats.rx_packets++;
1307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 } else {
1310 /*
1311 * Packet was mangled somehow. Just drop it and
1312 * put it back on the receive ring.
1313 */
1314 sc->sbm_stats.rx_errors++;
1315 sbdma_add_rcvbuffer(d,sb);
1316 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001317
1318
1319 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * .. and advance to the next buffer.
1321 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Mark Mason693aa942007-04-26 00:23:22 -07001324 work_done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Mark Mason693aa942007-04-26 00:23:22 -07001326 if (!poll) {
1327 work_to_do = 32;
1328 goto again; /* collect fifo drop statistics again */
1329 }
1330done:
1331 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/**********************************************************************
1335 * SBDMA_TX_PROCESS(sc,d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001336 *
1337 * Process "completed" transmit buffers on the specified DMA channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * This is normally called within the interrupt service routine.
1339 * Note that this isn't really ideal for priority channels, since
Ralf Baechle74b02472005-10-19 15:40:02 +01001340 * it processes all of the packets on a given channel before
1341 * returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001343 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * sc - softc structure
Mark Mason693aa942007-04-26 00:23:22 -07001345 * d - DMA channel context
1346 * poll - 1: using polling (for NAPI)
Ralf Baechle74b02472005-10-19 15:40:02 +01001347 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * Return value:
1349 * nothing
1350 ********************************************************************* */
1351
Mark Mason693aa942007-04-26 00:23:22 -07001352static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 int curidx;
1355 int hwidx;
1356 sbdmadscr_t *dsc;
1357 struct sk_buff *sb;
1358 unsigned long flags;
Mark Mason693aa942007-04-26 00:23:22 -07001359 int packets_handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 spin_lock_irqsave(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001362
Mark Mason693aa942007-04-26 00:23:22 -07001363 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 for (;;) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001370 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * figure out where we are (as an index) and where
1372 * the hardware is (also as an index)
1373 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001374 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * 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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (curidx == hwidx)
1390 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /*
1393 * Otherwise, get the packet's sk_buff ptr back
1394 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 dsc = &(d->sbdma_dscrtable[curidx]);
1397 sb = d->sbdma_ctxtable[curidx];
1398 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
1401 * Stats
1402 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 sc->sbm_stats.tx_bytes += sb->len;
1405 sc->sbm_stats.tx_packets++;
Ralf Baechle74b02472005-10-19 15:40:02 +01001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 /*
1408 * for transmits, we just free buffers.
1409 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 dev_kfree_skb_irq(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001412
1413 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * .. and advance to the next buffer.
1415 */
1416
1417 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001418
Mark Mason693aa942007-04-26 00:23:22 -07001419 packets_handled++;
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /*
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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001428
Mark Mason693aa942007-04-26 00:23:22 -07001429 if (packets_handled)
1430 netif_wake_queue(d->sbdma_eth->sbm_dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01001431
Mark Mason693aa942007-04-26 00:23:22 -07001432end_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437
1438
1439/**********************************************************************
1440 * SBMAC_INITCTX(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001441 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * 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
Ralf Baechle74b02472005-10-19 15:40:02 +01001446 *
1447 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * s - sbmac context structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001449 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 * Return value:
1451 * 0
1452 ********************************************************************* */
1453
1454static int sbmac_initctx(struct sbmac_softc *s)
1455{
Ralf Baechle74b02472005-10-19 15:40:02 +01001456
1457 /*
1458 * figure out the addresses of some ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 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);
Ralf Baechle74b02472005-10-19 15:40:02 +01001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * initial state is OFF
1488 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /*
1493 * Initial speed is (XXX TEMP) 10MBit/s HDX no FC
1494 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 s->sbm_speed = sbmac_speed_10;
1497 s->sbm_duplex = sbmac_duplex_half;
1498 s->sbm_fc = sbmac_fc_disabled;
Ralf Baechle74b02472005-10-19 15:40:02 +01001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501}
1502
1503
1504static void sbdma_uninitctx(struct sbmacdma_s *d)
1505{
Mark Mason693aa942007-04-26 00:23:22 -07001506 if (d->sbdma_dscrtable_unaligned) {
1507 kfree(d->sbdma_dscrtable_unaligned);
1508 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 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)
Ralf Baechle74b02472005-10-19 15:40:02 +01001527 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 * Start packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001529 *
1530 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001532 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * Return value:
1534 * nothing
1535 ********************************************************************* */
1536
1537static void sbmac_channel_start(struct sbmac_softc *s)
1538{
1539 uint64_t reg;
Ralf Baechle20399732005-10-19 15:39:05 +01001540 volatile void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 uint64_t cfg,fifo,framecfg;
1542 int idx, th_value;
Ralf Baechle74b02472005-10-19 15:40:02 +01001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /*
1545 * Don't do this if running
1546 */
1547
1548 if (s->sbm_state == sbmac_state_on)
1549 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Bring the controller out of reset, but leave it off.
1553 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001554
Ralf Baechle20399732005-10-19 15:39:05 +01001555 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /*
1558 * Ignore all received packets
1559 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001560
Ralf Baechle20399732005-10-19 15:39:05 +01001561 __raw_writeq(0, s->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001562
1563 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * Calculate values for various control registers.
1565 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 cfg = M_MAC_RETRY_EN |
Ralf Baechle74b02472005-10-19 15:40:02 +01001568 M_MAC_TX_HOLD_SOP_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 V_MAC_TX_PAUSE_CNT_16K |
1570 M_MAC_AP_STAT_EN |
1571 M_MAC_FAST_SYNC |
1572 M_MAC_SS_EN |
1573 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001574
1575 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 * 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 */
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001580 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 th_value = 28;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001582 else
1583 th_value = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
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 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001600 * Clear out the hash address map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 port = s->sbm_base + R_MAC_HASH_BASE;
1604 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001605 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 port += sizeof(uint64_t);
1607 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /*
1610 * Clear out the exact-match table
1611 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 port = s->sbm_base + R_MAC_ADDR_BASE;
1614 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001615 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 port += sizeof(uint64_t);
1617 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /*
1620 * Clear out the DMA Channel mapping table registers
1621 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 port = s->sbm_base + R_MAC_CHUP0_BASE;
1624 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001625 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 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++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001632 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 port += sizeof(uint64_t);
1634 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /*
1637 * Program the hardware address. It goes into the hardware-address
1638 * register as well as the first filter register.
1639 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 reg = sbmac_addr2reg(s->sbm_hwaddr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 port = s->sbm_base + R_MAC_ADDR_BASE;
Ralf Baechle20399732005-10-19 15:39:05 +01001644 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 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 */
Ralf Baechle20399732005-10-19 15:39:05 +01001653 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654#else
Ralf Baechle20399732005-10-19 15:39:05 +01001655 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /*
1659 * Set the receive filter for no packets, and write values
1660 * to the various config registers
1661 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001662
Ralf Baechle20399732005-10-19 15:39:05 +01001663 __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);
Ralf Baechle74b02472005-10-19 15:40:02 +01001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /*
1670 * Initialize DMA channels (rings should be ok now)
1671 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1674 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
Ralf Baechle74b02472005-10-19 15:40:02 +01001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
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);
Ralf Baechle74b02472005-10-19 15:40:02 +01001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Fill the receive ring
1685 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 sbdma_fillring(&(s->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001688
1689 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * Turn on the rest of the bits in the enable register
Ralf Baechle74b02472005-10-19 15:40:02 +01001691 */
1692
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001693#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)
Ralf Baechle20399732005-10-19 15:39:05 +01001697 __raw_writeq(M_MAC_RXDMA_EN0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 M_MAC_TXDMA_EN0 |
1699 M_MAC_RX_ENABLE |
Ralf Baechle20399732005-10-19 15:39:05 +01001700 M_MAC_TX_ENABLE, s->sbm_macenable);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001701#else
1702#error invalid SiByte MAC configuation
1703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +01001706 __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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708#else
Ralf Baechle20399732005-10-19 15:39:05 +01001709 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1710 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001714 * Enable receiving unicasts and broadcasts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001716
1717 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1718
1719 /*
1720 * we're running now.
1721 */
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 s->sbm_state = sbmac_state_on;
Ralf Baechle74b02472005-10-19 15:40:02 +01001724
1725 /*
1726 * Program multicast addresses
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 sbmac_setmulti(s);
Ralf Baechle74b02472005-10-19 15:40:02 +01001730
1731 /*
1732 * If channel was in promiscuous mode before, turn that on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (s->sbm_devflags & IFF_PROMISC) {
1736 sbmac_promiscuous_mode(s,1);
1737 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741
1742/**********************************************************************
1743 * SBMAC_CHANNEL_STOP(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * Stop packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001746 *
1747 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001749 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * Return value:
1751 * nothing
1752 ********************************************************************* */
1753
1754static void sbmac_channel_stop(struct sbmac_softc *s)
1755{
1756 /* don't do this if already stopped */
Ralf Baechle74b02472005-10-19 15:40:02 +01001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (s->sbm_state == sbmac_state_off)
1759 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* don't accept any packets, disable all interrupts */
Ralf Baechle74b02472005-10-19 15:40:02 +01001762
Ralf Baechle20399732005-10-19 15:39:05 +01001763 __raw_writeq(0, s->sbm_rxfilter);
1764 __raw_writeq(0, s->sbm_imr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* Turn off ticker */
Ralf Baechle74b02472005-10-19 15:40:02 +01001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* XXX */
Ralf Baechle74b02472005-10-19 15:40:02 +01001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* turn off receiver and transmitter */
Ralf Baechle74b02472005-10-19 15:40:02 +01001771
Ralf Baechle20399732005-10-19 15:39:05 +01001772 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* We're stopped now. */
Ralf Baechle74b02472005-10-19 15:40:02 +01001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /*
1779 * Stop DMA channels (rings should be ok now)
1780 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 sbdma_channel_stop(&(s->sbm_rxdma));
1783 sbdma_channel_stop(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Empty the receive and transmit rings */
Ralf Baechle74b02472005-10-19 15:40:02 +01001786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 sbdma_emptyring(&(s->sbm_rxdma));
1788 sbdma_emptyring(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
1792/**********************************************************************
1793 * SBMAC_SET_CHANNEL_STATE(state)
Ralf Baechle74b02472005-10-19 15:40:02 +01001794 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 * Set the channel's state ON or OFF
Ralf Baechle74b02472005-10-19 15:40:02 +01001796 *
1797 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * state - new state
Ralf Baechle74b02472005-10-19 15:40:02 +01001799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
1809 * If same as previous state, return
1810 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (state == oldstate) {
1813 return oldstate;
1814 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001817 * If new state is ON, turn channel on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (state == sbmac_state_on) {
1821 sbmac_channel_start(sc);
1822 }
1823 else {
1824 sbmac_channel_stop(sc);
1825 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /*
1828 * Return previous state
1829 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return oldstate;
1832}
1833
1834
1835/**********************************************************************
1836 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001837 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 * Turn on or off promiscuous mode
Ralf Baechle74b02472005-10-19 15:40:02 +01001839 *
1840 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 * sc - softc
1842 * onoff - 1 to turn on, 0 to turn off
Ralf Baechle74b02472005-10-19 15:40:02 +01001843 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 * Return value:
1845 * nothing
1846 ********************************************************************* */
1847
1848static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1849{
1850 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (sc->sbm_state != sbmac_state_on)
1853 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (onoff) {
Ralf Baechle20399732005-10-19 15:39:05 +01001856 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 reg |= M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001858 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 else {
Ralf Baechle20399732005-10-19 15:39:05 +01001861 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 reg &= ~M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001863 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865}
1866
1867/**********************************************************************
1868 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 * Set the iphdr offset as 15 assuming ethernet encapsulation
Ralf Baechle74b02472005-10-19 15:40:02 +01001871 *
1872 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01001874 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * Return value:
1876 * nothing
1877 ********************************************************************* */
1878
1879static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1880{
1881 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /* Hard code the off set to 15 for now */
Ralf Baechle20399732005-10-19 15:39:05 +01001884 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
Ralf Baechle20399732005-10-19 15:39:05 +01001886 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001887
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001888 /* BCM1250 pass1 didn't have hardware checksum. Everything
1889 later does. */
1890 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 sc->rx_hw_checksum = DISABLE;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001892 } else {
1893 sc->rx_hw_checksum = ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895}
1896
1897
1898/**********************************************************************
1899 * SBMAC_ADDR2REG(ptr)
Ralf Baechle74b02472005-10-19 15:40:02 +01001900 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * Convert six bytes into the 64-bit register value that
1902 * we typically write into the SBMAC's address/mcast registers
Ralf Baechle74b02472005-10-19 15:40:02 +01001903 *
1904 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * ptr - pointer to 6 bytes
Ralf Baechle74b02472005-10-19 15:40:02 +01001906 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 * Return value:
1908 * register value
1909 ********************************************************************* */
1910
1911static uint64_t sbmac_addr2reg(unsigned char *ptr)
1912{
1913 uint64_t reg = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ptr += 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01001916
1917 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001919 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001921 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001923 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001925 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001927 reg |= (uint64_t) *(--ptr);
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return reg;
1930}
1931
1932
1933/**********************************************************************
1934 * SBMAC_SET_SPEED(s,speed)
Ralf Baechle74b02472005-10-19 15:40:02 +01001935 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * Configure LAN speed for the specified MAC.
1937 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001938 *
1939 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * s - sbmac structure
1941 * speed - speed to set MAC to (see sbmac_speed_t enum)
Ralf Baechle74b02472005-10-19 15:40:02 +01001942 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * 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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 s->sbm_speed = speed;
Ralf Baechle74b02472005-10-19 15:40:02 +01001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (s->sbm_state == sbmac_state_on)
1960 return 0; /* save for next restart */
1961
1962 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001963 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001965
Ralf Baechle20399732005-10-19 15:39:05 +01001966 cfg = __raw_readq(s->sbm_maccfg);
1967 framecfg = __raw_readq(s->sbm_framecfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
1970 * Mask out the stuff we want to change
1971 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 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);
Ralf Baechle74b02472005-10-19 15:40:02 +01001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /*
1978 * Now add in the new bits
1979 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 case sbmac_speed_auto: /* XXX not implemented */
2007 /* fall through */
2008 default:
2009 return 0;
2010 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002013 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002015
Ralf Baechle20399732005-10-19 15:39:05 +01002016 __raw_writeq(framecfg, s->sbm_framecfg);
2017 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 1;
2020}
2021
2022/**********************************************************************
2023 * SBMAC_SET_DUPLEX(s,duplex,fc)
Ralf Baechle74b02472005-10-19 15:40:02 +01002024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 * Set Ethernet duplex and flow control options for this MAC
2026 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01002027 *
2028 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 * s - sbmac structure
2030 * duplex - duplex setting (see sbmac_duplex_t)
2031 * fc - flow control setting (see sbmac_fc_t)
Ralf Baechle74b02472005-10-19 15:40:02 +01002032 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 * 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Save new current values
2044 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 s->sbm_duplex = duplex;
2047 s->sbm_fc = fc;
Ralf Baechle74b02472005-10-19 15:40:02 +01002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (s->sbm_state == sbmac_state_on)
2050 return 0; /* save for next restart */
Ralf Baechle74b02472005-10-19 15:40:02 +01002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002053 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002055
Ralf Baechle20399732005-10-19 15:39:05 +01002056 cfg = __raw_readq(s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /*
2059 * Mask off the stuff we're about to change
2060 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
Ralf Baechle74b02472005-10-19 15:40:02 +01002063
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 case sbmac_fc_collision:
2073 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
2074 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 case sbmac_fc_carrier:
2077 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
2078 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 case sbmac_fc_auto: /* XXX not implemented */
Ralf Baechle74b02472005-10-19 15:40:02 +01002081 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 case sbmac_fc_frame: /* not valid in half duplex */
2083 default: /* invalid selection */
2084 return 0;
2085 }
2086 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 case sbmac_duplex_full:
2089 switch (fc) {
2090 case sbmac_fc_disabled:
2091 cfg |= V_MAC_FC_CMD_DISABLED;
2092 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 case sbmac_fc_frame:
2095 cfg |= V_MAC_FC_CMD_ENABLED;
2096 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002101 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 default:
2103 return 0;
2104 }
2105 break;
2106 case sbmac_duplex_auto:
2107 /* XXX not implemented */
2108 break;
2109 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002112 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002114
Ralf Baechle20399732005-10-19 15:39:05 +01002115 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return 1;
2118}
2119
2120
2121
2122
2123/**********************************************************************
2124 * SBMAC_INTR()
Ralf Baechle74b02472005-10-19 15:40:02 +01002125 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 * Interrupt handler for MAC interrupts
Ralf Baechle74b02472005-10-19 15:40:02 +01002127 *
2128 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 * MAC structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002130 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 * Return value:
2132 * nothing
2133 ********************************************************************* */
David Howells7d12e782006-10-05 14:55:46 +01002134static irqreturn_t sbmac_intr(int irq,void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
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
Mark Mason693aa942007-04-26 00:23:22 -07002141 /*
2142 * Read the ISR (this clears the bits in the real
2143 * register, except for counter addr)
2144 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002145
Mark Mason693aa942007-04-26 00:23:22 -07002146 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
Ralf Baechle74b02472005-10-19 15:40:02 +01002147
Mark Mason693aa942007-04-26 00:23:22 -07002148 if (isr == 0)
2149 return IRQ_RETVAL(0);
2150 handled = 1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002151
Mark Mason693aa942007-04-26 00:23:22 -07002152 /*
2153 * Transmits on channel 0
2154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002156 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
Mark Mason693aa942007-04-26 00:23:22 -07002157 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
Ralf Baechle74b02472005-10-19 15:40:02 +01002158
Mark Mason693aa942007-04-26 00:23:22 -07002159 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002160 if (netif_rx_schedule_prep(dev, &sc->napi)) {
Mark Mason693aa942007-04-26 00:23:22 -07002161 __raw_writeq(0, sc->sbm_imr);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002162 __netif_rx_schedule(dev, &sc->napi);
Mark Mason693aa942007-04-26 00:23:22 -07002163 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170 }
2171 return IRQ_RETVAL(handled);
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/**********************************************************************
2175 * SBMAC_START_TX(skb,dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002176 *
2177 * Start output on the specified interface. Basically, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 * 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.
Ralf Baechle74b02472005-10-19 15:40:02 +01002180 *
2181 * Input parameters:
2182 *
2183 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 * 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);
Ralf Baechle74b02472005-10-19 15:40:02 +01002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* lock eth irq */
2192 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002195 * Put the buffer on the transmit ring. If we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 * don't have room, stop the queue.
2197 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 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 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 dev->trans_start = jiffies;
Ralf Baechle74b02472005-10-19 15:40:02 +01002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 spin_unlock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return 0;
2212}
2213
2214/**********************************************************************
2215 * SBMAC_SETMULTI(sc)
Ralf Baechle74b02472005-10-19 15:40:02 +01002216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * Reprogram the multicast table into the hardware, given
2218 * the list of multicasts associated with the interface
2219 * structure.
Ralf Baechle74b02472005-10-19 15:40:02 +01002220 *
2221 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01002223 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 * Return value:
2225 * nothing
2226 ********************************************************************* */
2227
2228static void sbmac_setmulti(struct sbmac_softc *sc)
2229{
2230 uint64_t reg;
Ralf Baechle20399732005-10-19 15:39:05 +01002231 volatile void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 int idx;
2233 struct dev_mc_list *mclist;
2234 struct net_device *dev = sc->sbm_dev;
Ralf Baechle74b02472005-10-19 15:40:02 +01002235
2236 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 * Clear out entire multicast table. We do this by nuking
2238 * the entire hash table and all the direct matches except
Ralf Baechle74b02472005-10-19 15:40:02 +01002239 * the first one, which is used for our station address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2243 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002244 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2248 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002249 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * Clear the filter to say we don't want any multicasts.
2254 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002255
Ralf Baechle20399732005-10-19 15:39:05 +01002256 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002258 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (dev->flags & IFF_ALLMULTI) {
Ralf Baechle74b02472005-10-19 15:40:02 +01002261 /*
2262 * Enable ALL multicasts. Do this by inverting the
2263 * multicast enable bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 */
Ralf Baechle20399732005-10-19 15:39:05 +01002265 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002267 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return;
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Ralf Baechle74b02472005-10-19 15:40:02 +01002271
2272 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 * 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 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* XXX only using perfect filter for now, need to use hash
2279 * XXX if the table overflows */
Ralf Baechle74b02472005-10-19 15:40:02 +01002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 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));
Ralf Baechle20399732005-10-19 15:39:05 +01002286 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 idx++;
2288 mclist = mclist->next;
2289 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002290
2291 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * Enable the "accept multicast bits" if we programmed at least one
Ralf Baechle74b02472005-10-19 15:40:02 +01002293 * multicast.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (idx > 1) {
Ralf Baechle20399732005-10-19 15:39:05 +01002297 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 reg |= M_MAC_MCAST_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01002299 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301}
2302
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002303#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304/**********************************************************************
2305 * SBMAC_PARSE_XDIGIT(str)
Ralf Baechle74b02472005-10-19 15:40:02 +01002306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * Parse a hex digit, returning its value
Ralf Baechle74b02472005-10-19 15:40:02 +01002308 *
2309 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 * str - character
Ralf Baechle74b02472005-10-19 15:40:02 +01002311 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 * Return value:
2313 * hex value, or -1 if invalid
2314 ********************************************************************* */
2315
2316static int sbmac_parse_xdigit(char str)
2317{
2318 int digit;
Ralf Baechle74b02472005-10-19 15:40:02 +01002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return digit;
2330}
2331
2332/**********************************************************************
2333 * SBMAC_PARSE_HWADDR(str,hwaddr)
Ralf Baechle74b02472005-10-19 15:40:02 +01002334 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
2336 * Ethernet address.
Ralf Baechle74b02472005-10-19 15:40:02 +01002337 *
2338 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 * str - string
2340 * hwaddr - pointer to hardware address
Ralf Baechle74b02472005-10-19 15:40:02 +01002341 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 * 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 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 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 *hwaddr++ = (digit1 << 4) | digit2;
2371 idx--;
Ralf Baechle74b02472005-10-19 15:40:02 +01002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 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)
Ralf Baechle74b02472005-10-19 15:40:02 +01002393 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 * Attach routine - init hardware and hook ourselves into linux
Ralf Baechle74b02472005-10-19 15:40:02 +01002395 *
2396 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * dev - net_device structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002398 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 * 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;
Ralf Baechle74b02472005-10-19 15:40:02 +01002410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /* Determine controller base address */
Ralf Baechle74b02472005-10-19 15:40:02 +01002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 sc->sbm_base = IOADDR(dev->base_addr);
2416 sc->sbm_dev = dev;
2417 sc->sbe_idx = idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 eaddr = sc->sbm_hwaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +01002420
2421 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * Read the ethernet address. The firwmare left this programmed
2423 * for us in the ethernet address register for each mac.
2424 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002425
Ralf Baechle20399732005-10-19 15:39:05 +01002426 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2427 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 for (i = 0; i < 6; i++) {
2429 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2430 ea_reg >>= 8;
2431 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 for (i = 0; i < 6; i++) {
2434 dev->dev_addr[i] = eaddr[i];
2435 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002436
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002439 * Init packet size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;
2443
Ralf Baechle74b02472005-10-19 15:40:02 +01002444 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 * Initialize context (get pointers to registers and stuff), then
2446 * allocate the memory for the descriptor tables.
2447 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 sbmac_initctx(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*
2452 * Set up Linux device callins
2453 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 spin_lock_init(&(sc->sbm_lock));
Ralf Baechle74b02472005-10-19 15:40:02 +01002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 dev->open = sbmac_open;
2458 dev->hard_start_xmit = sbmac_start_tx;
2459 dev->stop = sbmac_close;
2460 dev->get_stats = sbmac_get_stats;
2461 dev->set_multicast_list = sbmac_set_rx_mode;
2462 dev->do_ioctl = sbmac_mii_ioctl;
2463 dev->tx_timeout = sbmac_tx_timeout;
2464 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002465
2466 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 dev->change_mtu = sb1250_change_mtu;
Deepak Saxenad6830012007-03-19 15:43:11 -07002469#ifdef CONFIG_NET_POLL_CONTROLLER
2470 dev->poll_controller = sbmac_netpoll;
2471#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 /* This is needed for PASS2 for Rx H/W checksum feature */
2474 sbmac_set_iphdr_offset(sc);
2475
2476 err = register_netdev(dev);
2477 if (err)
2478 goto out_uninit;
2479
Ralf Baechlef567ef92005-10-10 14:50:24 +01002480 if (sc->rx_hw_checksum == ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 printk(KERN_INFO "%s: enabling TCP rcv checksum\n",
2482 sc->sbm_dev->name);
2483 }
2484
2485 /*
2486 * Display Ethernet address (this is called during the config
2487 * process so we need to finish off the config message that
2488 * was being displayed)
2489 */
2490 printk(KERN_INFO
Ralf Baechle74b02472005-10-19 15:40:02 +01002491 "%s: SiByte Ethernet at 0x%08lX, address: %02X:%02X:%02X:%02X:%02X:%02X\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 dev->name, dev->base_addr,
2493 eaddr[0],eaddr[1],eaddr[2],eaddr[3],eaddr[4],eaddr[5]);
Ralf Baechle74b02472005-10-19 15:40:02 +01002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 return 0;
2497
2498out_uninit:
2499 sbmac_uninitctx(sc);
2500
2501 return err;
2502}
2503
2504
2505static int sbmac_open(struct net_device *dev)
2506{
2507 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (debug > 1) {
2510 printk(KERN_DEBUG "%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2511 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002512
2513 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 * map/route interrupt (clear status first, in case something
2515 * weird is pending; we haven't initialized the mac registers
2516 * yet)
2517 */
2518
Ralf Baechle20399732005-10-19 15:39:05 +01002519 __raw_readq(sc->sbm_isr);
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07002520 if (request_irq(dev->irq, &sbmac_intr, IRQF_SHARED, dev->name, dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return -EBUSY;
2522
2523 /*
Ralf Baechle59b81822005-10-20 12:01:28 +01002524 * Probe phy address
2525 */
2526
2527 if(sbmac_mii_probe(dev) == -1) {
2528 printk("%s: failed to probe PHY.\n", dev->name);
2529 return -EINVAL;
2530 }
2531
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002532 napi_enable(&sc->napi);
2533
Ralf Baechle59b81822005-10-20 12:01:28 +01002534 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002535 * Configure default speed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 */
2537
2538 sbmac_mii_poll(sc,noisy_mii);
Ralf Baechle74b02472005-10-19 15:40:02 +01002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Turn on the channel
2542 */
2543
2544 sbmac_set_channel_state(sc,sbmac_state_on);
Ralf Baechle74b02472005-10-19 15:40:02 +01002545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /*
2547 * XXX Station address is in dev->dev_addr
2548 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (dev->if_port == 0)
Ralf Baechle74b02472005-10-19 15:40:02 +01002551 dev->if_port = 0;
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 netif_start_queue(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 sbmac_set_rx_mode(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /* Set the timer to check for link beat. */
2558 init_timer(&sc->sbm_timer);
2559 sc->sbm_timer.expires = jiffies + 2 * HZ/100;
2560 sc->sbm_timer.data = (unsigned long)dev;
2561 sc->sbm_timer.function = &sbmac_timer;
2562 add_timer(&sc->sbm_timer);
Ralf Baechle74b02472005-10-19 15:40:02 +01002563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 return 0;
2565}
2566
Ralf Baechle59b81822005-10-20 12:01:28 +01002567static int sbmac_mii_probe(struct net_device *dev)
2568{
2569 int i;
2570 struct sbmac_softc *s = netdev_priv(dev);
2571 u16 bmsr, id1, id2;
2572 u32 vendor, device;
2573
2574 for (i=1; i<31; i++) {
2575 bmsr = sbmac_mii_read(s, i, MII_BMSR);
2576 if (bmsr != 0) {
2577 s->sbm_phys[0] = i;
2578 id1 = sbmac_mii_read(s, i, MII_PHYIDR1);
2579 id2 = sbmac_mii_read(s, i, MII_PHYIDR2);
2580 vendor = ((u32)id1 << 6) | ((id2 >> 10) & 0x3f);
2581 device = (id2 >> 4) & 0x3f;
2582
2583 printk(KERN_INFO "%s: found phy %d, vendor %06x part %02x\n",
2584 dev->name, i, vendor, device);
2585 return i;
2586 }
2587 }
2588 return -1;
2589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591
2592static int sbmac_mii_poll(struct sbmac_softc *s,int noisy)
2593{
2594 int bmsr,bmcr,k1stsr,anlpar;
2595 int chg;
2596 char buffer[100];
2597 char *p = buffer;
2598
2599 /* Read the mode status and mode control registers. */
2600 bmsr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMSR);
2601 bmcr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMCR);
2602
2603 /* get the link partner status */
2604 anlpar = sbmac_mii_read(s,s->sbm_phys[0],MII_ANLPAR);
2605
2606 /* if supported, read the 1000baseT register */
2607 if (bmsr & BMSR_1000BT_XSR) {
2608 k1stsr = sbmac_mii_read(s,s->sbm_phys[0],MII_K1STSR);
2609 }
2610 else {
2611 k1stsr = 0;
2612 }
2613
2614 chg = 0;
2615
2616 if ((bmsr & BMSR_LINKSTAT) == 0) {
2617 /*
2618 * If link status is down, clear out old info so that when
2619 * it comes back up it will force us to reconfigure speed
2620 */
2621 s->sbm_phy_oldbmsr = 0;
2622 s->sbm_phy_oldanlpar = 0;
2623 s->sbm_phy_oldk1stsr = 0;
2624 return 0;
2625 }
2626
2627 if ((s->sbm_phy_oldbmsr != bmsr) ||
2628 (s->sbm_phy_oldanlpar != anlpar) ||
2629 (s->sbm_phy_oldk1stsr != k1stsr)) {
2630 if (debug > 1) {
2631 printk(KERN_DEBUG "%s: bmsr:%x/%x anlpar:%x/%x k1stsr:%x/%x\n",
2632 s->sbm_dev->name,
2633 s->sbm_phy_oldbmsr,bmsr,
2634 s->sbm_phy_oldanlpar,anlpar,
2635 s->sbm_phy_oldk1stsr,k1stsr);
2636 }
2637 s->sbm_phy_oldbmsr = bmsr;
2638 s->sbm_phy_oldanlpar = anlpar;
2639 s->sbm_phy_oldk1stsr = k1stsr;
2640 chg = 1;
2641 }
2642
2643 if (chg == 0)
2644 return 0;
2645
2646 p += sprintf(p,"Link speed: ");
2647
2648 if (k1stsr & K1STSR_LP1KFD) {
2649 s->sbm_speed = sbmac_speed_1000;
2650 s->sbm_duplex = sbmac_duplex_full;
2651 s->sbm_fc = sbmac_fc_frame;
2652 p += sprintf(p,"1000BaseT FDX");
2653 }
2654 else if (k1stsr & K1STSR_LP1KHD) {
2655 s->sbm_speed = sbmac_speed_1000;
2656 s->sbm_duplex = sbmac_duplex_half;
2657 s->sbm_fc = sbmac_fc_disabled;
2658 p += sprintf(p,"1000BaseT HDX");
2659 }
2660 else if (anlpar & ANLPAR_TXFD) {
2661 s->sbm_speed = sbmac_speed_100;
2662 s->sbm_duplex = sbmac_duplex_full;
2663 s->sbm_fc = (anlpar & ANLPAR_PAUSE) ? sbmac_fc_frame : sbmac_fc_disabled;
2664 p += sprintf(p,"100BaseT FDX");
2665 }
2666 else if (anlpar & ANLPAR_TXHD) {
2667 s->sbm_speed = sbmac_speed_100;
2668 s->sbm_duplex = sbmac_duplex_half;
2669 s->sbm_fc = sbmac_fc_disabled;
2670 p += sprintf(p,"100BaseT HDX");
2671 }
2672 else if (anlpar & ANLPAR_10FD) {
2673 s->sbm_speed = sbmac_speed_10;
2674 s->sbm_duplex = sbmac_duplex_full;
2675 s->sbm_fc = sbmac_fc_frame;
2676 p += sprintf(p,"10BaseT FDX");
2677 }
2678 else if (anlpar & ANLPAR_10HD) {
2679 s->sbm_speed = sbmac_speed_10;
2680 s->sbm_duplex = sbmac_duplex_half;
2681 s->sbm_fc = sbmac_fc_collision;
2682 p += sprintf(p,"10BaseT HDX");
2683 }
2684 else {
2685 p += sprintf(p,"Unknown");
2686 }
2687
2688 if (noisy) {
2689 printk(KERN_INFO "%s: %s\n",s->sbm_dev->name,buffer);
2690 }
2691
2692 return 1;
2693}
2694
2695
2696static void sbmac_timer(unsigned long data)
2697{
2698 struct net_device *dev = (struct net_device *)data;
2699 struct sbmac_softc *sc = netdev_priv(dev);
2700 int next_tick = HZ;
2701 int mii_status;
2702
2703 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* make IFF_RUNNING follow the MII status bit "Link established" */
2706 mii_status = sbmac_mii_read(sc, sc->sbm_phys[0], MII_BMSR);
Ralf Baechle74b02472005-10-19 15:40:02 +01002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if ( (mii_status & BMSR_LINKSTAT) != (sc->sbm_phy_oldlinkstat) ) {
2709 sc->sbm_phy_oldlinkstat = mii_status & BMSR_LINKSTAT;
2710 if (mii_status & BMSR_LINKSTAT) {
2711 netif_carrier_on(dev);
2712 }
2713 else {
Ralf Baechle74b02472005-10-19 15:40:02 +01002714 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
2716 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /*
2719 * Poll the PHY to see what speed we should be running at
2720 */
2721
2722 if (sbmac_mii_poll(sc,noisy_mii)) {
2723 if (sc->sbm_state != sbmac_state_off) {
2724 /*
2725 * something changed, restart the channel
2726 */
2727 if (debug > 1) {
2728 printk("%s: restarting channel because speed changed\n",
2729 sc->sbm_dev->name);
2730 }
2731 sbmac_channel_stop(sc);
2732 sbmac_channel_start(sc);
2733 }
2734 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 spin_unlock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 sc->sbm_timer.expires = jiffies + next_tick;
2739 add_timer(&sc->sbm_timer);
2740}
2741
2742
2743static void sbmac_tx_timeout (struct net_device *dev)
2744{
2745 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002748
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 dev->trans_start = jiffies;
2751 sc->sbm_stats.tx_errors++;
Ralf Baechle74b02472005-10-19 15:40:02 +01002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 spin_unlock_irq (&sc->sbm_lock);
2754
2755 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2756}
2757
2758
2759
2760
2761static struct net_device_stats *sbmac_get_stats(struct net_device *dev)
2762{
2763 struct sbmac_softc *sc = netdev_priv(dev);
2764 unsigned long flags;
Ralf Baechle74b02472005-10-19 15:40:02 +01002765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 spin_lock_irqsave(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /* XXX update other stats here */
Ralf Baechle74b02472005-10-19 15:40:02 +01002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return &sc->sbm_stats;
2773}
2774
2775
2776
2777static void sbmac_set_rx_mode(struct net_device *dev)
2778{
2779 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct sbmac_softc *sc = netdev_priv(dev);
2781
2782 spin_lock_irqsave(&sc->sbm_lock, flags);
2783 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2784 /*
2785 * Promiscuous changed.
2786 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002787
2788 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 sbmac_promiscuous_mode(sc,1);
2790 }
2791 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 sbmac_promiscuous_mode(sc,0);
2793 }
2794 }
2795 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 /*
2798 * Program the multicasts. Do this every time.
2799 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 sbmac_setmulti(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
2805static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2806{
2807 struct sbmac_softc *sc = netdev_priv(dev);
2808 u16 *data = (u16 *)&rq->ifr_ifru;
2809 unsigned long flags;
2810 int retval;
Ralf Baechle74b02472005-10-19 15:40:02 +01002811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 spin_lock_irqsave(&sc->sbm_lock, flags);
2813 retval = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 switch(cmd) {
2816 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
2817 data[0] = sc->sbm_phys[0] & 0x1f;
2818 /* Fall Through */
2819 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
2820 data[3] = sbmac_mii_read(sc, data[0] & 0x1f, data[1] & 0x1f);
2821 break;
2822 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
2823 if (!capable(CAP_NET_ADMIN)) {
2824 retval = -EPERM;
2825 break;
2826 }
2827 if (debug > 1) {
2828 printk(KERN_DEBUG "%s: sbmac_mii_ioctl: write %02X %02X %02X\n",dev->name,
2829 data[0],data[1],data[2]);
2830 }
2831 sbmac_mii_write(sc, data[0] & 0x1f, data[1] & 0x1f, data[2]);
2832 break;
2833 default:
2834 retval = -EOPNOTSUPP;
2835 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2838 return retval;
2839}
2840
2841static int sbmac_close(struct net_device *dev)
2842{
2843 struct sbmac_softc *sc = netdev_priv(dev);
2844 unsigned long flags;
2845 int irq;
2846
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002847 napi_disable(&sc->napi);
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 sbmac_set_channel_state(sc,sbmac_state_off);
2850
2851 del_timer_sync(&sc->sbm_timer);
2852
2853 spin_lock_irqsave(&sc->sbm_lock, flags);
2854
2855 netif_stop_queue(dev);
2856
2857 if (debug > 1) {
2858 printk(KERN_DEBUG "%s: Shutting down ethercard\n",dev->name);
2859 }
2860
2861 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2862
2863 irq = dev->irq;
2864 synchronize_irq(irq);
2865 free_irq(irq, dev);
2866
2867 sbdma_emptyring(&(sc->sbm_txdma));
2868 sbdma_emptyring(&(sc->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01002869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return 0;
2871}
2872
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002873static int sbmac_poll(struct napi_struct *napi, int budget)
Mark Mason693aa942007-04-26 00:23:22 -07002874{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002875 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2876 struct net_device *dev = sc->sbm_dev;
Mark Mason693aa942007-04-26 00:23:22 -07002877 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002879 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
Mark Mason693aa942007-04-26 00:23:22 -07002880 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2881
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002882 if (work_done < budget) {
2883 netif_rx_complete(dev, napi);
Mark Mason693aa942007-04-26 00:23:22 -07002884
2885#ifdef CONFIG_SBMAC_COALESCE
2886 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2887 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2888 sc->sbm_imr);
2889#else
2890 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2891 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2892#endif
2893 }
2894
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002895 return work_done;
Mark Mason693aa942007-04-26 00:23:22 -07002896}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002898#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899static void
2900sbmac_setup_hwaddr(int chan,char *addr)
2901{
2902 uint8_t eaddr[6];
2903 uint64_t val;
Ralf Baechle20399732005-10-19 15:39:05 +01002904 unsigned long port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 port = A_MAC_CHANNEL_BASE(chan);
2907 sbmac_parse_hwaddr(addr,eaddr);
2908 val = sbmac_addr2reg(eaddr);
Ralf Baechle20399732005-10-19 15:39:05 +01002909 __raw_writeq(val, IOADDR(port+R_MAC_ETHERNET_ADDR));
2910 val = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912#endif
2913
2914static struct net_device *dev_sbmac[MAX_UNITS];
2915
2916static int __init
2917sbmac_init_module(void)
2918{
2919 int idx;
2920 struct net_device *dev;
Ralf Baechle20399732005-10-19 15:39:05 +01002921 unsigned long port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 int chip_max_units;
Ralf Baechle74b02472005-10-19 15:40:02 +01002923
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002924 /* Set the number of available units based on the SOC type. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 switch (soc_type) {
2926 case K_SYS_SOC_TYPE_BCM1250:
2927 case K_SYS_SOC_TYPE_BCM1250_ALT:
2928 chip_max_units = 3;
2929 break;
2930 case K_SYS_SOC_TYPE_BCM1120:
2931 case K_SYS_SOC_TYPE_BCM1125:
2932 case K_SYS_SOC_TYPE_BCM1125H:
2933 case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
2934 chip_max_units = 2;
2935 break;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002936 case K_SYS_SOC_TYPE_BCM1x55:
2937 case K_SYS_SOC_TYPE_BCM1x80:
2938 chip_max_units = 4;
2939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 default:
2941 chip_max_units = 0;
2942 break;
2943 }
2944 if (chip_max_units > MAX_UNITS)
2945 chip_max_units = MAX_UNITS;
2946
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002947 /*
2948 * For bringup when not using the firmware, we can pre-fill
2949 * the MAC addresses using the environment variables
2950 * specified in this file (or maybe from the config file?)
2951 */
2952#ifdef SBMAC_ETH0_HWADDR
2953 if (chip_max_units > 0)
2954 sbmac_setup_hwaddr(0,SBMAC_ETH0_HWADDR);
2955#endif
2956#ifdef SBMAC_ETH1_HWADDR
2957 if (chip_max_units > 1)
2958 sbmac_setup_hwaddr(1,SBMAC_ETH1_HWADDR);
2959#endif
2960#ifdef SBMAC_ETH2_HWADDR
2961 if (chip_max_units > 2)
2962 sbmac_setup_hwaddr(2,SBMAC_ETH2_HWADDR);
2963#endif
2964#ifdef SBMAC_ETH3_HWADDR
2965 if (chip_max_units > 3)
2966 sbmac_setup_hwaddr(3,SBMAC_ETH3_HWADDR);
2967#endif
2968
2969 /*
2970 * Walk through the Ethernet controllers and find
2971 * those who have their MAC addresses set.
2972 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 for (idx = 0; idx < chip_max_units; idx++) {
2974
2975 /*
2976 * This is the base address of the MAC.
2977 */
2978
2979 port = A_MAC_CHANNEL_BASE(idx);
2980
Ralf Baechle74b02472005-10-19 15:40:02 +01002981 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
Mark Mason693aa942007-04-26 00:23:22 -07002983 * value for us by the firmware if we are going to use this MAC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 * If we find a zero, skip this MAC.
2985 */
2986
Ralf Baechle20399732005-10-19 15:39:05 +01002987 sbmac_orig_hwaddr[idx] = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (sbmac_orig_hwaddr[idx] == 0) {
2989 printk(KERN_DEBUG "sbmac: not configuring MAC at "
2990 "%lx\n", port);
2991 continue;
2992 }
2993
2994 /*
2995 * Okay, cool. Initialize this MAC.
2996 */
2997
2998 dev = alloc_etherdev(sizeof(struct sbmac_softc));
Ralf Baechle74b02472005-10-19 15:40:02 +01002999 if (!dev)
Dave Jones089fff22006-10-18 00:30:27 -04003000 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 printk(KERN_DEBUG "sbmac: configuring MAC at %lx\n", port);
3003
Ralf Baechlef90fdc32006-02-08 23:23:26 +00003004 dev->irq = UNIT_INT(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 dev->base_addr = port;
3006 dev->mem_end = 0;
3007 if (sbmac_init(dev, idx)) {
3008 port = A_MAC_CHANNEL_BASE(idx);
Ralf Baechle20399732005-10-19 15:39:05 +01003009 __raw_writeq(sbmac_orig_hwaddr[idx], IOADDR(port+R_MAC_ETHERNET_ADDR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 free_netdev(dev);
3011 continue;
3012 }
3013 dev_sbmac[idx] = dev;
3014 }
3015 return 0;
3016}
3017
3018
3019static void __exit
3020sbmac_cleanup_module(void)
3021{
3022 struct net_device *dev;
3023 int idx;
3024
3025 for (idx = 0; idx < MAX_UNITS; idx++) {
3026 struct sbmac_softc *sc;
3027 dev = dev_sbmac[idx];
3028 if (!dev)
3029 continue;
3030
3031 sc = netdev_priv(dev);
3032 unregister_netdev(dev);
3033 sbmac_uninitctx(sc);
3034 free_netdev(dev);
3035 }
3036}
3037
3038module_init(sbmac_init_module);
3039module_exit(sbmac_cleanup_module);