blob: f2be9f83f09172b7a02ab530f3dc7d4b518ffe27 [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>
34#include <linux/config.h>
35#include <linux/bitops.h>
36#include <asm/processor.h> /* Processor type for cache alignment. */
37#include <asm/io.h>
38#include <asm/cache.h>
39
40/* This is only here until the firmware is ready. In that case,
41 the firmware leaves the ethernet address in the register for us. */
42#ifdef CONFIG_SIBYTE_STANDALONE
43#define SBMAC_ETH0_HWADDR "40:00:00:00:01:00"
44#define SBMAC_ETH1_HWADDR "40:00:00:00:01:01"
45#define SBMAC_ETH2_HWADDR "40:00:00:00:01:02"
Ralf Baechlef90fdc32006-02-08 23:23:26 +000046#define SBMAC_ETH3_HWADDR "40:00:00:00:01:03"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
48
49
50/* These identify the driver base version and may not be removed. */
51#if 0
52static char version1[] __devinitdata =
53"sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg\n";
54#endif
55
56
57/* Operational parameters that usually are not changed. */
58
59#define CONFIG_SBMAC_COALESCE
60
Ralf Baechlef90fdc32006-02-08 23:23:26 +000061#define MAX_UNITS 4 /* More are supported, limit only on options */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Time in jiffies before concluding the transmitter is hung. */
64#define TX_TIMEOUT (2*HZ)
65
66
67MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
68MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
69
70/* A few user-configurable values which may be modified when a driver
71 module is loaded. */
72
73/* 1 normal messages, 0 quiet .. 7 verbose. */
74static int debug = 1;
75module_param(debug, int, S_IRUGO);
76MODULE_PARM_DESC(debug, "Debug messages");
77
78/* mii status msgs */
79static int noisy_mii = 1;
80module_param(noisy_mii, int, S_IRUGO);
81MODULE_PARM_DESC(noisy_mii, "MII status messages");
82
83/* Used to pass the media type, etc.
84 Both 'options[]' and 'full_duplex[]' should exist for driver
85 interoperability.
86 The media type is usually passed in 'options[]'.
87*/
88#ifdef MODULE
Ralf Baechlef90fdc32006-02-08 23:23:26 +000089static int options[MAX_UNITS] = {-1, -1, -1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070090module_param_array(options, int, NULL, S_IRUGO);
91MODULE_PARM_DESC(options, "1-" __MODULE_STRING(MAX_UNITS));
92
Ralf Baechlef90fdc32006-02-08 23:23:26 +000093static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094module_param_array(full_duplex, int, NULL, S_IRUGO);
95MODULE_PARM_DESC(full_duplex, "1-" __MODULE_STRING(MAX_UNITS));
96#endif
97
98#ifdef CONFIG_SBMAC_COALESCE
99static int int_pktcnt = 0;
100module_param(int_pktcnt, int, S_IRUGO);
101MODULE_PARM_DESC(int_pktcnt, "Packet count");
102
103static int int_timeout = 0;
104module_param(int_timeout, int, S_IRUGO);
105MODULE_PARM_DESC(int_timeout, "Timeout value");
106#endif
107
108#include <asm/sibyte/sb1250.h>
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000109#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
110#include <asm/sibyte/bcm1480_regs.h>
111#include <asm/sibyte/bcm1480_int.h>
112#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <asm/sibyte/sb1250_regs.h>
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000114#include <asm/sibyte/sb1250_int.h>
115#else
116#error invalid SiByte MAC configuation
117#endif
118#include <asm/sibyte/sb1250_scd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#include <asm/sibyte/sb1250_mac.h>
120#include <asm/sibyte/sb1250_dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000122#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
123#define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
124#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
125#define UNIT_INT(n) (K_INT_MAC_0 + (n))
126#else
127#error invalid SiByte MAC configuation
128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/**********************************************************************
131 * Simple types
132 ********************************************************************* */
133
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135typedef enum { sbmac_speed_auto, sbmac_speed_10,
136 sbmac_speed_100, sbmac_speed_1000 } sbmac_speed_t;
137
138typedef enum { sbmac_duplex_auto, sbmac_duplex_half,
139 sbmac_duplex_full } sbmac_duplex_t;
140
141typedef enum { sbmac_fc_auto, sbmac_fc_disabled, sbmac_fc_frame,
142 sbmac_fc_collision, sbmac_fc_carrier } sbmac_fc_t;
143
Ralf Baechle74b02472005-10-19 15:40:02 +0100144typedef enum { sbmac_state_uninit, sbmac_state_off, sbmac_state_on,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 sbmac_state_broken } sbmac_state_t;
146
147
148/**********************************************************************
149 * Macros
150 ********************************************************************* */
151
152
153#define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
154 (d)->sbdma_dscrtable : (d)->f+1)
155
156
157#define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define SBMAC_MAX_TXDESCR 32
160#define SBMAC_MAX_RXDESCR 32
161
162#define ETHER_ALIGN 2
163#define ETHER_ADDR_LEN 6
Ralf Baechle74b02472005-10-19 15:40:02 +0100164#define ENET_PACKET_SIZE 1518
165/*#define ENET_PACKET_SIZE 9216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/**********************************************************************
168 * DMA Descriptor structure
169 ********************************************************************* */
170
171typedef struct sbdmadscr_s {
172 uint64_t dscr_a;
173 uint64_t dscr_b;
174} sbdmadscr_t;
175
176typedef unsigned long paddr_t;
177
178/**********************************************************************
179 * DMA Controller structure
180 ********************************************************************* */
181
182typedef struct sbmacdma_s {
Ralf Baechle74b02472005-10-19 15:40:02 +0100183
184 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * This stuff is used to identify the channel and the registers
186 * associated with it.
187 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct sbmac_softc *sbdma_eth; /* back pointer to associated MAC */
190 int sbdma_channel; /* channel number */
191 int sbdma_txdir; /* direction (1=transmit) */
192 int sbdma_maxdescr; /* total # of descriptors in ring */
193#ifdef CONFIG_SBMAC_COALESCE
194 int sbdma_int_pktcnt; /* # descriptors rx/tx before interrupt*/
195 int sbdma_int_timeout; /* # usec rx/tx interrupt */
196#endif
197
Ralf Baechle20399732005-10-19 15:39:05 +0100198 volatile void __iomem *sbdma_config0; /* DMA config register 0 */
199 volatile void __iomem *sbdma_config1; /* DMA config register 1 */
200 volatile void __iomem *sbdma_dscrbase; /* Descriptor base address */
201 volatile void __iomem *sbdma_dscrcnt; /* Descriptor count register */
202 volatile void __iomem *sbdma_curdscr; /* current descriptor address */
Ralf Baechle74b02472005-10-19 15:40:02 +0100203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /*
205 * This stuff is for maintenance of the ring
206 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 sbdmadscr_t *sbdma_dscrtable; /* base of descriptor table */
209 sbdmadscr_t *sbdma_dscrtable_end; /* end of descriptor table */
Ralf Baechle74b02472005-10-19 15:40:02 +0100210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct sk_buff **sbdma_ctxtable; /* context table, one per descr */
Ralf Baechle74b02472005-10-19 15:40:02 +0100212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 paddr_t sbdma_dscrtable_phys; /* and also the phys addr */
214 sbdmadscr_t *sbdma_addptr; /* next dscr for sw to add */
215 sbdmadscr_t *sbdma_remptr; /* next dscr for sw to remove */
216} sbmacdma_t;
217
218
219/**********************************************************************
220 * Ethernet softc structure
221 ********************************************************************* */
222
223struct sbmac_softc {
Ralf Baechle74b02472005-10-19 15:40:02 +0100224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * Linux-specific things
227 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct net_device *sbm_dev; /* pointer to linux device */
230 spinlock_t sbm_lock; /* spin lock */
231 struct timer_list sbm_timer; /* for monitoring MII */
Ralf Baechle74b02472005-10-19 15:40:02 +0100232 struct net_device_stats sbm_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int sbm_devflags; /* current device flags */
234
235 int sbm_phy_oldbmsr;
236 int sbm_phy_oldanlpar;
237 int sbm_phy_oldk1stsr;
238 int sbm_phy_oldlinkstat;
239 int sbm_buffersize;
Ralf Baechle74b02472005-10-19 15:40:02 +0100240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned char sbm_phys[2];
Ralf Baechle74b02472005-10-19 15:40:02 +0100242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /*
244 * Controller-specific things
245 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100246
Ralf Baechle20399732005-10-19 15:39:05 +0100247 volatile void __iomem *sbm_base; /* MAC's base address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sbmac_state_t sbm_state; /* current state */
Ralf Baechle74b02472005-10-19 15:40:02 +0100249
Ralf Baechle20399732005-10-19 15:39:05 +0100250 volatile void __iomem *sbm_macenable; /* MAC Enable Register */
251 volatile void __iomem *sbm_maccfg; /* MAC Configuration Register */
252 volatile void __iomem *sbm_fifocfg; /* FIFO configuration register */
253 volatile void __iomem *sbm_framecfg; /* Frame configuration register */
254 volatile void __iomem *sbm_rxfilter; /* receive filter register */
255 volatile void __iomem *sbm_isr; /* Interrupt status register */
256 volatile void __iomem *sbm_imr; /* Interrupt mask register */
257 volatile void __iomem *sbm_mdio; /* MDIO register */
Ralf Baechle74b02472005-10-19 15:40:02 +0100258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sbmac_speed_t sbm_speed; /* current speed */
260 sbmac_duplex_t sbm_duplex; /* current duplex */
261 sbmac_fc_t sbm_fc; /* current flow control setting */
Ralf Baechle74b02472005-10-19 15:40:02 +0100262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
Ralf Baechle74b02472005-10-19 15:40:02 +0100264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sbmacdma_t sbm_txdma; /* for now, only use channel 0 */
266 sbmacdma_t sbm_rxdma;
267 int rx_hw_checksum;
268 int sbe_idx;
269};
270
271
272/**********************************************************************
273 * Externs
274 ********************************************************************* */
275
276/**********************************************************************
277 * Prototypes
278 ********************************************************************* */
279
280static void sbdma_initctx(sbmacdma_t *d,
281 struct sbmac_softc *s,
282 int chan,
283 int txrx,
284 int maxdescr);
285static void sbdma_channel_start(sbmacdma_t *d, int rxtx);
286static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *m);
287static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *m);
288static void sbdma_emptyring(sbmacdma_t *d);
289static void sbdma_fillring(sbmacdma_t *d);
290static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d);
291static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d);
292static int sbmac_initctx(struct sbmac_softc *s);
293static void sbmac_channel_start(struct sbmac_softc *s);
294static void sbmac_channel_stop(struct sbmac_softc *s);
295static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *,sbmac_state_t);
296static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff);
297static uint64_t sbmac_addr2reg(unsigned char *ptr);
298static irqreturn_t sbmac_intr(int irq,void *dev_instance,struct pt_regs *rgs);
299static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
300static void sbmac_setmulti(struct sbmac_softc *sc);
301static int sbmac_init(struct net_device *dev, int idx);
302static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed);
303static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc);
304
305static int sbmac_open(struct net_device *dev);
306static void sbmac_timer(unsigned long data);
307static void sbmac_tx_timeout (struct net_device *dev);
308static struct net_device_stats *sbmac_get_stats(struct net_device *dev);
309static void sbmac_set_rx_mode(struct net_device *dev);
310static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
311static int sbmac_close(struct net_device *dev);
312static int sbmac_mii_poll(struct sbmac_softc *s,int noisy);
Ralf Baechle59b81822005-10-20 12:01:28 +0100313static int sbmac_mii_probe(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315static void sbmac_mii_sync(struct sbmac_softc *s);
316static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt);
317static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx);
318static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
319 unsigned int regval);
320
321
322/**********************************************************************
323 * Globals
324 ********************************************************************* */
325
326static uint64_t sbmac_orig_hwaddr[MAX_UNITS];
327
328
329/**********************************************************************
330 * MDIO constants
331 ********************************************************************* */
332
333#define MII_COMMAND_START 0x01
334#define MII_COMMAND_READ 0x02
335#define MII_COMMAND_WRITE 0x01
336#define MII_COMMAND_ACK 0x02
337
338#define BMCR_RESET 0x8000
339#define BMCR_LOOPBACK 0x4000
340#define BMCR_SPEED0 0x2000
341#define BMCR_ANENABLE 0x1000
342#define BMCR_POWERDOWN 0x0800
343#define BMCR_ISOLATE 0x0400
344#define BMCR_RESTARTAN 0x0200
345#define BMCR_DUPLEX 0x0100
346#define BMCR_COLTEST 0x0080
347#define BMCR_SPEED1 0x0040
348#define BMCR_SPEED1000 BMCR_SPEED1
349#define BMCR_SPEED100 BMCR_SPEED0
350#define BMCR_SPEED10 0
351
352#define BMSR_100BT4 0x8000
353#define BMSR_100BT_FDX 0x4000
354#define BMSR_100BT_HDX 0x2000
355#define BMSR_10BT_FDX 0x1000
356#define BMSR_10BT_HDX 0x0800
357#define BMSR_100BT2_FDX 0x0400
358#define BMSR_100BT2_HDX 0x0200
359#define BMSR_1000BT_XSR 0x0100
360#define BMSR_PRESUP 0x0040
361#define BMSR_ANCOMPLT 0x0020
362#define BMSR_REMFAULT 0x0010
363#define BMSR_AUTONEG 0x0008
364#define BMSR_LINKSTAT 0x0004
365#define BMSR_JABDETECT 0x0002
366#define BMSR_EXTCAPAB 0x0001
367
368#define PHYIDR1 0x2000
369#define PHYIDR2 0x5C60
370
371#define ANAR_NP 0x8000
372#define ANAR_RF 0x2000
373#define ANAR_ASYPAUSE 0x0800
374#define ANAR_PAUSE 0x0400
375#define ANAR_T4 0x0200
376#define ANAR_TXFD 0x0100
377#define ANAR_TXHD 0x0080
378#define ANAR_10FD 0x0040
379#define ANAR_10HD 0x0020
380#define ANAR_PSB 0x0001
381
382#define ANLPAR_NP 0x8000
383#define ANLPAR_ACK 0x4000
384#define ANLPAR_RF 0x2000
385#define ANLPAR_ASYPAUSE 0x0800
386#define ANLPAR_PAUSE 0x0400
387#define ANLPAR_T4 0x0200
388#define ANLPAR_TXFD 0x0100
389#define ANLPAR_TXHD 0x0080
390#define ANLPAR_10FD 0x0040
391#define ANLPAR_10HD 0x0020
392#define ANLPAR_PSB 0x0001 /* 802.3 */
393
394#define ANER_PDF 0x0010
395#define ANER_LPNPABLE 0x0008
396#define ANER_NPABLE 0x0004
397#define ANER_PAGERX 0x0002
398#define ANER_LPANABLE 0x0001
399
400#define ANNPTR_NP 0x8000
401#define ANNPTR_MP 0x2000
402#define ANNPTR_ACK2 0x1000
403#define ANNPTR_TOGTX 0x0800
404#define ANNPTR_CODE 0x0008
405
406#define ANNPRR_NP 0x8000
407#define ANNPRR_MP 0x2000
408#define ANNPRR_ACK3 0x1000
409#define ANNPRR_TOGTX 0x0800
410#define ANNPRR_CODE 0x0008
411
412#define K1TCR_TESTMODE 0x0000
413#define K1TCR_MSMCE 0x1000
414#define K1TCR_MSCV 0x0800
415#define K1TCR_RPTR 0x0400
416#define K1TCR_1000BT_FDX 0x200
417#define K1TCR_1000BT_HDX 0x100
418
419#define K1STSR_MSMCFLT 0x8000
420#define K1STSR_MSCFGRES 0x4000
421#define K1STSR_LRSTAT 0x2000
422#define K1STSR_RRSTAT 0x1000
423#define K1STSR_LP1KFD 0x0800
424#define K1STSR_LP1KHD 0x0400
425#define K1STSR_LPASMDIR 0x0200
426
427#define K1SCR_1KX_FDX 0x8000
428#define K1SCR_1KX_HDX 0x4000
429#define K1SCR_1KT_FDX 0x2000
430#define K1SCR_1KT_HDX 0x1000
431
432#define STRAP_PHY1 0x0800
433#define STRAP_NCMODE 0x0400
434#define STRAP_MANMSCFG 0x0200
435#define STRAP_ANENABLE 0x0100
436#define STRAP_MSVAL 0x0080
437#define STRAP_1KHDXADV 0x0010
438#define STRAP_1KFDXADV 0x0008
439#define STRAP_100ADV 0x0004
440#define STRAP_SPEEDSEL 0x0000
441#define STRAP_SPEED100 0x0001
442
443#define PHYSUP_SPEED1000 0x10
444#define PHYSUP_SPEED100 0x08
445#define PHYSUP_SPEED10 0x00
446#define PHYSUP_LINKUP 0x04
447#define PHYSUP_FDX 0x02
448
449#define MII_BMCR 0x00 /* Basic mode control register (rw) */
450#define MII_BMSR 0x01 /* Basic mode status register (ro) */
Ralf Baechle59b81822005-10-20 12:01:28 +0100451#define MII_PHYIDR1 0x02
452#define MII_PHYIDR2 0x03
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#define MII_K1STSR 0x0A /* 1K Status Register (ro) */
455#define MII_ANLPAR 0x05 /* Autonegotiation lnk partner abilities (rw) */
456
457
458#define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
459
460#define ENABLE 1
461#define DISABLE 0
462
463/**********************************************************************
464 * SBMAC_MII_SYNC(s)
Ralf Baechle74b02472005-10-19 15:40:02 +0100465 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * Synchronize with the MII - send a pattern of bits to the MII
467 * that will guarantee that it is ready to accept a command.
Ralf Baechle74b02472005-10-19 15:40:02 +0100468 *
469 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +0100471 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * Return value:
473 * nothing
474 ********************************************************************* */
475
476static void sbmac_mii_sync(struct sbmac_softc *s)
477{
478 int cnt;
479 uint64_t bits;
480 int mac_mdio_genc;
481
Ralf Baechle20399732005-10-19 15:39:05 +0100482 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
Ralf Baechle74b02472005-10-19 15:40:02 +0100485
Ralf Baechle20399732005-10-19 15:39:05 +0100486 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 for (cnt = 0; cnt < 32; cnt++) {
Ralf Baechle20399732005-10-19 15:39:05 +0100489 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
490 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492}
493
494/**********************************************************************
495 * SBMAC_MII_SENDDATA(s,data,bitcnt)
Ralf Baechle74b02472005-10-19 15:40:02 +0100496 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * Send some bits to the MII. The bits to be sent are right-
498 * justified in the 'data' parameter.
Ralf Baechle74b02472005-10-19 15:40:02 +0100499 *
500 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * s - sbmac structure
502 * data - data to send
503 * bitcnt - number of bits to send
504 ********************************************************************* */
505
506static void sbmac_mii_senddata(struct sbmac_softc *s,unsigned int data, int bitcnt)
507{
508 int i;
509 uint64_t bits;
510 unsigned int curmask;
511 int mac_mdio_genc;
512
Ralf Baechle20399732005-10-19 15:39:05 +0100513 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 bits = M_MAC_MDIO_DIR_OUTPUT;
Ralf Baechle20399732005-10-19 15:39:05 +0100516 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 curmask = 1 << (bitcnt - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 for (i = 0; i < bitcnt; i++) {
521 if (data & curmask)
522 bits |= M_MAC_MDIO_OUT;
523 else bits &= ~M_MAC_MDIO_OUT;
Ralf Baechle20399732005-10-19 15:39:05 +0100524 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
525 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
526 __raw_writeq(bits | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 curmask >>= 1;
528 }
529}
530
531
532
533/**********************************************************************
534 * SBMAC_MII_READ(s,phyaddr,regidx)
Ralf Baechle74b02472005-10-19 15:40:02 +0100535 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * Read a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100537 *
538 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * s - sbmac structure
540 * phyaddr - PHY's address
541 * regidx = index of register to read
Ralf Baechle74b02472005-10-19 15:40:02 +0100542 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * Return value:
544 * value read, or 0 if an error occurred.
545 ********************************************************************* */
546
547static unsigned int sbmac_mii_read(struct sbmac_softc *s,int phyaddr,int regidx)
548{
549 int idx;
550 int error;
551 int regval;
552 int mac_mdio_genc;
553
554 /*
555 * Synchronize ourselves so that the PHY knows the next
556 * thing coming down is a command
557 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 sbmac_mii_sync(s);
Ralf Baechle74b02472005-10-19 15:40:02 +0100560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /*
562 * Send the data to the PHY. The sequence is
563 * a "start" command (2 bits)
564 * a "read" command (2 bits)
565 * the PHY addr (5 bits)
566 * the register index (5 bits)
567 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 sbmac_mii_senddata(s,MII_COMMAND_START, 2);
570 sbmac_mii_senddata(s,MII_COMMAND_READ, 2);
571 sbmac_mii_senddata(s,phyaddr, 5);
572 sbmac_mii_senddata(s,regidx, 5);
Ralf Baechle74b02472005-10-19 15:40:02 +0100573
Ralf Baechle20399732005-10-19 15:39:05 +0100574 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100575
576 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * Switch the port around without a clock transition.
578 */
Ralf Baechle20399732005-10-19 15:39:05 +0100579 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Send out a clock pulse to signal we want the status
583 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100584
Ralf Baechle20399732005-10-19 15:39:05 +0100585 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
586 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100587
588 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * If an error occurred, the PHY will signal '1' back
590 */
Ralf Baechle20399732005-10-19 15:39:05 +0100591 error = __raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN;
Ralf Baechle74b02472005-10-19 15:40:02 +0100592
593 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * Issue an 'idle' clock pulse, but keep the direction
595 * the same.
596 */
Ralf Baechle20399732005-10-19 15:39:05 +0100597 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
598 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 regval = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 for (idx = 0; idx < 16; idx++) {
603 regval <<= 1;
Ralf Baechle74b02472005-10-19 15:40:02 +0100604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (error == 0) {
Ralf Baechle20399732005-10-19 15:39:05 +0100606 if (__raw_readq(s->sbm_mdio) & M_MAC_MDIO_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 regval |= 1;
608 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100609
Ralf Baechle20399732005-10-19 15:39:05 +0100610 __raw_writeq(M_MAC_MDIO_DIR_INPUT|M_MAC_MDC | mac_mdio_genc, s->sbm_mdio);
611 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Switch back to output */
Ralf Baechle20399732005-10-19 15:39:05 +0100615 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (error == 0)
618 return regval;
619 return 0;
620}
621
622
623/**********************************************************************
624 * SBMAC_MII_WRITE(s,phyaddr,regidx,regval)
Ralf Baechle74b02472005-10-19 15:40:02 +0100625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * Write a value to a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100627 *
628 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * s - sbmac structure
630 * phyaddr - PHY to use
631 * regidx - register within the PHY
632 * regval - data to write to register
Ralf Baechle74b02472005-10-19 15:40:02 +0100633 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * Return value:
635 * nothing
636 ********************************************************************* */
637
638static void sbmac_mii_write(struct sbmac_softc *s,int phyaddr,int regidx,
639 unsigned int regval)
640{
641 int mac_mdio_genc;
642
643 sbmac_mii_sync(s);
Ralf Baechle74b02472005-10-19 15:40:02 +0100644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 sbmac_mii_senddata(s,MII_COMMAND_START,2);
646 sbmac_mii_senddata(s,MII_COMMAND_WRITE,2);
647 sbmac_mii_senddata(s,phyaddr, 5);
648 sbmac_mii_senddata(s,regidx, 5);
649 sbmac_mii_senddata(s,MII_COMMAND_ACK,2);
650 sbmac_mii_senddata(s,regval,16);
651
Ralf Baechle20399732005-10-19 15:39:05 +0100652 mac_mdio_genc = __raw_readq(s->sbm_mdio) & M_MAC_GENC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Ralf Baechle20399732005-10-19 15:39:05 +0100654 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, s->sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657
658
659/**********************************************************************
660 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
Ralf Baechle74b02472005-10-19 15:40:02 +0100661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * Initialize a DMA channel context. Since there are potentially
663 * eight DMA channels per MAC, it's nice to do this in a standard
Ralf Baechle74b02472005-10-19 15:40:02 +0100664 * way.
665 *
666 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * d - sbmacdma_t structure (DMA channel context)
668 * s - sbmac_softc structure (pointer to a MAC)
669 * chan - channel number (0..1 right now)
670 * txrx - Identifies DMA_TX or DMA_RX for channel direction
671 * maxdescr - number of descriptors
Ralf Baechle74b02472005-10-19 15:40:02 +0100672 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * Return value:
674 * nothing
675 ********************************************************************* */
676
677static void sbdma_initctx(sbmacdma_t *d,
678 struct sbmac_softc *s,
679 int chan,
680 int txrx,
681 int maxdescr)
682{
Ralf Baechle74b02472005-10-19 15:40:02 +0100683 /*
684 * Save away interesting stuff in the structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 d->sbdma_eth = s;
688 d->sbdma_channel = chan;
689 d->sbdma_txdir = txrx;
Ralf Baechle74b02472005-10-19 15:40:02 +0100690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691#if 0
692 /* RMON clearing */
693 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
694#endif
695
Ralf Baechle20399732005-10-19 15:39:05 +0100696 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BYTES)));
697 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_COLLISIONS)));
698 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_LATE_COL)));
699 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_EX_COL)));
700 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_FCS_ERROR)));
701 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_ABORT)));
702 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_BAD)));
703 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_GOOD)));
704 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_RUNT)));
705 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_TX_OVERSIZE)));
706 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BYTES)));
707 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_MCAST)));
708 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BCAST)));
709 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_BAD)));
710 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_GOOD)));
711 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_RUNT)));
712 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_OVERSIZE)));
713 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_FCS_ERROR)));
714 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_LENGTH_ERROR)));
715 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_CODE_ERROR)));
716 __raw_writeq(0, IOADDR(A_MAC_REGISTER(s->sbe_idx, R_MAC_RMON_RX_ALIGN_ERROR)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Ralf Baechle74b02472005-10-19 15:40:02 +0100718 /*
719 * initialize register pointers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100721
722 d->sbdma_config0 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100724 d->sbdma_config1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100726 d->sbdma_dscrbase =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
Ralf Baechle74b02472005-10-19 15:40:02 +0100728 d->sbdma_dscrcnt =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
Ralf Baechle74b02472005-10-19 15:40:02 +0100730 d->sbdma_curdscr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
Ralf Baechle74b02472005-10-19 15:40:02 +0100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Allocate memory for the ring
735 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 d->sbdma_maxdescr = maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100738
739 d->sbdma_dscrtable = (sbdmadscr_t *)
Ralf Baechle04115de2005-10-10 14:50:36 +0100740 kmalloc((d->sbdma_maxdescr+1)*sizeof(sbdmadscr_t), GFP_KERNEL);
741
742 /*
743 * The descriptor table must be aligned to at least 16 bytes or the
744 * MAC will corrupt it.
745 */
746 d->sbdma_dscrtable = (sbdmadscr_t *)
747 ALIGN((unsigned long)d->sbdma_dscrtable, sizeof(sbdmadscr_t));
Ralf Baechle74b02472005-10-19 15:40:02 +0100748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 memset(d->sbdma_dscrtable,0,d->sbdma_maxdescr*sizeof(sbdmadscr_t));
Ralf Baechle74b02472005-10-19 15:40:02 +0100750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
Ralf Baechle74b02472005-10-19 15:40:02 +0100754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /*
756 * And context table
757 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100758
759 d->sbdma_ctxtable = (struct sk_buff **)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kmalloc(d->sbdma_maxdescr*sizeof(struct sk_buff *), GFP_KERNEL);
Ralf Baechle74b02472005-10-19 15:40:02 +0100761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 memset(d->sbdma_ctxtable,0,d->sbdma_maxdescr*sizeof(struct sk_buff *));
Ralf Baechle74b02472005-10-19 15:40:02 +0100763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#ifdef CONFIG_SBMAC_COALESCE
765 /*
766 * Setup Rx/Tx DMA coalescing defaults
767 */
768
769 if ( int_pktcnt ) {
770 d->sbdma_int_pktcnt = int_pktcnt;
771 } else {
772 d->sbdma_int_pktcnt = 1;
773 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if ( int_timeout ) {
776 d->sbdma_int_timeout = int_timeout;
777 } else {
778 d->sbdma_int_timeout = 0;
779 }
780#endif
781
782}
783
784/**********************************************************************
785 * SBDMA_CHANNEL_START(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100788 *
789 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * d - DMA channel to init (context must be previously init'd
791 * rxtx - DMA_RX or DMA_TX depending on what type of channel
Ralf Baechle74b02472005-10-19 15:40:02 +0100792 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * Return value:
794 * nothing
795 ********************************************************************* */
796
797static void sbdma_channel_start(sbmacdma_t *d, int rxtx )
798{
799 /*
800 * Turn on the DMA channel
801 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +0100804 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
805 0, d->sbdma_config1);
806 __raw_writeq(M_DMA_EOP_INT_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 V_DMA_RINGSZ(d->sbdma_maxdescr) |
808 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
Ralf Baechle20399732005-10-19 15:39:05 +0100809 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810#else
Ralf Baechle20399732005-10-19 15:39:05 +0100811 __raw_writeq(0, d->sbdma_config1);
812 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
813 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814#endif
815
Ralf Baechle20399732005-10-19 15:39:05 +0100816 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /*
819 * Initialize ring pointers
820 */
821
822 d->sbdma_addptr = d->sbdma_dscrtable;
823 d->sbdma_remptr = d->sbdma_dscrtable;
824}
825
826/**********************************************************************
827 * SBDMA_CHANNEL_STOP(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100828 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100830 *
831 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * d - DMA channel to init (context must be previously init'd
Ralf Baechle74b02472005-10-19 15:40:02 +0100833 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * Return value:
835 * nothing
836 ********************************************************************* */
837
838static void sbdma_channel_stop(sbmacdma_t *d)
839{
840 /*
841 * Turn off the DMA channel
842 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100843
Ralf Baechle20399732005-10-19 15:39:05 +0100844 __raw_writeq(0, d->sbdma_config1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100845
Ralf Baechle20399732005-10-19 15:39:05 +0100846 __raw_writeq(0, d->sbdma_dscrbase);
Ralf Baechle74b02472005-10-19 15:40:02 +0100847
Ralf Baechle20399732005-10-19 15:39:05 +0100848 __raw_writeq(0, d->sbdma_config0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /*
851 * Zero ring pointers
852 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100853
Ralf Baechle20399732005-10-19 15:39:05 +0100854 d->sbdma_addptr = NULL;
855 d->sbdma_remptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
859{
860 unsigned long addr;
861 unsigned long newaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 addr = (unsigned long) skb->data;
Ralf Baechle74b02472005-10-19 15:40:02 +0100864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 newaddr = (addr + power2 - 1) & ~(power2 - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 skb_reserve(skb,newaddr-addr+offset);
868}
869
870
871/**********************************************************************
872 * SBDMA_ADD_RCVBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +0100873 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * Add a buffer to the specified DMA channel. For receive channels,
875 * this queues a buffer for inbound packets.
Ralf Baechle74b02472005-10-19 15:40:02 +0100876 *
877 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * d - DMA channel descriptor
879 * sb - sk_buff to add, or NULL if we should allocate one
Ralf Baechle74b02472005-10-19 15:40:02 +0100880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * Return value:
882 * 0 if buffer could not be added (ring is full)
883 * 1 if buffer added successfully
884 ********************************************************************* */
885
886
887static int sbdma_add_rcvbuffer(sbmacdma_t *d,struct sk_buff *sb)
888{
889 sbdmadscr_t *dsc;
890 sbdmadscr_t *nextdsc;
891 struct sk_buff *sb_new = NULL;
892 int pktsize = ENET_PACKET_SIZE;
Ralf Baechle74b02472005-10-19 15:40:02 +0100893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +0100895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 dsc = d->sbdma_addptr;
897 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +0100898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /*
900 * figure out if the ring is full - if the next descriptor
901 * is the same as the one that we're going to remove from
902 * the ring, the ring is full
903 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (nextdsc == d->sbdma_remptr) {
906 return -ENOSPC;
907 }
908
Ralf Baechle74b02472005-10-19 15:40:02 +0100909 /*
910 * Allocate a sk_buff if we don't already have one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * If we do have an sk_buff, reset it so that it's empty.
912 *
913 * Note: sk_buffs don't seem to be guaranteed to have any sort
914 * of alignment when they are allocated. Therefore, allocate enough
915 * extra space to make sure that:
916 *
917 * 1. the data does not start in the middle of a cache line.
918 * 2. The data does not end in the middle of a cache line
Ralf Baechle74b02472005-10-19 15:40:02 +0100919 * 3. The buffer can be aligned such that the IP addresses are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * naturally aligned.
921 *
922 * Remember, the SOCs MAC writes whole cache lines at a time,
923 * without reading the old contents first. So, if the sk_buff's
924 * data portion starts in the middle of a cache line, the SOC
925 * DMA will trash the beginning (and ending) portions.
926 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (sb == NULL) {
929 sb_new = dev_alloc_skb(ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN);
930 if (sb_new == NULL) {
931 printk(KERN_INFO "%s: sk_buff allocation failed\n",
932 d->sbdma_eth->sbm_dev->name);
933 return -ENOBUFS;
934 }
935
936 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, ETHER_ALIGN);
937
938 /* mark skbuff owned by our device */
939 sb_new->dev = d->sbdma_eth->sbm_dev;
940 }
941 else {
942 sb_new = sb;
Ralf Baechle74b02472005-10-19 15:40:02 +0100943 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * nothing special to reinit buffer, it's already aligned
945 * and sb->data already points to a good place.
946 */
947 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100950 * fill in the descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953#ifdef CONFIG_SBMAC_COALESCE
954 /*
955 * Do not interrupt per DMA transfer.
956 */
David S. Miller689be432005-06-28 15:25:31 -0700957 dsc->dscr_a = virt_to_phys(sb_new->data) |
Ralf Baechle20399732005-10-19 15:39:05 +0100958 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) | 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#else
David S. Miller689be432005-06-28 15:25:31 -0700960 dsc->dscr_a = virt_to_phys(sb_new->data) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
962 M_DMA_DSCRA_INTERRUPT;
963#endif
964
965 /* receiving: no options */
966 dsc->dscr_b = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100969 * fill in the context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
Ralf Baechle74b02472005-10-19 15:40:02 +0100973
974 /*
975 * point at next packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 d->sbdma_addptr = nextdsc;
Ralf Baechle74b02472005-10-19 15:40:02 +0100979
980 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * Give the buffer to the DMA engine.
982 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100983
Ralf Baechle20399732005-10-19 15:39:05 +0100984 __raw_writeq(1, d->sbdma_dscrcnt);
Ralf Baechle74b02472005-10-19 15:40:02 +0100985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0; /* we did it */
987}
988
989/**********************************************************************
990 * SBDMA_ADD_TXBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +0100991 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * Add a transmit buffer to the specified DMA channel, causing a
993 * transmit to start.
Ralf Baechle74b02472005-10-19 15:40:02 +0100994 *
995 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 * d - DMA channel descriptor
997 * sb - sk_buff to add
Ralf Baechle74b02472005-10-19 15:40:02 +0100998 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * Return value:
1000 * 0 transmit queued successfully
1001 * otherwise error code
1002 ********************************************************************* */
1003
1004
1005static int sbdma_add_txbuffer(sbmacdma_t *d,struct sk_buff *sb)
1006{
1007 sbdmadscr_t *dsc;
1008 sbdmadscr_t *nextdsc;
1009 uint64_t phys;
1010 uint64_t ncb;
1011 int length;
Ralf Baechle74b02472005-10-19 15:40:02 +01001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +01001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dsc = d->sbdma_addptr;
1016 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /*
1019 * figure out if the ring is full - if the next descriptor
1020 * is the same as the one that we're going to remove from
1021 * the ring, the ring is full
1022 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (nextdsc == d->sbdma_remptr) {
1025 return -ENOSPC;
1026 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * Under Linux, it's not necessary to copy/coalesce buffers
1030 * like it is on NetBSD. We think they're all contiguous,
1031 * but that may not be true for GBE.
1032 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 length = sb->len;
Ralf Baechle74b02472005-10-19 15:40:02 +01001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /*
1037 * fill in the descriptor. Note that the number of cache
1038 * blocks in the descriptor is the number of blocks
1039 * *spanned*, so we need to add in the offset (if any)
1040 * while doing the calculation.
1041 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 phys = virt_to_phys(sb->data);
1044 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
1045
Ralf Baechle74b02472005-10-19 15:40:02 +01001046 dsc->dscr_a = phys |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 V_DMA_DSCRA_A_SIZE(ncb) |
1048#ifndef CONFIG_SBMAC_COALESCE
1049 M_DMA_DSCRA_INTERRUPT |
1050#endif
1051 M_DMA_ETHTX_SOP;
Ralf Baechle74b02472005-10-19 15:40:02 +01001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* transmitting: set outbound options and length */
1054
1055 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
1056 V_DMA_DSCRB_PKT_SIZE(length);
Ralf Baechle74b02472005-10-19 15:40:02 +01001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001059 * fill in the context
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
Ralf Baechle74b02472005-10-19 15:40:02 +01001063
1064 /*
1065 * point at next packet
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 d->sbdma_addptr = nextdsc;
Ralf Baechle74b02472005-10-19 15:40:02 +01001069
1070 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * Give the buffer to the DMA engine.
1072 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001073
Ralf Baechle20399732005-10-19 15:39:05 +01001074 __raw_writeq(1, d->sbdma_dscrcnt);
Ralf Baechle74b02472005-10-19 15:40:02 +01001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0; /* we did it */
1077}
1078
1079
1080
1081
1082/**********************************************************************
1083 * SBDMA_EMPTYRING(d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001084 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * Free all allocated sk_buffs on the specified DMA channel;
Ralf Baechle74b02472005-10-19 15:40:02 +01001086 *
1087 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001089 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * Return value:
1091 * nothing
1092 ********************************************************************* */
1093
1094static void sbdma_emptyring(sbmacdma_t *d)
1095{
1096 int idx;
1097 struct sk_buff *sb;
Ralf Baechle74b02472005-10-19 15:40:02 +01001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
1100 sb = d->sbdma_ctxtable[idx];
1101 if (sb) {
1102 dev_kfree_skb(sb);
1103 d->sbdma_ctxtable[idx] = NULL;
1104 }
1105 }
1106}
1107
1108
1109/**********************************************************************
1110 * SBDMA_FILLRING(d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * Fill the specified DMA channel (must be receive channel)
1113 * with sk_buffs
Ralf Baechle74b02472005-10-19 15:40:02 +01001114 *
1115 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * Return value:
1119 * nothing
1120 ********************************************************************* */
1121
1122static void sbdma_fillring(sbmacdma_t *d)
1123{
1124 int idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
1127 if (sbdma_add_rcvbuffer(d,NULL) != 0)
1128 break;
1129 }
1130}
1131
1132
1133/**********************************************************************
1134 * SBDMA_RX_PROCESS(sc,d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001136 * Process "completed" receive buffers on the specified DMA channel.
1137 * Note that this isn't really ideal for priority channels, since
1138 * it processes all of the packets on a given channel before
1139 * returning.
1140 *
1141 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * sc - softc structure
1143 * d - DMA channel context
Ralf Baechle74b02472005-10-19 15:40:02 +01001144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * Return value:
1146 * nothing
1147 ********************************************************************* */
1148
1149static void sbdma_rx_process(struct sbmac_softc *sc,sbmacdma_t *d)
1150{
1151 int curidx;
1152 int hwidx;
1153 sbdmadscr_t *dsc;
1154 struct sk_buff *sb;
1155 int len;
Ralf Baechle74b02472005-10-19 15:40:02 +01001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 for (;;) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001158 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * figure out where we are (as an index) and where
1160 * the hardware is (also as an index)
1161 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001162 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * descriptor table was page-aligned and contiguous in
1164 * both virtual and physical memory -- you could then
1165 * just compare the low-order bits of the virtual address
1166 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1167 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
Ralf Baechle20399732005-10-19 15:39:05 +01001170 hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
Ralf Baechle74b02472005-10-19 15:40:02 +01001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * If they're the same, that means we've processed all
1175 * of the descriptors up to (but not including) the one that
1176 * the hardware is working on right now.
1177 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (curidx == hwidx)
1180 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /*
1183 * Otherwise, get the packet's sk_buff ptr back
1184 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 dsc = &(d->sbdma_dscrtable[curidx]);
1187 sb = d->sbdma_ctxtable[curidx];
1188 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
Ralf Baechle74b02472005-10-19 15:40:02 +01001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /*
1193 * Check packet status. If good, process it.
1194 * If not, silently drop it and put it back on the
1195 * receive ring.
1196 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (!(dsc->dscr_a & M_DMA_ETHRX_BAD)) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /*
1201 * Add a new buffer to replace the old one. If we fail
1202 * to allocate a buffer, we're going to drop this
1203 * packet and put it right back on the receive ring.
1204 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (sbdma_add_rcvbuffer(d,NULL) == -ENOBUFS) {
1207 sc->sbm_stats.rx_dropped++;
1208 sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
1209 } else {
1210 /*
1211 * Set length into the packet
1212 */
1213 skb_put(sb,len);
Ralf Baechle74b02472005-10-19 15:40:02 +01001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * Buffer has been replaced on the
1217 * receive ring. Pass the buffer to
1218 * the kernel
1219 */
1220 sc->sbm_stats.rx_bytes += len;
1221 sc->sbm_stats.rx_packets++;
1222 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1223 /* Check hw IPv4/TCP checksum if supported */
1224 if (sc->rx_hw_checksum == ENABLE) {
1225 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1226 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1227 sb->ip_summed = CHECKSUM_UNNECESSARY;
1228 /* don't need to set sb->csum */
1229 } else {
1230 sb->ip_summed = CHECKSUM_NONE;
1231 }
1232 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 netif_rx(sb);
1235 }
1236 } else {
1237 /*
1238 * Packet was mangled somehow. Just drop it and
1239 * put it back on the receive ring.
1240 */
1241 sc->sbm_stats.rx_errors++;
1242 sbdma_add_rcvbuffer(d,sb);
1243 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001244
1245
1246 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * .. and advance to the next buffer.
1248 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253}
1254
1255
1256
1257/**********************************************************************
1258 * SBDMA_TX_PROCESS(sc,d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001259 *
1260 * Process "completed" transmit buffers on the specified DMA channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * This is normally called within the interrupt service routine.
1262 * Note that this isn't really ideal for priority channels, since
Ralf Baechle74b02472005-10-19 15:40:02 +01001263 * it processes all of the packets on a given channel before
1264 * returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001266 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * sc - softc structure
1268 * d - DMA channel context
Ralf Baechle74b02472005-10-19 15:40:02 +01001269 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * Return value:
1271 * nothing
1272 ********************************************************************* */
1273
1274static void sbdma_tx_process(struct sbmac_softc *sc,sbmacdma_t *d)
1275{
1276 int curidx;
1277 int hwidx;
1278 sbdmadscr_t *dsc;
1279 struct sk_buff *sb;
1280 unsigned long flags;
1281
1282 spin_lock_irqsave(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 for (;;) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001285 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 * figure out where we are (as an index) and where
1287 * the hardware is (also as an index)
1288 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001289 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 * descriptor table was page-aligned and contiguous in
1291 * both virtual and physical memory -- you could then
1292 * just compare the low-order bits of the virtual address
1293 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1294 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
Ralf Baechle20399732005-10-19 15:39:05 +01001297 hwidx = (int) (((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 d->sbdma_dscrtable_phys) / sizeof(sbdmadscr_t));
1299
1300 /*
1301 * If they're the same, that means we've processed all
1302 * of the descriptors up to (but not including) the one that
1303 * the hardware is working on right now.
1304 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (curidx == hwidx)
1307 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * Otherwise, get the packet's sk_buff ptr back
1311 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 dsc = &(d->sbdma_dscrtable[curidx]);
1314 sb = d->sbdma_ctxtable[curidx];
1315 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /*
1318 * Stats
1319 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sc->sbm_stats.tx_bytes += sb->len;
1322 sc->sbm_stats.tx_packets++;
Ralf Baechle74b02472005-10-19 15:40:02 +01001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * for transmits, we just free buffers.
1326 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 dev_kfree_skb_irq(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001329
1330 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * .. and advance to the next buffer.
1332 */
1333
1334 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /*
1339 * Decide if we should wake up the protocol or not.
1340 * Other drivers seem to do this when we reach a low
1341 * watermark on the transmit queue.
1342 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 netif_wake_queue(d->sbdma_eth->sbm_dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350
1351
1352/**********************************************************************
1353 * SBMAC_INITCTX(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001354 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * Initialize an Ethernet context structure - this is called
1356 * once per MAC on the 1250. Memory is allocated here, so don't
1357 * call it again from inside the ioctl routines that bring the
1358 * interface up/down
Ralf Baechle74b02472005-10-19 15:40:02 +01001359 *
1360 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * s - sbmac context structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001362 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * Return value:
1364 * 0
1365 ********************************************************************* */
1366
1367static int sbmac_initctx(struct sbmac_softc *s)
1368{
Ralf Baechle74b02472005-10-19 15:40:02 +01001369
1370 /*
1371 * figure out the addresses of some ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1375 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1376 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1377 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1378 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1379 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1380 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1381 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1382
1383 s->sbm_phys[0] = 1;
1384 s->sbm_phys[1] = 0;
1385
1386 s->sbm_phy_oldbmsr = 0;
1387 s->sbm_phy_oldanlpar = 0;
1388 s->sbm_phy_oldk1stsr = 0;
1389 s->sbm_phy_oldlinkstat = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /*
1392 * Initialize the DMA channels. Right now, only one per MAC is used
1393 * Note: Only do this _once_, as it allocates memory from the kernel!
1394 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1397 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
Ralf Baechle74b02472005-10-19 15:40:02 +01001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /*
1400 * initial state is OFF
1401 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
1406 * Initial speed is (XXX TEMP) 10MBit/s HDX no FC
1407 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 s->sbm_speed = sbmac_speed_10;
1410 s->sbm_duplex = sbmac_duplex_half;
1411 s->sbm_fc = sbmac_fc_disabled;
Ralf Baechle74b02472005-10-19 15:40:02 +01001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return 0;
1414}
1415
1416
1417static void sbdma_uninitctx(struct sbmacdma_s *d)
1418{
1419 if (d->sbdma_dscrtable) {
1420 kfree(d->sbdma_dscrtable);
1421 d->sbdma_dscrtable = NULL;
1422 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (d->sbdma_ctxtable) {
1425 kfree(d->sbdma_ctxtable);
1426 d->sbdma_ctxtable = NULL;
1427 }
1428}
1429
1430
1431static void sbmac_uninitctx(struct sbmac_softc *sc)
1432{
1433 sbdma_uninitctx(&(sc->sbm_txdma));
1434 sbdma_uninitctx(&(sc->sbm_rxdma));
1435}
1436
1437
1438/**********************************************************************
1439 * SBMAC_CHANNEL_START(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001440 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * Start packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001442 *
1443 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001445 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 * Return value:
1447 * nothing
1448 ********************************************************************* */
1449
1450static void sbmac_channel_start(struct sbmac_softc *s)
1451{
1452 uint64_t reg;
Ralf Baechle20399732005-10-19 15:39:05 +01001453 volatile void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 uint64_t cfg,fifo,framecfg;
1455 int idx, th_value;
Ralf Baechle74b02472005-10-19 15:40:02 +01001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /*
1458 * Don't do this if running
1459 */
1460
1461 if (s->sbm_state == sbmac_state_on)
1462 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * Bring the controller out of reset, but leave it off.
1466 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001467
Ralf Baechle20399732005-10-19 15:39:05 +01001468 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /*
1471 * Ignore all received packets
1472 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001473
Ralf Baechle20399732005-10-19 15:39:05 +01001474 __raw_writeq(0, s->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001475
1476 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 * Calculate values for various control registers.
1478 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 cfg = M_MAC_RETRY_EN |
Ralf Baechle74b02472005-10-19 15:40:02 +01001481 M_MAC_TX_HOLD_SOP_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 V_MAC_TX_PAUSE_CNT_16K |
1483 M_MAC_AP_STAT_EN |
1484 M_MAC_FAST_SYNC |
1485 M_MAC_SS_EN |
1486 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001487
1488 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1490 * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1491 * Use a larger RD_THRSH for gigabit
1492 */
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001493 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 th_value = 28;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001495 else
1496 th_value = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1499 ((s->sbm_speed == sbmac_speed_1000)
1500 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1501 V_MAC_TX_RL_THRSH(4) |
1502 V_MAC_RX_PL_THRSH(4) |
1503 V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
1504 V_MAC_RX_PL_THRSH(4) |
1505 V_MAC_RX_RL_THRSH(8) |
1506 0;
1507
1508 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1509 V_MAC_MAX_FRAMESZ_DEFAULT |
1510 V_MAC_BACKOFF_SEL(1);
1511
1512 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001513 * Clear out the hash address map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 port = s->sbm_base + R_MAC_HASH_BASE;
1517 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001518 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 port += sizeof(uint64_t);
1520 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * Clear out the exact-match table
1524 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 port = s->sbm_base + R_MAC_ADDR_BASE;
1527 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001528 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 port += sizeof(uint64_t);
1530 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /*
1533 * Clear out the DMA Channel mapping table registers
1534 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 port = s->sbm_base + R_MAC_CHUP0_BASE;
1537 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001538 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 port += sizeof(uint64_t);
1540 }
1541
1542
1543 port = s->sbm_base + R_MAC_CHLO0_BASE;
1544 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001545 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 port += sizeof(uint64_t);
1547 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /*
1550 * Program the hardware address. It goes into the hardware-address
1551 * register as well as the first filter register.
1552 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 reg = sbmac_addr2reg(s->sbm_hwaddr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 port = s->sbm_base + R_MAC_ADDR_BASE;
Ralf Baechle20399732005-10-19 15:39:05 +01001557 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1559
1560#ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
1561 /*
1562 * Pass1 SOCs do not receive packets addressed to the
1563 * destination address in the R_MAC_ETHERNET_ADDR register.
1564 * Set the value to zero.
1565 */
Ralf Baechle20399732005-10-19 15:39:05 +01001566 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567#else
Ralf Baechle20399732005-10-19 15:39:05 +01001568 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * Set the receive filter for no packets, and write values
1573 * to the various config registers
1574 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001575
Ralf Baechle20399732005-10-19 15:39:05 +01001576 __raw_writeq(0, s->sbm_rxfilter);
1577 __raw_writeq(0, s->sbm_imr);
1578 __raw_writeq(framecfg, s->sbm_framecfg);
1579 __raw_writeq(fifo, s->sbm_fifocfg);
1580 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /*
1583 * Initialize DMA channels (rings should be ok now)
1584 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1587 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
Ralf Baechle74b02472005-10-19 15:40:02 +01001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * Configure the speed, duplex, and flow control
1591 */
1592
1593 sbmac_set_speed(s,s->sbm_speed);
1594 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
Ralf Baechle74b02472005-10-19 15:40:02 +01001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /*
1597 * Fill the receive ring
1598 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 sbdma_fillring(&(s->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001601
1602 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * Turn on the rest of the bits in the enable register
Ralf Baechle74b02472005-10-19 15:40:02 +01001604 */
1605
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001606#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1607 __raw_writeq(M_MAC_RXDMA_EN0 |
1608 M_MAC_TXDMA_EN0, s->sbm_macenable);
1609#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Ralf Baechle20399732005-10-19 15:39:05 +01001610 __raw_writeq(M_MAC_RXDMA_EN0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 M_MAC_TXDMA_EN0 |
1612 M_MAC_RX_ENABLE |
Ralf Baechle20399732005-10-19 15:39:05 +01001613 M_MAC_TX_ENABLE, s->sbm_macenable);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001614#else
1615#error invalid SiByte MAC configuation
1616#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618#ifdef CONFIG_SBMAC_COALESCE
1619 /*
1620 * Accept any TX interrupt and EOP count/timer RX interrupts on ch 0
1621 */
Ralf Baechle20399732005-10-19 15:39:05 +01001622 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1623 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624#else
1625 /*
1626 * Accept any kind of interrupt on TX and RX DMA channel 0
1627 */
Ralf Baechle20399732005-10-19 15:39:05 +01001628 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1629 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001633 * Enable receiving unicasts and broadcasts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001635
1636 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1637
1638 /*
1639 * we're running now.
1640 */
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 s->sbm_state = sbmac_state_on;
Ralf Baechle74b02472005-10-19 15:40:02 +01001643
1644 /*
1645 * Program multicast addresses
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 sbmac_setmulti(s);
Ralf Baechle74b02472005-10-19 15:40:02 +01001649
1650 /*
1651 * If channel was in promiscuous mode before, turn that on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (s->sbm_devflags & IFF_PROMISC) {
1655 sbmac_promiscuous_mode(s,1);
1656 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660
1661/**********************************************************************
1662 * SBMAC_CHANNEL_STOP(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001663 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 * Stop packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001665 *
1666 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001668 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * Return value:
1670 * nothing
1671 ********************************************************************* */
1672
1673static void sbmac_channel_stop(struct sbmac_softc *s)
1674{
1675 /* don't do this if already stopped */
Ralf Baechle74b02472005-10-19 15:40:02 +01001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (s->sbm_state == sbmac_state_off)
1678 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /* don't accept any packets, disable all interrupts */
Ralf Baechle74b02472005-10-19 15:40:02 +01001681
Ralf Baechle20399732005-10-19 15:39:05 +01001682 __raw_writeq(0, s->sbm_rxfilter);
1683 __raw_writeq(0, s->sbm_imr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* Turn off ticker */
Ralf Baechle74b02472005-10-19 15:40:02 +01001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* XXX */
Ralf Baechle74b02472005-10-19 15:40:02 +01001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /* turn off receiver and transmitter */
Ralf Baechle74b02472005-10-19 15:40:02 +01001690
Ralf Baechle20399732005-10-19 15:39:05 +01001691 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /* We're stopped now. */
Ralf Baechle74b02472005-10-19 15:40:02 +01001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * Stop DMA channels (rings should be ok now)
1699 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 sbdma_channel_stop(&(s->sbm_rxdma));
1702 sbdma_channel_stop(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /* Empty the receive and transmit rings */
Ralf Baechle74b02472005-10-19 15:40:02 +01001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 sbdma_emptyring(&(s->sbm_rxdma));
1707 sbdma_emptyring(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711/**********************************************************************
1712 * SBMAC_SET_CHANNEL_STATE(state)
Ralf Baechle74b02472005-10-19 15:40:02 +01001713 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * Set the channel's state ON or OFF
Ralf Baechle74b02472005-10-19 15:40:02 +01001715 *
1716 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * state - new state
Ralf Baechle74b02472005-10-19 15:40:02 +01001718 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 * Return value:
1720 * old state
1721 ********************************************************************* */
1722static sbmac_state_t sbmac_set_channel_state(struct sbmac_softc *sc,
1723 sbmac_state_t state)
1724{
1725 sbmac_state_t oldstate = sc->sbm_state;
Ralf Baechle74b02472005-10-19 15:40:02 +01001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * If same as previous state, return
1729 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (state == oldstate) {
1732 return oldstate;
1733 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001736 * If new state is ON, turn channel on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (state == sbmac_state_on) {
1740 sbmac_channel_start(sc);
1741 }
1742 else {
1743 sbmac_channel_stop(sc);
1744 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * Return previous state
1748 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return oldstate;
1751}
1752
1753
1754/**********************************************************************
1755 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001756 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * Turn on or off promiscuous mode
Ralf Baechle74b02472005-10-19 15:40:02 +01001758 *
1759 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * sc - softc
1761 * onoff - 1 to turn on, 0 to turn off
Ralf Baechle74b02472005-10-19 15:40:02 +01001762 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * Return value:
1764 * nothing
1765 ********************************************************************* */
1766
1767static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1768{
1769 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (sc->sbm_state != sbmac_state_on)
1772 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (onoff) {
Ralf Baechle20399732005-10-19 15:39:05 +01001775 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 reg |= M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001777 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 else {
Ralf Baechle20399732005-10-19 15:39:05 +01001780 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 reg &= ~M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001782 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784}
1785
1786/**********************************************************************
1787 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001788 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 * Set the iphdr offset as 15 assuming ethernet encapsulation
Ralf Baechle74b02472005-10-19 15:40:02 +01001790 *
1791 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01001793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 * Return value:
1795 * nothing
1796 ********************************************************************* */
1797
1798static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1799{
1800 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /* Hard code the off set to 15 for now */
Ralf Baechle20399732005-10-19 15:39:05 +01001803 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
Ralf Baechle20399732005-10-19 15:39:05 +01001805 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001806
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001807 /* BCM1250 pass1 didn't have hardware checksum. Everything
1808 later does. */
1809 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 sc->rx_hw_checksum = DISABLE;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001811 } else {
1812 sc->rx_hw_checksum = ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814}
1815
1816
1817/**********************************************************************
1818 * SBMAC_ADDR2REG(ptr)
Ralf Baechle74b02472005-10-19 15:40:02 +01001819 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * Convert six bytes into the 64-bit register value that
1821 * we typically write into the SBMAC's address/mcast registers
Ralf Baechle74b02472005-10-19 15:40:02 +01001822 *
1823 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 * ptr - pointer to 6 bytes
Ralf Baechle74b02472005-10-19 15:40:02 +01001825 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 * Return value:
1827 * register value
1828 ********************************************************************* */
1829
1830static uint64_t sbmac_addr2reg(unsigned char *ptr)
1831{
1832 uint64_t reg = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ptr += 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01001835
1836 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001838 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001840 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001842 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001844 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001846 reg |= (uint64_t) *(--ptr);
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return reg;
1849}
1850
1851
1852/**********************************************************************
1853 * SBMAC_SET_SPEED(s,speed)
Ralf Baechle74b02472005-10-19 15:40:02 +01001854 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 * Configure LAN speed for the specified MAC.
1856 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001857 *
1858 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * s - sbmac structure
1860 * speed - speed to set MAC to (see sbmac_speed_t enum)
Ralf Baechle74b02472005-10-19 15:40:02 +01001861 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * Return value:
1863 * 1 if successful
1864 * 0 indicates invalid parameters
1865 ********************************************************************* */
1866
1867static int sbmac_set_speed(struct sbmac_softc *s,sbmac_speed_t speed)
1868{
1869 uint64_t cfg;
1870 uint64_t framecfg;
1871
1872 /*
1873 * Save new current values
1874 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 s->sbm_speed = speed;
Ralf Baechle74b02472005-10-19 15:40:02 +01001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (s->sbm_state == sbmac_state_on)
1879 return 0; /* save for next restart */
1880
1881 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001882 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001884
Ralf Baechle20399732005-10-19 15:39:05 +01001885 cfg = __raw_readq(s->sbm_maccfg);
1886 framecfg = __raw_readq(s->sbm_framecfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Mask out the stuff we want to change
1890 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1893 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1894 M_MAC_SLOT_SIZE);
Ralf Baechle74b02472005-10-19 15:40:02 +01001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /*
1897 * Now add in the new bits
1898 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 switch (speed) {
1901 case sbmac_speed_10:
1902 framecfg |= V_MAC_IFG_RX_10 |
1903 V_MAC_IFG_TX_10 |
1904 K_MAC_IFG_THRSH_10 |
1905 V_MAC_SLOT_SIZE_10;
1906 cfg |= V_MAC_SPEED_SEL_10MBPS;
1907 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 case sbmac_speed_100:
1910 framecfg |= V_MAC_IFG_RX_100 |
1911 V_MAC_IFG_TX_100 |
1912 V_MAC_IFG_THRSH_100 |
1913 V_MAC_SLOT_SIZE_100;
1914 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1915 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 case sbmac_speed_1000:
1918 framecfg |= V_MAC_IFG_RX_1000 |
1919 V_MAC_IFG_TX_1000 |
1920 V_MAC_IFG_THRSH_1000 |
1921 V_MAC_SLOT_SIZE_1000;
1922 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1923 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 case sbmac_speed_auto: /* XXX not implemented */
1926 /* fall through */
1927 default:
1928 return 0;
1929 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001932 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001934
Ralf Baechle20399732005-10-19 15:39:05 +01001935 __raw_writeq(framecfg, s->sbm_framecfg);
1936 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 1;
1939}
1940
1941/**********************************************************************
1942 * SBMAC_SET_DUPLEX(s,duplex,fc)
Ralf Baechle74b02472005-10-19 15:40:02 +01001943 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 * Set Ethernet duplex and flow control options for this MAC
1945 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001946 *
1947 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * s - sbmac structure
1949 * duplex - duplex setting (see sbmac_duplex_t)
1950 * fc - flow control setting (see sbmac_fc_t)
Ralf Baechle74b02472005-10-19 15:40:02 +01001951 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 * Return value:
1953 * 1 if ok
1954 * 0 if an invalid parameter combination was specified
1955 ********************************************************************* */
1956
1957static int sbmac_set_duplex(struct sbmac_softc *s,sbmac_duplex_t duplex,sbmac_fc_t fc)
1958{
1959 uint64_t cfg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
1962 * Save new current values
1963 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 s->sbm_duplex = duplex;
1966 s->sbm_fc = fc;
Ralf Baechle74b02472005-10-19 15:40:02 +01001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (s->sbm_state == sbmac_state_on)
1969 return 0; /* save for next restart */
Ralf Baechle74b02472005-10-19 15:40:02 +01001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001972 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001974
Ralf Baechle20399732005-10-19 15:39:05 +01001975 cfg = __raw_readq(s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /*
1978 * Mask off the stuff we're about to change
1979 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
Ralf Baechle74b02472005-10-19 15:40:02 +01001982
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 switch (duplex) {
1985 case sbmac_duplex_half:
1986 switch (fc) {
1987 case sbmac_fc_disabled:
1988 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1989 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 case sbmac_fc_collision:
1992 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1993 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 case sbmac_fc_carrier:
1996 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1997 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 case sbmac_fc_auto: /* XXX not implemented */
Ralf Baechle74b02472005-10-19 15:40:02 +01002000 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 case sbmac_fc_frame: /* not valid in half duplex */
2002 default: /* invalid selection */
2003 return 0;
2004 }
2005 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 case sbmac_duplex_full:
2008 switch (fc) {
2009 case sbmac_fc_disabled:
2010 cfg |= V_MAC_FC_CMD_DISABLED;
2011 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 case sbmac_fc_frame:
2014 cfg |= V_MAC_FC_CMD_ENABLED;
2015 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 case sbmac_fc_collision: /* not valid in full duplex */
2018 case sbmac_fc_carrier: /* not valid in full duplex */
2019 case sbmac_fc_auto: /* XXX not implemented */
Ralf Baechle74b02472005-10-19 15:40:02 +01002020 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 default:
2022 return 0;
2023 }
2024 break;
2025 case sbmac_duplex_auto:
2026 /* XXX not implemented */
2027 break;
2028 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002031 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002033
Ralf Baechle20399732005-10-19 15:39:05 +01002034 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return 1;
2037}
2038
2039
2040
2041
2042/**********************************************************************
2043 * SBMAC_INTR()
Ralf Baechle74b02472005-10-19 15:40:02 +01002044 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 * Interrupt handler for MAC interrupts
Ralf Baechle74b02472005-10-19 15:40:02 +01002046 *
2047 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * MAC structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002049 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 * Return value:
2051 * nothing
2052 ********************************************************************* */
2053static irqreturn_t sbmac_intr(int irq,void *dev_instance,struct pt_regs *rgs)
2054{
2055 struct net_device *dev = (struct net_device *) dev_instance;
2056 struct sbmac_softc *sc = netdev_priv(dev);
2057 uint64_t isr;
2058 int handled = 0;
2059
2060 for (;;) {
Ralf Baechle74b02472005-10-19 15:40:02 +01002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * Read the ISR (this clears the bits in the real
2064 * register, except for counter addr)
2065 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002066
Ralf Baechle20399732005-10-19 15:39:05 +01002067 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
Ralf Baechle74b02472005-10-19 15:40:02 +01002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (isr == 0)
2070 break;
2071
2072 handled = 1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
2075 * Transmits on channel 0
2076 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0)) {
2079 sbdma_tx_process(sc,&(sc->sbm_txdma));
2080 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * Receives on channel 0
2084 */
2085
2086 /*
2087 * It's important to test all the bits (or at least the
2088 * EOP_SEEN bit) when deciding to do the RX process
2089 * particularly when coalescing, to make sure we
2090 * take care of the following:
2091 *
2092 * If you have some packets waiting (have been received
2093 * but no interrupt) and get a TX interrupt before
2094 * the RX timer or counter expires, reading the ISR
2095 * above will clear the timer and counter, and you
2096 * won't get another interrupt until a packet shows
2097 * up to start the timer again. Testing
2098 * EOP_SEEN here takes care of this case.
2099 * (EOP_SEEN is part of M_MAC_INT_CHANNEL << S_MAC_RX_CH0)
2100 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002101
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
2104 sbdma_rx_process(sc,&(sc->sbm_rxdma));
2105 }
2106 }
2107 return IRQ_RETVAL(handled);
2108}
2109
2110
2111/**********************************************************************
2112 * SBMAC_START_TX(skb,dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002113 *
2114 * Start output on the specified interface. Basically, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * queue as many buffers as we can until the ring fills up, or
2116 * we run off the end of the queue, whichever comes first.
Ralf Baechle74b02472005-10-19 15:40:02 +01002117 *
2118 * Input parameters:
2119 *
2120 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * Return value:
2122 * nothing
2123 ********************************************************************* */
2124static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2125{
2126 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* lock eth irq */
2129 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002132 * Put the buffer on the transmit ring. If we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 * don't have room, stop the queue.
2134 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2137 /* XXX save skb that we could not send */
2138 netif_stop_queue(dev);
2139 spin_unlock_irq(&sc->sbm_lock);
2140
2141 return 1;
2142 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 dev->trans_start = jiffies;
Ralf Baechle74b02472005-10-19 15:40:02 +01002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 spin_unlock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return 0;
2149}
2150
2151/**********************************************************************
2152 * SBMAC_SETMULTI(sc)
Ralf Baechle74b02472005-10-19 15:40:02 +01002153 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 * Reprogram the multicast table into the hardware, given
2155 * the list of multicasts associated with the interface
2156 * structure.
Ralf Baechle74b02472005-10-19 15:40:02 +01002157 *
2158 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01002160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 * Return value:
2162 * nothing
2163 ********************************************************************* */
2164
2165static void sbmac_setmulti(struct sbmac_softc *sc)
2166{
2167 uint64_t reg;
Ralf Baechle20399732005-10-19 15:39:05 +01002168 volatile void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int idx;
2170 struct dev_mc_list *mclist;
2171 struct net_device *dev = sc->sbm_dev;
Ralf Baechle74b02472005-10-19 15:40:02 +01002172
2173 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 * Clear out entire multicast table. We do this by nuking
2175 * the entire hash table and all the direct matches except
Ralf Baechle74b02472005-10-19 15:40:02 +01002176 * the first one, which is used for our station address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2180 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002181 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2185 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002186 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
2190 * Clear the filter to say we don't want any multicasts.
2191 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002192
Ralf Baechle20399732005-10-19 15:39:05 +01002193 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002195 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (dev->flags & IFF_ALLMULTI) {
Ralf Baechle74b02472005-10-19 15:40:02 +01002198 /*
2199 * Enable ALL multicasts. Do this by inverting the
2200 * multicast enable bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 */
Ralf Baechle20399732005-10-19 15:39:05 +01002202 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002204 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Ralf Baechle74b02472005-10-19 15:40:02 +01002208
2209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 * Progam new multicast entries. For now, only use the
2211 * perfect filter. In the future we'll need to use the
2212 * hash filter if the perfect filter overflows
2213 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /* XXX only using perfect filter for now, need to use hash
2216 * XXX if the table overflows */
Ralf Baechle74b02472005-10-19 15:40:02 +01002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 idx = 1; /* skip station address */
2219 mclist = dev->mc_list;
2220 while (mclist && (idx < MAC_ADDR_COUNT)) {
2221 reg = sbmac_addr2reg(mclist->dmi_addr);
2222 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002223 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 idx++;
2225 mclist = mclist->next;
2226 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002227
2228 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 * Enable the "accept multicast bits" if we programmed at least one
Ralf Baechle74b02472005-10-19 15:40:02 +01002230 * multicast.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (idx > 1) {
Ralf Baechle20399732005-10-19 15:39:05 +01002234 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 reg |= M_MAC_MCAST_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01002236 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238}
2239
2240
2241
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002242#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/**********************************************************************
2244 * SBMAC_PARSE_XDIGIT(str)
Ralf Baechle74b02472005-10-19 15:40:02 +01002245 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 * Parse a hex digit, returning its value
Ralf Baechle74b02472005-10-19 15:40:02 +01002247 *
2248 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 * str - character
Ralf Baechle74b02472005-10-19 15:40:02 +01002250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 * Return value:
2252 * hex value, or -1 if invalid
2253 ********************************************************************* */
2254
2255static int sbmac_parse_xdigit(char str)
2256{
2257 int digit;
Ralf Baechle74b02472005-10-19 15:40:02 +01002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if ((str >= '0') && (str <= '9'))
2260 digit = str - '0';
2261 else if ((str >= 'a') && (str <= 'f'))
2262 digit = str - 'a' + 10;
2263 else if ((str >= 'A') && (str <= 'F'))
2264 digit = str - 'A' + 10;
2265 else
2266 return -1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return digit;
2269}
2270
2271/**********************************************************************
2272 * SBMAC_PARSE_HWADDR(str,hwaddr)
Ralf Baechle74b02472005-10-19 15:40:02 +01002273 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
2275 * Ethernet address.
Ralf Baechle74b02472005-10-19 15:40:02 +01002276 *
2277 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * str - string
2279 * hwaddr - pointer to hardware address
Ralf Baechle74b02472005-10-19 15:40:02 +01002280 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * Return value:
2282 * 0 if ok, else -1
2283 ********************************************************************* */
2284
2285static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr)
2286{
2287 int digit1,digit2;
2288 int idx = 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01002289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 while (*str && (idx > 0)) {
2291 digit1 = sbmac_parse_xdigit(*str);
2292 if (digit1 < 0)
2293 return -1;
2294 str++;
2295 if (!*str)
2296 return -1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if ((*str == ':') || (*str == '-')) {
2299 digit2 = digit1;
2300 digit1 = 0;
2301 }
2302 else {
2303 digit2 = sbmac_parse_xdigit(*str);
2304 if (digit2 < 0)
2305 return -1;
2306 str++;
2307 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 *hwaddr++ = (digit1 << 4) | digit2;
2310 idx--;
Ralf Baechle74b02472005-10-19 15:40:02 +01002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (*str == '-')
2313 str++;
2314 if (*str == ':')
2315 str++;
2316 }
2317 return 0;
2318}
2319#endif
2320
2321static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2322{
2323 if (new_mtu > ENET_PACKET_SIZE)
2324 return -EINVAL;
2325 _dev->mtu = new_mtu;
2326 printk(KERN_INFO "changing the mtu to %d\n", new_mtu);
2327 return 0;
2328}
2329
2330/**********************************************************************
2331 * SBMAC_INIT(dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002332 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 * Attach routine - init hardware and hook ourselves into linux
Ralf Baechle74b02472005-10-19 15:40:02 +01002334 *
2335 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 * dev - net_device structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002337 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 * Return value:
2339 * status
2340 ********************************************************************* */
2341
2342static int sbmac_init(struct net_device *dev, int idx)
2343{
2344 struct sbmac_softc *sc;
2345 unsigned char *eaddr;
2346 uint64_t ea_reg;
2347 int i;
2348 int err;
Ralf Baechle74b02472005-10-19 15:40:02 +01002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /* Determine controller base address */
Ralf Baechle74b02472005-10-19 15:40:02 +01002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 sc->sbm_base = IOADDR(dev->base_addr);
2355 sc->sbm_dev = dev;
2356 sc->sbe_idx = idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01002357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 eaddr = sc->sbm_hwaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +01002359
2360 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 * Read the ethernet address. The firwmare left this programmed
2362 * for us in the ethernet address register for each mac.
2363 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002364
Ralf Baechle20399732005-10-19 15:39:05 +01002365 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2366 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 for (i = 0; i < 6; i++) {
2368 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2369 ea_reg >>= 8;
2370 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 for (i = 0; i < 6; i++) {
2373 dev->dev_addr[i] = eaddr[i];
2374 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002375
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002378 * Init packet size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;
2382
Ralf Baechle74b02472005-10-19 15:40:02 +01002383 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 * Initialize context (get pointers to registers and stuff), then
2385 * allocate the memory for the descriptor tables.
2386 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 sbmac_initctx(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 /*
2391 * Set up Linux device callins
2392 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 spin_lock_init(&(sc->sbm_lock));
Ralf Baechle74b02472005-10-19 15:40:02 +01002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 dev->open = sbmac_open;
2397 dev->hard_start_xmit = sbmac_start_tx;
2398 dev->stop = sbmac_close;
2399 dev->get_stats = sbmac_get_stats;
2400 dev->set_multicast_list = sbmac_set_rx_mode;
2401 dev->do_ioctl = sbmac_mii_ioctl;
2402 dev->tx_timeout = sbmac_tx_timeout;
2403 dev->watchdog_timeo = TX_TIMEOUT;
2404
2405 dev->change_mtu = sb1250_change_mtu;
2406
2407 /* This is needed for PASS2 for Rx H/W checksum feature */
2408 sbmac_set_iphdr_offset(sc);
2409
2410 err = register_netdev(dev);
2411 if (err)
2412 goto out_uninit;
2413
Ralf Baechlef567ef92005-10-10 14:50:24 +01002414 if (sc->rx_hw_checksum == ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 printk(KERN_INFO "%s: enabling TCP rcv checksum\n",
2416 sc->sbm_dev->name);
2417 }
2418
2419 /*
2420 * Display Ethernet address (this is called during the config
2421 * process so we need to finish off the config message that
2422 * was being displayed)
2423 */
2424 printk(KERN_INFO
Ralf Baechle74b02472005-10-19 15:40:02 +01002425 "%s: SiByte Ethernet at 0x%08lX, address: %02X:%02X:%02X:%02X:%02X:%02X\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 dev->name, dev->base_addr,
2427 eaddr[0],eaddr[1],eaddr[2],eaddr[3],eaddr[4],eaddr[5]);
Ralf Baechle74b02472005-10-19 15:40:02 +01002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 return 0;
2431
2432out_uninit:
2433 sbmac_uninitctx(sc);
2434
2435 return err;
2436}
2437
2438
2439static int sbmac_open(struct net_device *dev)
2440{
2441 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (debug > 1) {
2444 printk(KERN_DEBUG "%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2445 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002446
2447 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 * map/route interrupt (clear status first, in case something
2449 * weird is pending; we haven't initialized the mac registers
2450 * yet)
2451 */
2452
Ralf Baechle20399732005-10-19 15:39:05 +01002453 __raw_readq(sc->sbm_isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (request_irq(dev->irq, &sbmac_intr, SA_SHIRQ, dev->name, dev))
2455 return -EBUSY;
2456
2457 /*
Ralf Baechle59b81822005-10-20 12:01:28 +01002458 * Probe phy address
2459 */
2460
2461 if(sbmac_mii_probe(dev) == -1) {
2462 printk("%s: failed to probe PHY.\n", dev->name);
2463 return -EINVAL;
2464 }
2465
2466 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002467 * Configure default speed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 */
2469
2470 sbmac_mii_poll(sc,noisy_mii);
Ralf Baechle74b02472005-10-19 15:40:02 +01002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * Turn on the channel
2474 */
2475
2476 sbmac_set_channel_state(sc,sbmac_state_on);
Ralf Baechle74b02472005-10-19 15:40:02 +01002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /*
2479 * XXX Station address is in dev->dev_addr
2480 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (dev->if_port == 0)
Ralf Baechle74b02472005-10-19 15:40:02 +01002483 dev->if_port = 0;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 netif_start_queue(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 sbmac_set_rx_mode(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /* Set the timer to check for link beat. */
2490 init_timer(&sc->sbm_timer);
2491 sc->sbm_timer.expires = jiffies + 2 * HZ/100;
2492 sc->sbm_timer.data = (unsigned long)dev;
2493 sc->sbm_timer.function = &sbmac_timer;
2494 add_timer(&sc->sbm_timer);
Ralf Baechle74b02472005-10-19 15:40:02 +01002495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return 0;
2497}
2498
Ralf Baechle59b81822005-10-20 12:01:28 +01002499static int sbmac_mii_probe(struct net_device *dev)
2500{
2501 int i;
2502 struct sbmac_softc *s = netdev_priv(dev);
2503 u16 bmsr, id1, id2;
2504 u32 vendor, device;
2505
2506 for (i=1; i<31; i++) {
2507 bmsr = sbmac_mii_read(s, i, MII_BMSR);
2508 if (bmsr != 0) {
2509 s->sbm_phys[0] = i;
2510 id1 = sbmac_mii_read(s, i, MII_PHYIDR1);
2511 id2 = sbmac_mii_read(s, i, MII_PHYIDR2);
2512 vendor = ((u32)id1 << 6) | ((id2 >> 10) & 0x3f);
2513 device = (id2 >> 4) & 0x3f;
2514
2515 printk(KERN_INFO "%s: found phy %d, vendor %06x part %02x\n",
2516 dev->name, i, vendor, device);
2517 return i;
2518 }
2519 }
2520 return -1;
2521}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523
2524static int sbmac_mii_poll(struct sbmac_softc *s,int noisy)
2525{
2526 int bmsr,bmcr,k1stsr,anlpar;
2527 int chg;
2528 char buffer[100];
2529 char *p = buffer;
2530
2531 /* Read the mode status and mode control registers. */
2532 bmsr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMSR);
2533 bmcr = sbmac_mii_read(s,s->sbm_phys[0],MII_BMCR);
2534
2535 /* get the link partner status */
2536 anlpar = sbmac_mii_read(s,s->sbm_phys[0],MII_ANLPAR);
2537
2538 /* if supported, read the 1000baseT register */
2539 if (bmsr & BMSR_1000BT_XSR) {
2540 k1stsr = sbmac_mii_read(s,s->sbm_phys[0],MII_K1STSR);
2541 }
2542 else {
2543 k1stsr = 0;
2544 }
2545
2546 chg = 0;
2547
2548 if ((bmsr & BMSR_LINKSTAT) == 0) {
2549 /*
2550 * If link status is down, clear out old info so that when
2551 * it comes back up it will force us to reconfigure speed
2552 */
2553 s->sbm_phy_oldbmsr = 0;
2554 s->sbm_phy_oldanlpar = 0;
2555 s->sbm_phy_oldk1stsr = 0;
2556 return 0;
2557 }
2558
2559 if ((s->sbm_phy_oldbmsr != bmsr) ||
2560 (s->sbm_phy_oldanlpar != anlpar) ||
2561 (s->sbm_phy_oldk1stsr != k1stsr)) {
2562 if (debug > 1) {
2563 printk(KERN_DEBUG "%s: bmsr:%x/%x anlpar:%x/%x k1stsr:%x/%x\n",
2564 s->sbm_dev->name,
2565 s->sbm_phy_oldbmsr,bmsr,
2566 s->sbm_phy_oldanlpar,anlpar,
2567 s->sbm_phy_oldk1stsr,k1stsr);
2568 }
2569 s->sbm_phy_oldbmsr = bmsr;
2570 s->sbm_phy_oldanlpar = anlpar;
2571 s->sbm_phy_oldk1stsr = k1stsr;
2572 chg = 1;
2573 }
2574
2575 if (chg == 0)
2576 return 0;
2577
2578 p += sprintf(p,"Link speed: ");
2579
2580 if (k1stsr & K1STSR_LP1KFD) {
2581 s->sbm_speed = sbmac_speed_1000;
2582 s->sbm_duplex = sbmac_duplex_full;
2583 s->sbm_fc = sbmac_fc_frame;
2584 p += sprintf(p,"1000BaseT FDX");
2585 }
2586 else if (k1stsr & K1STSR_LP1KHD) {
2587 s->sbm_speed = sbmac_speed_1000;
2588 s->sbm_duplex = sbmac_duplex_half;
2589 s->sbm_fc = sbmac_fc_disabled;
2590 p += sprintf(p,"1000BaseT HDX");
2591 }
2592 else if (anlpar & ANLPAR_TXFD) {
2593 s->sbm_speed = sbmac_speed_100;
2594 s->sbm_duplex = sbmac_duplex_full;
2595 s->sbm_fc = (anlpar & ANLPAR_PAUSE) ? sbmac_fc_frame : sbmac_fc_disabled;
2596 p += sprintf(p,"100BaseT FDX");
2597 }
2598 else if (anlpar & ANLPAR_TXHD) {
2599 s->sbm_speed = sbmac_speed_100;
2600 s->sbm_duplex = sbmac_duplex_half;
2601 s->sbm_fc = sbmac_fc_disabled;
2602 p += sprintf(p,"100BaseT HDX");
2603 }
2604 else if (anlpar & ANLPAR_10FD) {
2605 s->sbm_speed = sbmac_speed_10;
2606 s->sbm_duplex = sbmac_duplex_full;
2607 s->sbm_fc = sbmac_fc_frame;
2608 p += sprintf(p,"10BaseT FDX");
2609 }
2610 else if (anlpar & ANLPAR_10HD) {
2611 s->sbm_speed = sbmac_speed_10;
2612 s->sbm_duplex = sbmac_duplex_half;
2613 s->sbm_fc = sbmac_fc_collision;
2614 p += sprintf(p,"10BaseT HDX");
2615 }
2616 else {
2617 p += sprintf(p,"Unknown");
2618 }
2619
2620 if (noisy) {
2621 printk(KERN_INFO "%s: %s\n",s->sbm_dev->name,buffer);
2622 }
2623
2624 return 1;
2625}
2626
2627
2628static void sbmac_timer(unsigned long data)
2629{
2630 struct net_device *dev = (struct net_device *)data;
2631 struct sbmac_softc *sc = netdev_priv(dev);
2632 int next_tick = HZ;
2633 int mii_status;
2634
2635 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /* make IFF_RUNNING follow the MII status bit "Link established" */
2638 mii_status = sbmac_mii_read(sc, sc->sbm_phys[0], MII_BMSR);
Ralf Baechle74b02472005-10-19 15:40:02 +01002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if ( (mii_status & BMSR_LINKSTAT) != (sc->sbm_phy_oldlinkstat) ) {
2641 sc->sbm_phy_oldlinkstat = mii_status & BMSR_LINKSTAT;
2642 if (mii_status & BMSR_LINKSTAT) {
2643 netif_carrier_on(dev);
2644 }
2645 else {
Ralf Baechle74b02472005-10-19 15:40:02 +01002646 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 /*
2651 * Poll the PHY to see what speed we should be running at
2652 */
2653
2654 if (sbmac_mii_poll(sc,noisy_mii)) {
2655 if (sc->sbm_state != sbmac_state_off) {
2656 /*
2657 * something changed, restart the channel
2658 */
2659 if (debug > 1) {
2660 printk("%s: restarting channel because speed changed\n",
2661 sc->sbm_dev->name);
2662 }
2663 sbmac_channel_stop(sc);
2664 sbmac_channel_start(sc);
2665 }
2666 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 spin_unlock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 sc->sbm_timer.expires = jiffies + next_tick;
2671 add_timer(&sc->sbm_timer);
2672}
2673
2674
2675static void sbmac_tx_timeout (struct net_device *dev)
2676{
2677 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002680
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 dev->trans_start = jiffies;
2683 sc->sbm_stats.tx_errors++;
Ralf Baechle74b02472005-10-19 15:40:02 +01002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 spin_unlock_irq (&sc->sbm_lock);
2686
2687 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2688}
2689
2690
2691
2692
2693static struct net_device_stats *sbmac_get_stats(struct net_device *dev)
2694{
2695 struct sbmac_softc *sc = netdev_priv(dev);
2696 unsigned long flags;
Ralf Baechle74b02472005-10-19 15:40:02 +01002697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 spin_lock_irqsave(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* XXX update other stats here */
Ralf Baechle74b02472005-10-19 15:40:02 +01002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return &sc->sbm_stats;
2705}
2706
2707
2708
2709static void sbmac_set_rx_mode(struct net_device *dev)
2710{
2711 unsigned long flags;
2712 int msg_flag = 0;
2713 struct sbmac_softc *sc = netdev_priv(dev);
2714
2715 spin_lock_irqsave(&sc->sbm_lock, flags);
2716 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2717 /*
2718 * Promiscuous changed.
2719 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002720
2721 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /* Unconditionally log net taps. */
2723 msg_flag = 1;
2724 sbmac_promiscuous_mode(sc,1);
2725 }
2726 else {
2727 msg_flag = 2;
2728 sbmac_promiscuous_mode(sc,0);
2729 }
2730 }
2731 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (msg_flag) {
2734 printk(KERN_NOTICE "%s: Promiscuous mode %sabled.\n",
2735 dev->name,(msg_flag==1)?"en":"dis");
2736 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /*
2739 * Program the multicasts. Do this every time.
2740 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 sbmac_setmulti(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
2746static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2747{
2748 struct sbmac_softc *sc = netdev_priv(dev);
2749 u16 *data = (u16 *)&rq->ifr_ifru;
2750 unsigned long flags;
2751 int retval;
Ralf Baechle74b02472005-10-19 15:40:02 +01002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 spin_lock_irqsave(&sc->sbm_lock, flags);
2754 retval = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01002755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 switch(cmd) {
2757 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
2758 data[0] = sc->sbm_phys[0] & 0x1f;
2759 /* Fall Through */
2760 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
2761 data[3] = sbmac_mii_read(sc, data[0] & 0x1f, data[1] & 0x1f);
2762 break;
2763 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
2764 if (!capable(CAP_NET_ADMIN)) {
2765 retval = -EPERM;
2766 break;
2767 }
2768 if (debug > 1) {
2769 printk(KERN_DEBUG "%s: sbmac_mii_ioctl: write %02X %02X %02X\n",dev->name,
2770 data[0],data[1],data[2]);
2771 }
2772 sbmac_mii_write(sc, data[0] & 0x1f, data[1] & 0x1f, data[2]);
2773 break;
2774 default:
2775 retval = -EOPNOTSUPP;
2776 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2779 return retval;
2780}
2781
2782static int sbmac_close(struct net_device *dev)
2783{
2784 struct sbmac_softc *sc = netdev_priv(dev);
2785 unsigned long flags;
2786 int irq;
2787
2788 sbmac_set_channel_state(sc,sbmac_state_off);
2789
2790 del_timer_sync(&sc->sbm_timer);
2791
2792 spin_lock_irqsave(&sc->sbm_lock, flags);
2793
2794 netif_stop_queue(dev);
2795
2796 if (debug > 1) {
2797 printk(KERN_DEBUG "%s: Shutting down ethercard\n",dev->name);
2798 }
2799
2800 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2801
2802 irq = dev->irq;
2803 synchronize_irq(irq);
2804 free_irq(irq, dev);
2805
2806 sbdma_emptyring(&(sc->sbm_txdma));
2807 sbdma_emptyring(&(sc->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01002808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return 0;
2810}
2811
2812
2813
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002814#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815static void
2816sbmac_setup_hwaddr(int chan,char *addr)
2817{
2818 uint8_t eaddr[6];
2819 uint64_t val;
Ralf Baechle20399732005-10-19 15:39:05 +01002820 unsigned long port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 port = A_MAC_CHANNEL_BASE(chan);
2823 sbmac_parse_hwaddr(addr,eaddr);
2824 val = sbmac_addr2reg(eaddr);
Ralf Baechle20399732005-10-19 15:39:05 +01002825 __raw_writeq(val, IOADDR(port+R_MAC_ETHERNET_ADDR));
2826 val = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828#endif
2829
2830static struct net_device *dev_sbmac[MAX_UNITS];
2831
2832static int __init
2833sbmac_init_module(void)
2834{
2835 int idx;
2836 struct net_device *dev;
Ralf Baechle20399732005-10-19 15:39:05 +01002837 unsigned long port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 int chip_max_units;
Ralf Baechle74b02472005-10-19 15:40:02 +01002839
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002840 /* Set the number of available units based on the SOC type. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 switch (soc_type) {
2842 case K_SYS_SOC_TYPE_BCM1250:
2843 case K_SYS_SOC_TYPE_BCM1250_ALT:
2844 chip_max_units = 3;
2845 break;
2846 case K_SYS_SOC_TYPE_BCM1120:
2847 case K_SYS_SOC_TYPE_BCM1125:
2848 case K_SYS_SOC_TYPE_BCM1125H:
2849 case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
2850 chip_max_units = 2;
2851 break;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002852 case K_SYS_SOC_TYPE_BCM1x55:
2853 case K_SYS_SOC_TYPE_BCM1x80:
2854 chip_max_units = 4;
2855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 default:
2857 chip_max_units = 0;
2858 break;
2859 }
2860 if (chip_max_units > MAX_UNITS)
2861 chip_max_units = MAX_UNITS;
2862
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002863 /*
2864 * For bringup when not using the firmware, we can pre-fill
2865 * the MAC addresses using the environment variables
2866 * specified in this file (or maybe from the config file?)
2867 */
2868#ifdef SBMAC_ETH0_HWADDR
2869 if (chip_max_units > 0)
2870 sbmac_setup_hwaddr(0,SBMAC_ETH0_HWADDR);
2871#endif
2872#ifdef SBMAC_ETH1_HWADDR
2873 if (chip_max_units > 1)
2874 sbmac_setup_hwaddr(1,SBMAC_ETH1_HWADDR);
2875#endif
2876#ifdef SBMAC_ETH2_HWADDR
2877 if (chip_max_units > 2)
2878 sbmac_setup_hwaddr(2,SBMAC_ETH2_HWADDR);
2879#endif
2880#ifdef SBMAC_ETH3_HWADDR
2881 if (chip_max_units > 3)
2882 sbmac_setup_hwaddr(3,SBMAC_ETH3_HWADDR);
2883#endif
2884
2885 /*
2886 * Walk through the Ethernet controllers and find
2887 * those who have their MAC addresses set.
2888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 for (idx = 0; idx < chip_max_units; idx++) {
2890
2891 /*
2892 * This is the base address of the MAC.
2893 */
2894
2895 port = A_MAC_CHANNEL_BASE(idx);
2896
Ralf Baechle74b02472005-10-19 15:40:02 +01002897 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2899 * value for us by the firmware if we're going to use this MAC.
2900 * If we find a zero, skip this MAC.
2901 */
2902
Ralf Baechle20399732005-10-19 15:39:05 +01002903 sbmac_orig_hwaddr[idx] = __raw_readq(IOADDR(port+R_MAC_ETHERNET_ADDR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (sbmac_orig_hwaddr[idx] == 0) {
2905 printk(KERN_DEBUG "sbmac: not configuring MAC at "
2906 "%lx\n", port);
2907 continue;
2908 }
2909
2910 /*
2911 * Okay, cool. Initialize this MAC.
2912 */
2913
2914 dev = alloc_etherdev(sizeof(struct sbmac_softc));
Ralf Baechle74b02472005-10-19 15:40:02 +01002915 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return -ENOMEM; /* return ENOMEM */
2917
2918 printk(KERN_DEBUG "sbmac: configuring MAC at %lx\n", port);
2919
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002920 dev->irq = UNIT_INT(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 dev->base_addr = port;
2922 dev->mem_end = 0;
2923 if (sbmac_init(dev, idx)) {
2924 port = A_MAC_CHANNEL_BASE(idx);
Ralf Baechle20399732005-10-19 15:39:05 +01002925 __raw_writeq(sbmac_orig_hwaddr[idx], IOADDR(port+R_MAC_ETHERNET_ADDR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 free_netdev(dev);
2927 continue;
2928 }
2929 dev_sbmac[idx] = dev;
2930 }
2931 return 0;
2932}
2933
2934
2935static void __exit
2936sbmac_cleanup_module(void)
2937{
2938 struct net_device *dev;
2939 int idx;
2940
2941 for (idx = 0; idx < MAX_UNITS; idx++) {
2942 struct sbmac_softc *sc;
2943 dev = dev_sbmac[idx];
2944 if (!dev)
2945 continue;
2946
2947 sc = netdev_priv(dev);
2948 unregister_netdev(dev);
2949 sbmac_uninitctx(sc);
2950 free_netdev(dev);
2951 }
2952}
2953
2954module_init(sbmac_init_module);
2955module_exit(sbmac_cleanup_module);