blob: 564d4d7f855b163c3e937925e3e49a7e335c293c [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
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01003 * Copyright (c) 2006, 2007 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Ralf Baechle74b02472005-10-19 15:40:02 +010014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 *
20 * This driver is designed for the Broadcom SiByte SOC built-in
21 * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +010022 *
23 * Updated to the driver model and the PHY abstraction layer
24 * by Maciej W. Rozycki.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +010026
27#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/string.h>
31#include <linux/timer.h>
32#include <linux/errno.h>
33#include <linux/ioport.h>
34#include <linux/slab.h>
35#include <linux/interrupt.h>
36#include <linux/netdevice.h>
37#include <linux/etherdevice.h>
38#include <linux/skbuff.h>
39#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/bitops.h>
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +010041#include <linux/err.h>
42#include <linux/ethtool.h>
43#include <linux/mii.h>
44#include <linux/phy.h>
45#include <linux/platform_device.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/cache.h>
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +010048#include <asm/io.h>
49#include <asm/processor.h> /* Processor type for cache alignment. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* This is only here until the firmware is ready. In that case,
52 the firmware leaves the ethernet address in the register for us. */
53#ifdef CONFIG_SIBYTE_STANDALONE
54#define SBMAC_ETH0_HWADDR "40:00:00:00:01:00"
55#define SBMAC_ETH1_HWADDR "40:00:00:00:01:01"
56#define SBMAC_ETH2_HWADDR "40:00:00:00:01:02"
Ralf Baechlef90fdc32006-02-08 23:23:26 +000057#define SBMAC_ETH3_HWADDR "40:00:00:00:01:03"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
60
61/* These identify the driver base version and may not be removed. */
62#if 0
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +010063static char version1[] __initdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -070064"sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg\n";
65#endif
66
67
68/* Operational parameters that usually are not changed. */
69
70#define CONFIG_SBMAC_COALESCE
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Time in jiffies before concluding the transmitter is hung. */
73#define TX_TIMEOUT (2*HZ)
74
75
76MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
77MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
78
79/* A few user-configurable values which may be modified when a driver
80 module is loaded. */
81
82/* 1 normal messages, 0 quiet .. 7 verbose. */
83static int debug = 1;
84module_param(debug, int, S_IRUGO);
85MODULE_PARM_DESC(debug, "Debug messages");
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#ifdef CONFIG_SBMAC_COALESCE
Mark Mason693aa942007-04-26 00:23:22 -070088static int int_pktcnt_tx = 255;
89module_param(int_pktcnt_tx, int, S_IRUGO);
90MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Mark Mason693aa942007-04-26 00:23:22 -070092static int int_timeout_tx = 255;
93module_param(int_timeout_tx, int, S_IRUGO);
94MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
95
96static int int_pktcnt_rx = 64;
97module_param(int_pktcnt_rx, int, S_IRUGO);
98MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
99
100static int int_timeout_rx = 64;
101module_param(int_timeout_rx, int, S_IRUGO);
102MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100105#include <asm/sibyte/board.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include <asm/sibyte/sb1250.h>
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000107#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
108#include <asm/sibyte/bcm1480_regs.h>
109#include <asm/sibyte/bcm1480_int.h>
Mark Mason693aa942007-04-26 00:23:22 -0700110#define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000111#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#include <asm/sibyte/sb1250_regs.h>
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000113#include <asm/sibyte/sb1250_int.h>
114#else
115#error invalid SiByte MAC configuation
116#endif
117#include <asm/sibyte/sb1250_scd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#include <asm/sibyte/sb1250_mac.h>
119#include <asm/sibyte/sb1250_dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Ralf Baechlef90fdc32006-02-08 23:23:26 +0000121#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
122#define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
123#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
124#define UNIT_INT(n) (K_INT_MAC_0 + (n))
125#else
126#error invalid SiByte MAC configuation
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100129#ifdef K_INT_PHY
130#define SBMAC_PHY_INT K_INT_PHY
131#else
132#define SBMAC_PHY_INT PHY_POLL
133#endif
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**********************************************************************
136 * Simple types
137 ********************************************************************* */
138
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100139enum sbmac_speed {
140 sbmac_speed_none = 0,
141 sbmac_speed_10 = SPEED_10,
142 sbmac_speed_100 = SPEED_100,
143 sbmac_speed_1000 = SPEED_1000,
144};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100146enum sbmac_duplex {
147 sbmac_duplex_none = -1,
148 sbmac_duplex_half = DUPLEX_HALF,
149 sbmac_duplex_full = DUPLEX_FULL,
150};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100152enum sbmac_fc {
153 sbmac_fc_none,
154 sbmac_fc_disabled,
155 sbmac_fc_frame,
156 sbmac_fc_collision,
157 sbmac_fc_carrier,
158};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100160enum sbmac_state {
161 sbmac_state_uninit,
162 sbmac_state_off,
163 sbmac_state_on,
164 sbmac_state_broken,
165};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167
168/**********************************************************************
169 * Macros
170 ********************************************************************* */
171
172
173#define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
174 (d)->sbdma_dscrtable : (d)->f+1)
175
176
177#define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
178
Mark Mason693aa942007-04-26 00:23:22 -0700179#define SBMAC_MAX_TXDESCR 256
180#define SBMAC_MAX_RXDESCR 256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Stephen Hemminger789585e2008-05-18 04:45:09 +0100182#define ETHER_ADDR_LEN 6
Ralf Baechle74b02472005-10-19 15:40:02 +0100183#define ENET_PACKET_SIZE 1518
184/*#define ENET_PACKET_SIZE 9216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/**********************************************************************
187 * DMA Descriptor structure
188 ********************************************************************* */
189
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100190struct sbdmadscr {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 uint64_t dscr_a;
192 uint64_t dscr_b;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100193};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/**********************************************************************
196 * DMA Controller structure
197 ********************************************************************* */
198
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100199struct sbmacdma {
Ralf Baechle74b02472005-10-19 15:40:02 +0100200
201 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * This stuff is used to identify the channel and the registers
203 * associated with it.
204 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100205 struct sbmac_softc *sbdma_eth; /* back pointer to associated
206 MAC */
207 int sbdma_channel; /* channel number */
208 int sbdma_txdir; /* direction (1=transmit) */
209 int sbdma_maxdescr; /* total # of descriptors
210 in ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#ifdef CONFIG_SBMAC_COALESCE
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100212 int sbdma_int_pktcnt;
213 /* # descriptors rx/tx
214 before interrupt */
215 int sbdma_int_timeout;
216 /* # usec rx/tx interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#endif
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100218 void __iomem *sbdma_config0; /* DMA config register 0 */
219 void __iomem *sbdma_config1; /* DMA config register 1 */
220 void __iomem *sbdma_dscrbase;
221 /* descriptor base address */
222 void __iomem *sbdma_dscrcnt; /* descriptor count register */
223 void __iomem *sbdma_curdscr; /* current descriptor
224 address */
225 void __iomem *sbdma_oodpktlost;
226 /* pkt drop (rx only) */
Ralf Baechle74b02472005-10-19 15:40:02 +0100227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /*
229 * This stuff is for maintenance of the ring
230 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100231 void *sbdma_dscrtable_unaligned;
232 struct sbdmadscr *sbdma_dscrtable;
233 /* base of descriptor table */
234 struct sbdmadscr *sbdma_dscrtable_end;
235 /* end of descriptor table */
236 struct sk_buff **sbdma_ctxtable;
237 /* context table, one
238 per descr */
239 dma_addr_t sbdma_dscrtable_phys;
240 /* and also the phys addr */
241 struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
242 struct sbdmadscr *sbdma_remptr; /* next dscr for sw
243 to remove */
244};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246
247/**********************************************************************
248 * Ethernet softc structure
249 ********************************************************************* */
250
251struct sbmac_softc {
Ralf Baechle74b02472005-10-19 15:40:02 +0100252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
254 * Linux-specific things
255 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100256 struct net_device *sbm_dev; /* pointer to linux device */
257 struct napi_struct napi;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100258 struct phy_device *phy_dev; /* the associated PHY device */
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700259 struct mii_bus *mii_bus; /* the MII bus */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100260 int phy_irq[PHY_MAX_ADDR];
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100261 spinlock_t sbm_lock; /* spin lock */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100262 int sbm_devflags; /* current device flags */
Ralf Baechle74b02472005-10-19 15:40:02 +0100263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /*
265 * Controller-specific things
266 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100267 void __iomem *sbm_base; /* MAC's base address */
268 enum sbmac_state sbm_state; /* current state */
Ralf Baechle74b02472005-10-19 15:40:02 +0100269
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100270 void __iomem *sbm_macenable; /* MAC Enable Register */
271 void __iomem *sbm_maccfg; /* MAC Config Register */
272 void __iomem *sbm_fifocfg; /* FIFO Config Register */
273 void __iomem *sbm_framecfg; /* Frame Config Register */
274 void __iomem *sbm_rxfilter; /* Receive Filter Register */
275 void __iomem *sbm_isr; /* Interrupt Status Register */
276 void __iomem *sbm_imr; /* Interrupt Mask Register */
277 void __iomem *sbm_mdio; /* MDIO Register */
Ralf Baechle74b02472005-10-19 15:40:02 +0100278
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100279 enum sbmac_speed sbm_speed; /* current speed */
280 enum sbmac_duplex sbm_duplex; /* current duplex */
281 enum sbmac_fc sbm_fc; /* cur. flow control setting */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100282 int sbm_pause; /* current pause setting */
283 int sbm_link; /* current link state */
Ralf Baechle74b02472005-10-19 15:40:02 +0100284
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100285 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
Ralf Baechle74b02472005-10-19 15:40:02 +0100286
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100287 struct sbmacdma sbm_txdma; /* only channel 0 for now */
288 struct sbmacdma sbm_rxdma;
289 int rx_hw_checksum;
290 int sbe_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292
293
294/**********************************************************************
295 * Externs
296 ********************************************************************* */
297
298/**********************************************************************
299 * Prototypes
300 ********************************************************************* */
301
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100302static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
303 int txrx, int maxdescr);
304static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
Stephen Hemminger789585e2008-05-18 04:45:09 +0100305static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
306 struct sk_buff *m);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100307static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
308static void sbdma_emptyring(struct sbmacdma *d);
Stephen Hemminger789585e2008-05-18 04:45:09 +0100309static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100310static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
311 int work_to_do, int poll);
312static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
313 int poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static int sbmac_initctx(struct sbmac_softc *s);
315static void sbmac_channel_start(struct sbmac_softc *s);
316static void sbmac_channel_stop(struct sbmac_softc *s);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100317static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
318 enum sbmac_state);
319static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static uint64_t sbmac_addr2reg(unsigned char *ptr);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100321static irqreturn_t sbmac_intr(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
323static void sbmac_setmulti(struct sbmac_softc *sc);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100324static int sbmac_init(struct platform_device *pldev, long long base);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100325static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
326static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
327 enum sbmac_fc fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329static int sbmac_open(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static void sbmac_tx_timeout (struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static void sbmac_set_rx_mode(struct net_device *dev);
332static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
333static int sbmac_close(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700334static int sbmac_poll(struct napi_struct *napi, int budget);
Mark Mason693aa942007-04-26 00:23:22 -0700335
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100336static void sbmac_mii_poll(struct net_device *dev);
Ralf Baechle59b81822005-10-20 12:01:28 +0100337static int sbmac_mii_probe(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100339static void sbmac_mii_sync(void __iomem *sbm_mdio);
340static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100341 int bitcnt);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100342static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
343static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
344 u16 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346
347/**********************************************************************
348 * Globals
349 ********************************************************************* */
350
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100351static char sbmac_string[] = "sb1250-mac";
352static char sbmac_pretty[] = "SB1250 MAC";
353
354static char sbmac_mdio_string[] = "sb1250-mac-mdio";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356
357/**********************************************************************
358 * MDIO constants
359 ********************************************************************* */
360
361#define MII_COMMAND_START 0x01
362#define MII_COMMAND_READ 0x02
363#define MII_COMMAND_WRITE 0x01
364#define MII_COMMAND_ACK 0x02
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
367
368#define ENABLE 1
369#define DISABLE 0
370
371/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100372 * SBMAC_MII_SYNC(sbm_mdio)
Ralf Baechle74b02472005-10-19 15:40:02 +0100373 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * Synchronize with the MII - send a pattern of bits to the MII
375 * that will guarantee that it is ready to accept a command.
Ralf Baechle74b02472005-10-19 15:40:02 +0100376 *
377 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100378 * sbm_mdio - address of the MAC's MDIO register
Ralf Baechle74b02472005-10-19 15:40:02 +0100379 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Return value:
381 * nothing
382 ********************************************************************* */
383
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100384static void sbmac_mii_sync(void __iomem *sbm_mdio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 int cnt;
387 uint64_t bits;
388 int mac_mdio_genc;
389
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100390 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
Ralf Baechle74b02472005-10-19 15:40:02 +0100393
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100394 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 for (cnt = 0; cnt < 32; cnt++) {
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100397 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
398 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400}
401
402/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100403 * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
Ralf Baechle74b02472005-10-19 15:40:02 +0100404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * Send some bits to the MII. The bits to be sent are right-
406 * justified in the 'data' parameter.
Ralf Baechle74b02472005-10-19 15:40:02 +0100407 *
408 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100409 * sbm_mdio - address of the MAC's MDIO register
410 * data - data to send
411 * bitcnt - number of bits to send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ********************************************************************* */
413
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100414static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
415 int bitcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int i;
418 uint64_t bits;
419 unsigned int curmask;
420 int mac_mdio_genc;
421
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100422 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 bits = M_MAC_MDIO_DIR_OUTPUT;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100425 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 curmask = 1 << (bitcnt - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 for (i = 0; i < bitcnt; i++) {
430 if (data & curmask)
431 bits |= M_MAC_MDIO_OUT;
432 else bits &= ~M_MAC_MDIO_OUT;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100433 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
434 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
435 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 curmask >>= 1;
437 }
438}
439
440
441
442/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100443 * SBMAC_MII_READ(bus, phyaddr, regidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * Read a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100445 *
446 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100447 * bus - MDIO bus handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * phyaddr - PHY's address
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100449 * regnum - index of register to read
Ralf Baechle74b02472005-10-19 15:40:02 +0100450 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * Return value:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100452 * value read, or 0xffff if an error occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 ********************************************************************* */
454
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100455static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100457 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
458 void __iomem *sbm_mdio = sc->sbm_mdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int idx;
460 int error;
461 int regval;
462 int mac_mdio_genc;
463
464 /*
465 * Synchronize ourselves so that the PHY knows the next
466 * thing coming down is a command
467 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100468 sbmac_mii_sync(sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * Send the data to the PHY. The sequence is
472 * a "start" command (2 bits)
473 * a "read" command (2 bits)
474 * the PHY addr (5 bits)
475 * the register index (5 bits)
476 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100477 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
478 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
479 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
480 sbmac_mii_senddata(sbm_mdio, regidx, 5);
Ralf Baechle74b02472005-10-19 15:40:02 +0100481
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100482 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100483
484 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * Switch the port around without a clock transition.
486 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100487 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Send out a clock pulse to signal we want the status
491 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100492 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
493 sbm_mdio);
494 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100495
496 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * If an error occurred, the PHY will signal '1' back
498 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100499 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
Ralf Baechle74b02472005-10-19 15:40:02 +0100500
501 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 * Issue an 'idle' clock pulse, but keep the direction
503 * the same.
504 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100505 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
506 sbm_mdio);
507 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 regval = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 for (idx = 0; idx < 16; idx++) {
512 regval <<= 1;
Ralf Baechle74b02472005-10-19 15:40:02 +0100513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (error == 0) {
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100515 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 regval |= 1;
517 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100518
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100519 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
520 sbm_mdio);
521 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Switch back to output */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100525 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (error == 0)
528 return regval;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100529 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532
533/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100534 * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
Ralf Baechle74b02472005-10-19 15:40:02 +0100535 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * Write a value to a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100537 *
538 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100539 * bus - MDIO bus handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 * phyaddr - PHY to use
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100541 * regidx - register within the PHY
542 * regval - data to write to register
Ralf Baechle74b02472005-10-19 15:40:02 +0100543 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * Return value:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100545 * 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 ********************************************************************* */
547
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100548static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
549 u16 regval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100551 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
552 void __iomem *sbm_mdio = sc->sbm_mdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int mac_mdio_genc;
554
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100555 sbmac_mii_sync(sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100556
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100557 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
558 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
559 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
560 sbmac_mii_senddata(sbm_mdio, regidx, 5);
561 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
562 sbmac_mii_senddata(sbm_mdio, regval, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100564 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100566 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
567
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571
572
573/**********************************************************************
574 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
Ralf Baechle74b02472005-10-19 15:40:02 +0100575 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * Initialize a DMA channel context. Since there are potentially
577 * eight DMA channels per MAC, it's nice to do this in a standard
Ralf Baechle74b02472005-10-19 15:40:02 +0100578 * way.
579 *
580 * Input parameters:
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100581 * d - struct sbmacdma (DMA channel context)
582 * s - struct sbmac_softc (pointer to a MAC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * chan - channel number (0..1 right now)
584 * txrx - Identifies DMA_TX or DMA_RX for channel direction
585 * maxdescr - number of descriptors
Ralf Baechle74b02472005-10-19 15:40:02 +0100586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 * Return value:
588 * nothing
589 ********************************************************************* */
590
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100591static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
592 int txrx, int maxdescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Mark Mason693aa942007-04-26 00:23:22 -0700594#ifdef CONFIG_SBMAC_COALESCE
595 int int_pktcnt, int_timeout;
596#endif
597
Ralf Baechle74b02472005-10-19 15:40:02 +0100598 /*
599 * Save away interesting stuff in the structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 d->sbdma_eth = s;
603 d->sbdma_channel = chan;
604 d->sbdma_txdir = txrx;
Ralf Baechle74b02472005-10-19 15:40:02 +0100605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#if 0
607 /* RMON clearing */
608 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
609#endif
610
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100611 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
612 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
613 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
614 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
615 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
616 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
617 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
618 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
619 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
620 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
621 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
622 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
623 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
624 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
625 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
626 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
627 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
628 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
629 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
630 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
631 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Ralf Baechle74b02472005-10-19 15:40:02 +0100633 /*
634 * initialize register pointers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100636
637 d->sbdma_config0 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100639 d->sbdma_config1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100641 d->sbdma_dscrbase =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
Ralf Baechle74b02472005-10-19 15:40:02 +0100643 d->sbdma_dscrcnt =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
Ralf Baechle74b02472005-10-19 15:40:02 +0100645 d->sbdma_curdscr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
Mark Mason693aa942007-04-26 00:23:22 -0700647 if (d->sbdma_txdir)
648 d->sbdma_oodpktlost = NULL;
649 else
650 d->sbdma_oodpktlost =
651 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
Ralf Baechle74b02472005-10-19 15:40:02 +0100652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * Allocate memory for the ring
655 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 d->sbdma_maxdescr = maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100658
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100659 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
660 sizeof(*d->sbdma_dscrtable),
661 GFP_KERNEL);
Ralf Baechle04115de2005-10-10 14:50:36 +0100662
663 /*
664 * The descriptor table must be aligned to at least 16 bytes or the
665 * MAC will corrupt it.
666 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100667 d->sbdma_dscrtable = (struct sbdmadscr *)
668 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
669 sizeof(*d->sbdma_dscrtable));
Ralf Baechle74b02472005-10-19 15:40:02 +0100670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
Ralf Baechle74b02472005-10-19 15:40:02 +0100674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * And context table
677 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100678
Mariusz Kozlowskic477f332007-07-31 23:58:36 +0200679 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100680 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
Ralf Baechle74b02472005-10-19 15:40:02 +0100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#ifdef CONFIG_SBMAC_COALESCE
683 /*
684 * Setup Rx/Tx DMA coalescing defaults
685 */
686
Mark Mason693aa942007-04-26 00:23:22 -0700687 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if ( int_pktcnt ) {
689 d->sbdma_int_pktcnt = int_pktcnt;
690 } else {
691 d->sbdma_int_pktcnt = 1;
692 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100693
Mark Mason693aa942007-04-26 00:23:22 -0700694 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if ( int_timeout ) {
696 d->sbdma_int_timeout = int_timeout;
697 } else {
698 d->sbdma_int_timeout = 0;
699 }
700#endif
701
702}
703
704/**********************************************************************
705 * SBDMA_CHANNEL_START(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100708 *
709 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * d - DMA channel to init (context must be previously init'd
711 * rxtx - DMA_RX or DMA_TX depending on what type of channel
Ralf Baechle74b02472005-10-19 15:40:02 +0100712 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * Return value:
714 * nothing
715 ********************************************************************* */
716
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100717static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 /*
720 * Turn on the DMA channel
721 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +0100724 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
725 0, d->sbdma_config1);
726 __raw_writeq(M_DMA_EOP_INT_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 V_DMA_RINGSZ(d->sbdma_maxdescr) |
728 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
Ralf Baechle20399732005-10-19 15:39:05 +0100729 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#else
Ralf Baechle20399732005-10-19 15:39:05 +0100731 __raw_writeq(0, d->sbdma_config1);
732 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
733 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734#endif
735
Ralf Baechle20399732005-10-19 15:39:05 +0100736 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
739 * Initialize ring pointers
740 */
741
742 d->sbdma_addptr = d->sbdma_dscrtable;
743 d->sbdma_remptr = d->sbdma_dscrtable;
744}
745
746/**********************************************************************
747 * SBDMA_CHANNEL_STOP(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100748 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100750 *
751 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * d - DMA channel to init (context must be previously init'd
Ralf Baechle74b02472005-10-19 15:40:02 +0100753 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * Return value:
755 * nothing
756 ********************************************************************* */
757
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100758static void sbdma_channel_stop(struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 /*
761 * Turn off the DMA channel
762 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100763
Ralf Baechle20399732005-10-19 15:39:05 +0100764 __raw_writeq(0, d->sbdma_config1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100765
Ralf Baechle20399732005-10-19 15:39:05 +0100766 __raw_writeq(0, d->sbdma_dscrbase);
Ralf Baechle74b02472005-10-19 15:40:02 +0100767
Ralf Baechle20399732005-10-19 15:39:05 +0100768 __raw_writeq(0, d->sbdma_config0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /*
771 * Zero ring pointers
772 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100773
Ralf Baechle20399732005-10-19 15:39:05 +0100774 d->sbdma_addptr = NULL;
775 d->sbdma_remptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Stephen Hemminger789585e2008-05-18 04:45:09 +0100778static inline void sbdma_align_skb(struct sk_buff *skb,
779 unsigned int power2, unsigned int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Stephen Hemminger789585e2008-05-18 04:45:09 +0100781 unsigned char *addr = skb->data;
782 unsigned char *newaddr = PTR_ALIGN(addr, power2);
Ralf Baechle74b02472005-10-19 15:40:02 +0100783
Stephen Hemminger789585e2008-05-18 04:45:09 +0100784 skb_reserve(skb, newaddr - addr + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787
788/**********************************************************************
789 * SBDMA_ADD_RCVBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +0100790 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * Add a buffer to the specified DMA channel. For receive channels,
792 * this queues a buffer for inbound packets.
Ralf Baechle74b02472005-10-19 15:40:02 +0100793 *
794 * Input parameters:
Stephen Hemminger789585e2008-05-18 04:45:09 +0100795 * sc - softc structure
796 * d - DMA channel descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * sb - sk_buff to add, or NULL if we should allocate one
Ralf Baechle74b02472005-10-19 15:40:02 +0100798 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 * Return value:
800 * 0 if buffer could not be added (ring is full)
801 * 1 if buffer added successfully
802 ********************************************************************* */
803
804
Stephen Hemminger789585e2008-05-18 04:45:09 +0100805static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
806 struct sk_buff *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Stephen Hemminger789585e2008-05-18 04:45:09 +0100808 struct net_device *dev = sc->sbm_dev;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100809 struct sbdmadscr *dsc;
810 struct sbdmadscr *nextdsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct sk_buff *sb_new = NULL;
812 int pktsize = ENET_PACKET_SIZE;
Ralf Baechle74b02472005-10-19 15:40:02 +0100813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dsc = d->sbdma_addptr;
817 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +0100818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 /*
820 * figure out if the ring is full - if the next descriptor
821 * is the same as the one that we're going to remove from
822 * the ring, the ring is full
823 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (nextdsc == d->sbdma_remptr) {
826 return -ENOSPC;
827 }
828
Ralf Baechle74b02472005-10-19 15:40:02 +0100829 /*
830 * Allocate a sk_buff if we don't already have one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * If we do have an sk_buff, reset it so that it's empty.
832 *
833 * Note: sk_buffs don't seem to be guaranteed to have any sort
834 * of alignment when they are allocated. Therefore, allocate enough
835 * extra space to make sure that:
836 *
837 * 1. the data does not start in the middle of a cache line.
838 * 2. The data does not end in the middle of a cache line
Ralf Baechle74b02472005-10-19 15:40:02 +0100839 * 3. The buffer can be aligned such that the IP addresses are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * naturally aligned.
841 *
842 * Remember, the SOCs MAC writes whole cache lines at a time,
843 * without reading the old contents first. So, if the sk_buff's
844 * data portion starts in the middle of a cache line, the SOC
845 * DMA will trash the beginning (and ending) portions.
846 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (sb == NULL) {
Stephen Hemminger789585e2008-05-18 04:45:09 +0100849 sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
850 SMP_CACHE_BYTES * 2 +
851 NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (sb_new == NULL) {
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100853 pr_info("%s: sk_buff allocation failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 d->sbdma_eth->sbm_dev->name);
855 return -ENOBUFS;
856 }
857
Stephen Hemminger789585e2008-05-18 04:45:09 +0100858 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 else {
861 sb_new = sb;
Ralf Baechle74b02472005-10-19 15:40:02 +0100862 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * nothing special to reinit buffer, it's already aligned
864 * and sb->data already points to a good place.
865 */
866 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100869 * fill in the descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#ifdef CONFIG_SBMAC_COALESCE
873 /*
874 * Do not interrupt per DMA transfer.
875 */
David S. Miller689be432005-06-28 15:25:31 -0700876 dsc->dscr_a = virt_to_phys(sb_new->data) |
Stephen Hemminger789585e2008-05-18 04:45:09 +0100877 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#else
David S. Miller689be432005-06-28 15:25:31 -0700879 dsc->dscr_a = virt_to_phys(sb_new->data) |
Stephen Hemminger789585e2008-05-18 04:45:09 +0100880 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 M_DMA_DSCRA_INTERRUPT;
882#endif
883
884 /* receiving: no options */
885 dsc->dscr_b = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100888 * fill in the context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
Ralf Baechle74b02472005-10-19 15:40:02 +0100892
893 /*
894 * point at next packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 d->sbdma_addptr = nextdsc;
Ralf Baechle74b02472005-10-19 15:40:02 +0100898
899 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * Give the buffer to the DMA engine.
901 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100902
Ralf Baechle20399732005-10-19 15:39:05 +0100903 __raw_writeq(1, d->sbdma_dscrcnt);
Ralf Baechle74b02472005-10-19 15:40:02 +0100904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0; /* we did it */
906}
907
908/**********************************************************************
909 * SBDMA_ADD_TXBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +0100910 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Add a transmit buffer to the specified DMA channel, causing a
912 * transmit to start.
Ralf Baechle74b02472005-10-19 15:40:02 +0100913 *
914 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * d - DMA channel descriptor
916 * sb - sk_buff to add
Ralf Baechle74b02472005-10-19 15:40:02 +0100917 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * Return value:
919 * 0 transmit queued successfully
920 * otherwise error code
921 ********************************************************************* */
922
923
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100924static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100926 struct sbdmadscr *dsc;
927 struct sbdmadscr *nextdsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 uint64_t phys;
929 uint64_t ncb;
930 int length;
Ralf Baechle74b02472005-10-19 15:40:02 +0100931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +0100933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 dsc = d->sbdma_addptr;
935 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +0100936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /*
938 * figure out if the ring is full - if the next descriptor
939 * is the same as the one that we're going to remove from
940 * the ring, the ring is full
941 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (nextdsc == d->sbdma_remptr) {
944 return -ENOSPC;
945 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * Under Linux, it's not necessary to copy/coalesce buffers
949 * like it is on NetBSD. We think they're all contiguous,
950 * but that may not be true for GBE.
951 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 length = sb->len;
Ralf Baechle74b02472005-10-19 15:40:02 +0100954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * fill in the descriptor. Note that the number of cache
957 * blocks in the descriptor is the number of blocks
958 * *spanned*, so we need to add in the offset (if any)
959 * while doing the calculation.
960 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 phys = virt_to_phys(sb->data);
963 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
964
Ralf Baechle74b02472005-10-19 15:40:02 +0100965 dsc->dscr_a = phys |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 V_DMA_DSCRA_A_SIZE(ncb) |
967#ifndef CONFIG_SBMAC_COALESCE
968 M_DMA_DSCRA_INTERRUPT |
969#endif
970 M_DMA_ETHTX_SOP;
Ralf Baechle74b02472005-10-19 15:40:02 +0100971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* transmitting: set outbound options and length */
973
974 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
975 V_DMA_DSCRB_PKT_SIZE(length);
Ralf Baechle74b02472005-10-19 15:40:02 +0100976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /*
Ralf Baechle74b02472005-10-19 15:40:02 +0100978 * fill in the context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
Ralf Baechle74b02472005-10-19 15:40:02 +0100982
983 /*
984 * point at next packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 d->sbdma_addptr = nextdsc;
Ralf Baechle74b02472005-10-19 15:40:02 +0100988
989 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 * Give the buffer to the DMA engine.
991 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100992
Ralf Baechle20399732005-10-19 15:39:05 +0100993 __raw_writeq(1, d->sbdma_dscrcnt);
Ralf Baechle74b02472005-10-19 15:40:02 +0100994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return 0; /* we did it */
996}
997
998
999
1000
1001/**********************************************************************
1002 * SBDMA_EMPTYRING(d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * Free all allocated sk_buffs on the specified DMA channel;
Ralf Baechle74b02472005-10-19 15:40:02 +01001005 *
1006 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * Return value:
1010 * nothing
1011 ********************************************************************* */
1012
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001013static void sbdma_emptyring(struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 int idx;
1016 struct sk_buff *sb;
Ralf Baechle74b02472005-10-19 15:40:02 +01001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
1019 sb = d->sbdma_ctxtable[idx];
1020 if (sb) {
1021 dev_kfree_skb(sb);
1022 d->sbdma_ctxtable[idx] = NULL;
1023 }
1024 }
1025}
1026
1027
1028/**********************************************************************
1029 * SBDMA_FILLRING(d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001030 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * Fill the specified DMA channel (must be receive channel)
1032 * with sk_buffs
Ralf Baechle74b02472005-10-19 15:40:02 +01001033 *
1034 * Input parameters:
Stephen Hemminger789585e2008-05-18 04:45:09 +01001035 * sc - softc structure
1036 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001037 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 * Return value:
1039 * nothing
1040 ********************************************************************* */
1041
Stephen Hemminger789585e2008-05-18 04:45:09 +01001042static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 int idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01001045
Stephen Hemminger789585e2008-05-18 04:45:09 +01001046 for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
1047 if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
1049 }
1050}
1051
Deepak Saxenad6830012007-03-19 15:43:11 -07001052#ifdef CONFIG_NET_POLL_CONTROLLER
1053static void sbmac_netpoll(struct net_device *netdev)
1054{
1055 struct sbmac_softc *sc = netdev_priv(netdev);
1056 int irq = sc->sbm_dev->irq;
1057
1058 __raw_writeq(0, sc->sbm_imr);
1059
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -07001060 sbmac_intr(irq, netdev);
Deepak Saxenad6830012007-03-19 15:43:11 -07001061
1062#ifdef CONFIG_SBMAC_COALESCE
1063 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1064 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1065 sc->sbm_imr);
1066#else
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04001067 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
Deepak Saxenad6830012007-03-19 15:43:11 -07001068 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1069#endif
1070}
1071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073/**********************************************************************
Mark Mason693aa942007-04-26 00:23:22 -07001074 * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001076 * Process "completed" receive buffers on the specified DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +01001077 *
1078 * Input parameters:
Mark Mason693aa942007-04-26 00:23:22 -07001079 * sc - softc structure
1080 * d - DMA channel context
1081 * work_to_do - no. of packets to process before enabling interrupt
1082 * again (for NAPI)
1083 * poll - 1: using polling (for NAPI)
Ralf Baechle74b02472005-10-19 15:40:02 +01001084 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * Return value:
1086 * nothing
1087 ********************************************************************* */
1088
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001089static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1090 int work_to_do, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001092 struct net_device *dev = sc->sbm_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int curidx;
1094 int hwidx;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001095 struct sbdmadscr *dsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct sk_buff *sb;
1097 int len;
Mark Mason693aa942007-04-26 00:23:22 -07001098 int work_done = 0;
1099 int dropped = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001100
Mark Mason693aa942007-04-26 00:23:22 -07001101 prefetch(d);
1102
1103again:
1104 /* Check if the HW dropped any frames */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001105 dev->stats.rx_fifo_errors
Mark Mason693aa942007-04-26 00:23:22 -07001106 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1107 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1108
1109 while (work_to_do-- > 0) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001110 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * figure out where we are (as an index) and where
1112 * the hardware is (also as an index)
1113 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001114 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * descriptor table was page-aligned and contiguous in
1116 * both virtual and physical memory -- you could then
1117 * just compare the low-order bits of the virtual address
1118 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1119 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001120
Mark Mason693aa942007-04-26 00:23:22 -07001121 dsc = d->sbdma_remptr;
1122 curidx = dsc - d->sbdma_dscrtable;
1123
1124 prefetch(dsc);
1125 prefetch(&d->sbdma_ctxtable[curidx]);
1126
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001127 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1128 d->sbdma_dscrtable_phys) /
1129 sizeof(*d->sbdma_dscrtable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /*
1132 * If they're the same, that means we've processed all
1133 * of the descriptors up to (but not including) the one that
1134 * the hardware is working on right now.
1135 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (curidx == hwidx)
Mark Mason693aa942007-04-26 00:23:22 -07001138 goto done;
Ralf Baechle74b02472005-10-19 15:40:02 +01001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /*
1141 * Otherwise, get the packet's sk_buff ptr back
1142 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 sb = d->sbdma_ctxtable[curidx];
1145 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
Ralf Baechle74b02472005-10-19 15:40:02 +01001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * Check packet status. If good, process it.
1151 * If not, silently drop it and put it back on the
1152 * receive ring.
1153 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001154
Mark Mason693aa942007-04-26 00:23:22 -07001155 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Add a new buffer to replace the old one. If we fail
1159 * to allocate a buffer, we're going to drop this
1160 * packet and put it right back on the receive ring.
1161 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001162
Stephen Hemminger789585e2008-05-18 04:45:09 +01001163 if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
1164 -ENOBUFS)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001165 dev->stats.rx_dropped++;
Stephen Hemminger789585e2008-05-18 04:45:09 +01001166 /* Re-add old buffer */
1167 sbdma_add_rcvbuffer(sc, d, sb);
Mark Mason693aa942007-04-26 00:23:22 -07001168 /* No point in continuing at the moment */
1169 printk(KERN_ERR "dropped packet (1)\n");
1170 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1171 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 } else {
1173 /*
1174 * Set length into the packet
1175 */
1176 skb_put(sb,len);
Ralf Baechle74b02472005-10-19 15:40:02 +01001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * Buffer has been replaced on the
1180 * receive ring. Pass the buffer to
1181 * the kernel
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1184 /* Check hw IPv4/TCP checksum if supported */
1185 if (sc->rx_hw_checksum == ENABLE) {
1186 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1187 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1188 sb->ip_summed = CHECKSUM_UNNECESSARY;
1189 /* don't need to set sb->csum */
1190 } else {
1191 sb->ip_summed = CHECKSUM_NONE;
1192 }
1193 }
Mark Mason693aa942007-04-26 00:23:22 -07001194 prefetch(sb->data);
1195 prefetch((const void *)(((char *)sb->data)+32));
1196 if (poll)
1197 dropped = netif_receive_skb(sb);
1198 else
1199 dropped = netif_rx(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001200
Mark Mason693aa942007-04-26 00:23:22 -07001201 if (dropped == NET_RX_DROP) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001202 dev->stats.rx_dropped++;
Mark Mason693aa942007-04-26 00:23:22 -07001203 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1204 goto done;
1205 }
1206 else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001207 dev->stats.rx_bytes += len;
1208 dev->stats.rx_packets++;
Mark Mason693aa942007-04-26 00:23:22 -07001209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 } else {
1212 /*
1213 * Packet was mangled somehow. Just drop it and
1214 * put it back on the receive ring.
1215 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001216 dev->stats.rx_errors++;
Stephen Hemminger789585e2008-05-18 04:45:09 +01001217 sbdma_add_rcvbuffer(sc, d, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001219
1220
1221 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * .. and advance to the next buffer.
1223 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Mark Mason693aa942007-04-26 00:23:22 -07001226 work_done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Mark Mason693aa942007-04-26 00:23:22 -07001228 if (!poll) {
1229 work_to_do = 32;
1230 goto again; /* collect fifo drop statistics again */
1231 }
1232done:
1233 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236/**********************************************************************
1237 * SBDMA_TX_PROCESS(sc,d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001238 *
1239 * Process "completed" transmit buffers on the specified DMA channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * This is normally called within the interrupt service routine.
1241 * Note that this isn't really ideal for priority channels, since
Ralf Baechle74b02472005-10-19 15:40:02 +01001242 * it processes all of the packets on a given channel before
1243 * returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001245 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * sc - softc structure
Mark Mason693aa942007-04-26 00:23:22 -07001247 * d - DMA channel context
1248 * poll - 1: using polling (for NAPI)
Ralf Baechle74b02472005-10-19 15:40:02 +01001249 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * Return value:
1251 * nothing
1252 ********************************************************************* */
1253
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001254static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1255 int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001257 struct net_device *dev = sc->sbm_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int curidx;
1259 int hwidx;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001260 struct sbdmadscr *dsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct sk_buff *sb;
1262 unsigned long flags;
Mark Mason693aa942007-04-26 00:23:22 -07001263 int packets_handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 spin_lock_irqsave(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001266
Mark Mason693aa942007-04-26 00:23:22 -07001267 if (d->sbdma_remptr == d->sbdma_addptr)
1268 goto end_unlock;
1269
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001270 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1271 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
Mark Mason693aa942007-04-26 00:23:22 -07001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 for (;;) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001274 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * figure out where we are (as an index) and where
1276 * the hardware is (also as an index)
1277 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001278 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 * descriptor table was page-aligned and contiguous in
1280 * both virtual and physical memory -- you could then
1281 * just compare the low-order bits of the virtual address
1282 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1283 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 /*
1288 * If they're the same, that means we've processed all
1289 * of the descriptors up to (but not including) the one that
1290 * the hardware is working on right now.
1291 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (curidx == hwidx)
1294 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /*
1297 * Otherwise, get the packet's sk_buff ptr back
1298 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 dsc = &(d->sbdma_dscrtable[curidx]);
1301 sb = d->sbdma_ctxtable[curidx];
1302 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Stats
1306 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001307
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001308 dev->stats.tx_bytes += sb->len;
1309 dev->stats.tx_packets++;
Ralf Baechle74b02472005-10-19 15:40:02 +01001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
1312 * for transmits, we just free buffers.
1313 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 dev_kfree_skb_irq(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001316
1317 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * .. and advance to the next buffer.
1319 */
1320
1321 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001322
Mark Mason693aa942007-04-26 00:23:22 -07001323 packets_handled++;
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
1328 * Decide if we should wake up the protocol or not.
1329 * Other drivers seem to do this when we reach a low
1330 * watermark on the transmit queue.
1331 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001332
Mark Mason693aa942007-04-26 00:23:22 -07001333 if (packets_handled)
1334 netif_wake_queue(d->sbdma_eth->sbm_dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01001335
Mark Mason693aa942007-04-26 00:23:22 -07001336end_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341
1342
1343/**********************************************************************
1344 * SBMAC_INITCTX(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001345 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * Initialize an Ethernet context structure - this is called
1347 * once per MAC on the 1250. Memory is allocated here, so don't
1348 * call it again from inside the ioctl routines that bring the
1349 * interface up/down
Ralf Baechle74b02472005-10-19 15:40:02 +01001350 *
1351 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * s - sbmac context structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * Return value:
1355 * 0
1356 ********************************************************************* */
1357
1358static int sbmac_initctx(struct sbmac_softc *s)
1359{
Ralf Baechle74b02472005-10-19 15:40:02 +01001360
1361 /*
1362 * figure out the addresses of some ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1366 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1367 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1368 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1369 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1370 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1371 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1372 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /*
1375 * Initialize the DMA channels. Right now, only one per MAC is used
1376 * Note: Only do this _once_, as it allocates memory from the kernel!
1377 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1380 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
Ralf Baechle74b02472005-10-19 15:40:02 +01001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /*
1383 * initial state is OFF
1384 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return 0;
1389}
1390
1391
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001392static void sbdma_uninitctx(struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Mark Mason693aa942007-04-26 00:23:22 -07001394 if (d->sbdma_dscrtable_unaligned) {
1395 kfree(d->sbdma_dscrtable_unaligned);
1396 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (d->sbdma_ctxtable) {
1400 kfree(d->sbdma_ctxtable);
1401 d->sbdma_ctxtable = NULL;
1402 }
1403}
1404
1405
1406static void sbmac_uninitctx(struct sbmac_softc *sc)
1407{
1408 sbdma_uninitctx(&(sc->sbm_txdma));
1409 sbdma_uninitctx(&(sc->sbm_rxdma));
1410}
1411
1412
1413/**********************************************************************
1414 * SBMAC_CHANNEL_START(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001415 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * Start packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001417 *
1418 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001420 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * Return value:
1422 * nothing
1423 ********************************************************************* */
1424
1425static void sbmac_channel_start(struct sbmac_softc *s)
1426{
1427 uint64_t reg;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001428 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 uint64_t cfg,fifo,framecfg;
1430 int idx, th_value;
Ralf Baechle74b02472005-10-19 15:40:02 +01001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * Don't do this if running
1434 */
1435
1436 if (s->sbm_state == sbmac_state_on)
1437 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /*
1440 * Bring the controller out of reset, but leave it off.
1441 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001442
Ralf Baechle20399732005-10-19 15:39:05 +01001443 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /*
1446 * Ignore all received packets
1447 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001448
Ralf Baechle20399732005-10-19 15:39:05 +01001449 __raw_writeq(0, s->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001450
1451 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * Calculate values for various control registers.
1453 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 cfg = M_MAC_RETRY_EN |
Ralf Baechle74b02472005-10-19 15:40:02 +01001456 M_MAC_TX_HOLD_SOP_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 V_MAC_TX_PAUSE_CNT_16K |
1458 M_MAC_AP_STAT_EN |
1459 M_MAC_FAST_SYNC |
1460 M_MAC_SS_EN |
1461 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001462
1463 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1465 * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1466 * Use a larger RD_THRSH for gigabit
1467 */
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001468 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 th_value = 28;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001470 else
1471 th_value = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1474 ((s->sbm_speed == sbmac_speed_1000)
1475 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1476 V_MAC_TX_RL_THRSH(4) |
1477 V_MAC_RX_PL_THRSH(4) |
1478 V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 V_MAC_RX_RL_THRSH(8) |
1480 0;
1481
1482 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1483 V_MAC_MAX_FRAMESZ_DEFAULT |
1484 V_MAC_BACKOFF_SEL(1);
1485
1486 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001487 * Clear out the hash address map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 port = s->sbm_base + R_MAC_HASH_BASE;
1491 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001492 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 port += sizeof(uint64_t);
1494 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /*
1497 * Clear out the exact-match table
1498 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 port = s->sbm_base + R_MAC_ADDR_BASE;
1501 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001502 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 port += sizeof(uint64_t);
1504 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
1507 * Clear out the DMA Channel mapping table registers
1508 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 port = s->sbm_base + R_MAC_CHUP0_BASE;
1511 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001512 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 port += sizeof(uint64_t);
1514 }
1515
1516
1517 port = s->sbm_base + R_MAC_CHLO0_BASE;
1518 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001519 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 port += sizeof(uint64_t);
1521 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 /*
1524 * Program the hardware address. It goes into the hardware-address
1525 * register as well as the first filter register.
1526 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 reg = sbmac_addr2reg(s->sbm_hwaddr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 port = s->sbm_base + R_MAC_ADDR_BASE;
Ralf Baechle20399732005-10-19 15:39:05 +01001531 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1533
1534#ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
1535 /*
1536 * Pass1 SOCs do not receive packets addressed to the
1537 * destination address in the R_MAC_ETHERNET_ADDR register.
1538 * Set the value to zero.
1539 */
Ralf Baechle20399732005-10-19 15:39:05 +01001540 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541#else
Ralf Baechle20399732005-10-19 15:39:05 +01001542 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
1546 * Set the receive filter for no packets, and write values
1547 * to the various config registers
1548 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001549
Ralf Baechle20399732005-10-19 15:39:05 +01001550 __raw_writeq(0, s->sbm_rxfilter);
1551 __raw_writeq(0, s->sbm_imr);
1552 __raw_writeq(framecfg, s->sbm_framecfg);
1553 __raw_writeq(fifo, s->sbm_fifocfg);
1554 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /*
1557 * Initialize DMA channels (rings should be ok now)
1558 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1561 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
Ralf Baechle74b02472005-10-19 15:40:02 +01001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * Configure the speed, duplex, and flow control
1565 */
1566
1567 sbmac_set_speed(s,s->sbm_speed);
1568 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
Ralf Baechle74b02472005-10-19 15:40:02 +01001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Fill the receive ring
1572 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001573
Stephen Hemminger789585e2008-05-18 04:45:09 +01001574 sbdma_fillring(s, &(s->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001575
1576 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * Turn on the rest of the bits in the enable register
Ralf Baechle74b02472005-10-19 15:40:02 +01001578 */
1579
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001580#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1581 __raw_writeq(M_MAC_RXDMA_EN0 |
1582 M_MAC_TXDMA_EN0, s->sbm_macenable);
1583#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Ralf Baechle20399732005-10-19 15:39:05 +01001584 __raw_writeq(M_MAC_RXDMA_EN0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 M_MAC_TXDMA_EN0 |
1586 M_MAC_RX_ENABLE |
Ralf Baechle20399732005-10-19 15:39:05 +01001587 M_MAC_TX_ENABLE, s->sbm_macenable);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001588#else
1589#error invalid SiByte MAC configuation
1590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +01001593 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1594 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595#else
Ralf Baechle20399732005-10-19 15:39:05 +01001596 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1597 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001601 * Enable receiving unicasts and broadcasts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001603
1604 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1605
1606 /*
1607 * we're running now.
1608 */
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 s->sbm_state = sbmac_state_on;
Ralf Baechle74b02472005-10-19 15:40:02 +01001611
1612 /*
1613 * Program multicast addresses
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 sbmac_setmulti(s);
Ralf Baechle74b02472005-10-19 15:40:02 +01001617
1618 /*
1619 * If channel was in promiscuous mode before, turn that on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (s->sbm_devflags & IFF_PROMISC) {
1623 sbmac_promiscuous_mode(s,1);
1624 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628
1629/**********************************************************************
1630 * SBMAC_CHANNEL_STOP(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001631 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 * Stop packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001633 *
1634 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001636 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * Return value:
1638 * nothing
1639 ********************************************************************* */
1640
1641static void sbmac_channel_stop(struct sbmac_softc *s)
1642{
1643 /* don't do this if already stopped */
Ralf Baechle74b02472005-10-19 15:40:02 +01001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (s->sbm_state == sbmac_state_off)
1646 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /* don't accept any packets, disable all interrupts */
Ralf Baechle74b02472005-10-19 15:40:02 +01001649
Ralf Baechle20399732005-10-19 15:39:05 +01001650 __raw_writeq(0, s->sbm_rxfilter);
1651 __raw_writeq(0, s->sbm_imr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* Turn off ticker */
Ralf Baechle74b02472005-10-19 15:40:02 +01001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /* XXX */
Ralf Baechle74b02472005-10-19 15:40:02 +01001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /* turn off receiver and transmitter */
Ralf Baechle74b02472005-10-19 15:40:02 +01001658
Ralf Baechle20399732005-10-19 15:39:05 +01001659 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /* We're stopped now. */
Ralf Baechle74b02472005-10-19 15:40:02 +01001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * Stop DMA channels (rings should be ok now)
1667 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 sbdma_channel_stop(&(s->sbm_rxdma));
1670 sbdma_channel_stop(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 /* Empty the receive and transmit rings */
Ralf Baechle74b02472005-10-19 15:40:02 +01001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 sbdma_emptyring(&(s->sbm_rxdma));
1675 sbdma_emptyring(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679/**********************************************************************
1680 * SBMAC_SET_CHANNEL_STATE(state)
Ralf Baechle74b02472005-10-19 15:40:02 +01001681 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * Set the channel's state ON or OFF
Ralf Baechle74b02472005-10-19 15:40:02 +01001683 *
1684 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * state - new state
Ralf Baechle74b02472005-10-19 15:40:02 +01001686 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * Return value:
1688 * old state
1689 ********************************************************************* */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001690static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1691 enum sbmac_state state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001693 enum sbmac_state oldstate = sc->sbm_state;
Ralf Baechle74b02472005-10-19 15:40:02 +01001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * If same as previous state, return
1697 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (state == oldstate) {
1700 return oldstate;
1701 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001704 * If new state is ON, turn channel on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (state == sbmac_state_on) {
1708 sbmac_channel_start(sc);
1709 }
1710 else {
1711 sbmac_channel_stop(sc);
1712 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
1715 * Return previous state
1716 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return oldstate;
1719}
1720
1721
1722/**********************************************************************
1723 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001724 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 * Turn on or off promiscuous mode
Ralf Baechle74b02472005-10-19 15:40:02 +01001726 *
1727 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * sc - softc
1729 * onoff - 1 to turn on, 0 to turn off
Ralf Baechle74b02472005-10-19 15:40:02 +01001730 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * Return value:
1732 * nothing
1733 ********************************************************************* */
1734
1735static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1736{
1737 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (sc->sbm_state != sbmac_state_on)
1740 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (onoff) {
Ralf Baechle20399732005-10-19 15:39:05 +01001743 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 reg |= M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001745 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 else {
Ralf Baechle20399732005-10-19 15:39:05 +01001748 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 reg &= ~M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001750 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752}
1753
1754/**********************************************************************
1755 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001756 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * Set the iphdr offset as 15 assuming ethernet encapsulation
Ralf Baechle74b02472005-10-19 15:40:02 +01001758 *
1759 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01001761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 * Return value:
1763 * nothing
1764 ********************************************************************* */
1765
1766static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1767{
1768 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* Hard code the off set to 15 for now */
Ralf Baechle20399732005-10-19 15:39:05 +01001771 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
Ralf Baechle20399732005-10-19 15:39:05 +01001773 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001774
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001775 /* BCM1250 pass1 didn't have hardware checksum. Everything
1776 later does. */
1777 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 sc->rx_hw_checksum = DISABLE;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001779 } else {
1780 sc->rx_hw_checksum = ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782}
1783
1784
1785/**********************************************************************
1786 * SBMAC_ADDR2REG(ptr)
Ralf Baechle74b02472005-10-19 15:40:02 +01001787 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * Convert six bytes into the 64-bit register value that
1789 * we typically write into the SBMAC's address/mcast registers
Ralf Baechle74b02472005-10-19 15:40:02 +01001790 *
1791 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 * ptr - pointer to 6 bytes
Ralf Baechle74b02472005-10-19 15:40:02 +01001793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 * Return value:
1795 * register value
1796 ********************************************************************* */
1797
1798static uint64_t sbmac_addr2reg(unsigned char *ptr)
1799{
1800 uint64_t reg = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 ptr += 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01001803
1804 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001806 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001808 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001810 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001812 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001814 reg |= (uint64_t) *(--ptr);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return reg;
1817}
1818
1819
1820/**********************************************************************
1821 * SBMAC_SET_SPEED(s,speed)
Ralf Baechle74b02472005-10-19 15:40:02 +01001822 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 * Configure LAN speed for the specified MAC.
1824 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001825 *
1826 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * s - sbmac structure
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001828 * speed - speed to set MAC to (see enum sbmac_speed)
Ralf Baechle74b02472005-10-19 15:40:02 +01001829 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * Return value:
1831 * 1 if successful
1832 * 0 indicates invalid parameters
1833 ********************************************************************* */
1834
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001835static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 uint64_t cfg;
1838 uint64_t framecfg;
1839
1840 /*
1841 * Save new current values
1842 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 s->sbm_speed = speed;
Ralf Baechle74b02472005-10-19 15:40:02 +01001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (s->sbm_state == sbmac_state_on)
1847 return 0; /* save for next restart */
1848
1849 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001850 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001852
Ralf Baechle20399732005-10-19 15:39:05 +01001853 cfg = __raw_readq(s->sbm_maccfg);
1854 framecfg = __raw_readq(s->sbm_framecfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /*
1857 * Mask out the stuff we want to change
1858 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1861 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1862 M_MAC_SLOT_SIZE);
Ralf Baechle74b02472005-10-19 15:40:02 +01001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /*
1865 * Now add in the new bits
1866 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 switch (speed) {
1869 case sbmac_speed_10:
1870 framecfg |= V_MAC_IFG_RX_10 |
1871 V_MAC_IFG_TX_10 |
1872 K_MAC_IFG_THRSH_10 |
1873 V_MAC_SLOT_SIZE_10;
1874 cfg |= V_MAC_SPEED_SEL_10MBPS;
1875 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case sbmac_speed_100:
1878 framecfg |= V_MAC_IFG_RX_100 |
1879 V_MAC_IFG_TX_100 |
1880 V_MAC_IFG_THRSH_100 |
1881 V_MAC_SLOT_SIZE_100;
1882 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1883 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 case sbmac_speed_1000:
1886 framecfg |= V_MAC_IFG_RX_1000 |
1887 V_MAC_IFG_TX_1000 |
1888 V_MAC_IFG_THRSH_1000 |
1889 V_MAC_SLOT_SIZE_1000;
1890 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1891 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 default:
1894 return 0;
1895 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001898 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001900
Ralf Baechle20399732005-10-19 15:39:05 +01001901 __raw_writeq(framecfg, s->sbm_framecfg);
1902 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return 1;
1905}
1906
1907/**********************************************************************
1908 * SBMAC_SET_DUPLEX(s,duplex,fc)
Ralf Baechle74b02472005-10-19 15:40:02 +01001909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 * Set Ethernet duplex and flow control options for this MAC
1911 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001912 *
1913 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 * s - sbmac structure
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001915 * duplex - duplex setting (see enum sbmac_duplex)
1916 * fc - flow control setting (see enum sbmac_fc)
Ralf Baechle74b02472005-10-19 15:40:02 +01001917 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * Return value:
1919 * 1 if ok
1920 * 0 if an invalid parameter combination was specified
1921 ********************************************************************* */
1922
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001923static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1924 enum sbmac_fc fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 uint64_t cfg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /*
1929 * Save new current values
1930 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 s->sbm_duplex = duplex;
1933 s->sbm_fc = fc;
Ralf Baechle74b02472005-10-19 15:40:02 +01001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (s->sbm_state == sbmac_state_on)
1936 return 0; /* save for next restart */
Ralf Baechle74b02472005-10-19 15:40:02 +01001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001939 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001941
Ralf Baechle20399732005-10-19 15:39:05 +01001942 cfg = __raw_readq(s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /*
1945 * Mask off the stuff we're about to change
1946 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
Ralf Baechle74b02472005-10-19 15:40:02 +01001949
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 switch (duplex) {
1952 case sbmac_duplex_half:
1953 switch (fc) {
1954 case sbmac_fc_disabled:
1955 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1956 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 case sbmac_fc_collision:
1959 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1960 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 case sbmac_fc_carrier:
1963 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1964 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 case sbmac_fc_frame: /* not valid in half duplex */
1967 default: /* invalid selection */
1968 return 0;
1969 }
1970 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 case sbmac_duplex_full:
1973 switch (fc) {
1974 case sbmac_fc_disabled:
1975 cfg |= V_MAC_FC_CMD_DISABLED;
1976 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 case sbmac_fc_frame:
1979 cfg |= V_MAC_FC_CMD_ENABLED;
1980 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 case sbmac_fc_collision: /* not valid in full duplex */
1983 case sbmac_fc_carrier: /* not valid in full duplex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 default:
1985 return 0;
1986 }
1987 break;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01001988 default:
1989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001993 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001995
Ralf Baechle20399732005-10-19 15:39:05 +01001996 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 1;
1999}
2000
2001
2002
2003
2004/**********************************************************************
2005 * SBMAC_INTR()
Ralf Baechle74b02472005-10-19 15:40:02 +01002006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * Interrupt handler for MAC interrupts
Ralf Baechle74b02472005-10-19 15:40:02 +01002008 *
2009 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * MAC structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * Return value:
2013 * nothing
2014 ********************************************************************* */
David Howells7d12e782006-10-05 14:55:46 +01002015static irqreturn_t sbmac_intr(int irq,void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct net_device *dev = (struct net_device *) dev_instance;
2018 struct sbmac_softc *sc = netdev_priv(dev);
2019 uint64_t isr;
2020 int handled = 0;
2021
Mark Mason693aa942007-04-26 00:23:22 -07002022 /*
2023 * Read the ISR (this clears the bits in the real
2024 * register, except for counter addr)
2025 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002026
Mark Mason693aa942007-04-26 00:23:22 -07002027 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
Ralf Baechle74b02472005-10-19 15:40:02 +01002028
Mark Mason693aa942007-04-26 00:23:22 -07002029 if (isr == 0)
2030 return IRQ_RETVAL(0);
2031 handled = 1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002032
Mark Mason693aa942007-04-26 00:23:22 -07002033 /*
2034 * Transmits on channel 0
2035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002037 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
Mark Mason693aa942007-04-26 00:23:22 -07002038 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
Ralf Baechle74b02472005-10-19 15:40:02 +01002039
Mark Mason693aa942007-04-26 00:23:22 -07002040 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002041 if (napi_schedule_prep(&sc->napi)) {
Mark Mason693aa942007-04-26 00:23:22 -07002042 __raw_writeq(0, sc->sbm_imr);
Ben Hutchings288379f2009-01-19 16:43:59 -08002043 __napi_schedule(&sc->napi);
Mark Mason693aa942007-04-26 00:23:22 -07002044 /* Depend on the exit from poll to reenable intr */
2045 }
2046 else {
2047 /* may leave some packets behind */
2048 sbdma_rx_process(sc,&(sc->sbm_rxdma),
2049 SBMAC_MAX_RXDESCR * 2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 }
2052 return IRQ_RETVAL(handled);
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055/**********************************************************************
2056 * SBMAC_START_TX(skb,dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002057 *
2058 * Start output on the specified interface. Basically, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 * queue as many buffers as we can until the ring fills up, or
2060 * we run off the end of the queue, whichever comes first.
Ralf Baechle74b02472005-10-19 15:40:02 +01002061 *
2062 * Input parameters:
2063 *
2064 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * Return value:
2066 * nothing
2067 ********************************************************************* */
2068static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2069{
2070 struct sbmac_softc *sc = netdev_priv(dev);
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002071 unsigned long flags;
Ralf Baechle74b02472005-10-19 15:40:02 +01002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /* lock eth irq */
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002074 spin_lock_irqsave(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002077 * Put the buffer on the transmit ring. If we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 * don't have room, stop the queue.
2079 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2082 /* XXX save skb that we could not send */
2083 netif_stop_queue(dev);
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002084 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Patrick McHardy5b548142009-06-12 06:22:29 +00002086 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 dev->trans_start = jiffies;
Ralf Baechle74b02472005-10-19 15:40:02 +01002090
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002091 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002092
Patrick McHardy6ed10652009-06-23 06:03:08 +00002093 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
2096/**********************************************************************
2097 * SBMAC_SETMULTI(sc)
Ralf Baechle74b02472005-10-19 15:40:02 +01002098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * Reprogram the multicast table into the hardware, given
2100 * the list of multicasts associated with the interface
2101 * structure.
Ralf Baechle74b02472005-10-19 15:40:02 +01002102 *
2103 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01002105 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * Return value:
2107 * nothing
2108 ********************************************************************* */
2109
2110static void sbmac_setmulti(struct sbmac_softc *sc)
2111{
2112 uint64_t reg;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01002113 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 int idx;
2115 struct dev_mc_list *mclist;
2116 struct net_device *dev = sc->sbm_dev;
Ralf Baechle74b02472005-10-19 15:40:02 +01002117
2118 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 * Clear out entire multicast table. We do this by nuking
2120 * the entire hash table and all the direct matches except
Ralf Baechle74b02472005-10-19 15:40:02 +01002121 * the first one, which is used for our station address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2125 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002126 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2130 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002131 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /*
2135 * Clear the filter to say we don't want any multicasts.
2136 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002137
Ralf Baechle20399732005-10-19 15:39:05 +01002138 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002140 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (dev->flags & IFF_ALLMULTI) {
Ralf Baechle74b02472005-10-19 15:40:02 +01002143 /*
2144 * Enable ALL multicasts. Do this by inverting the
2145 * multicast enable bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 */
Ralf Baechle20399732005-10-19 15:39:05 +01002147 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002149 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return;
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Ralf Baechle74b02472005-10-19 15:40:02 +01002153
2154 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * Progam new multicast entries. For now, only use the
2156 * perfect filter. In the future we'll need to use the
2157 * hash filter if the perfect filter overflows
2158 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* XXX only using perfect filter for now, need to use hash
2161 * XXX if the table overflows */
Ralf Baechle74b02472005-10-19 15:40:02 +01002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 idx = 1; /* skip station address */
2164 mclist = dev->mc_list;
2165 while (mclist && (idx < MAC_ADDR_COUNT)) {
2166 reg = sbmac_addr2reg(mclist->dmi_addr);
2167 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002168 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 idx++;
2170 mclist = mclist->next;
2171 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002172
2173 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 * Enable the "accept multicast bits" if we programmed at least one
Ralf Baechle74b02472005-10-19 15:40:02 +01002175 * multicast.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (idx > 1) {
Ralf Baechle20399732005-10-19 15:39:05 +01002179 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 reg |= M_MAC_MCAST_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01002181 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183}
2184
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002185#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186/**********************************************************************
2187 * SBMAC_PARSE_XDIGIT(str)
Ralf Baechle74b02472005-10-19 15:40:02 +01002188 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 * Parse a hex digit, returning its value
Ralf Baechle74b02472005-10-19 15:40:02 +01002190 *
2191 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 * str - character
Ralf Baechle74b02472005-10-19 15:40:02 +01002193 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 * Return value:
2195 * hex value, or -1 if invalid
2196 ********************************************************************* */
2197
2198static int sbmac_parse_xdigit(char str)
2199{
2200 int digit;
Ralf Baechle74b02472005-10-19 15:40:02 +01002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if ((str >= '0') && (str <= '9'))
2203 digit = str - '0';
2204 else if ((str >= 'a') && (str <= 'f'))
2205 digit = str - 'a' + 10;
2206 else if ((str >= 'A') && (str <= 'F'))
2207 digit = str - 'A' + 10;
2208 else
2209 return -1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return digit;
2212}
2213
2214/**********************************************************************
2215 * SBMAC_PARSE_HWADDR(str,hwaddr)
Ralf Baechle74b02472005-10-19 15:40:02 +01002216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
2218 * Ethernet address.
Ralf Baechle74b02472005-10-19 15:40:02 +01002219 *
2220 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * str - string
2222 * hwaddr - pointer to hardware address
Ralf Baechle74b02472005-10-19 15:40:02 +01002223 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 * Return value:
2225 * 0 if ok, else -1
2226 ********************************************************************* */
2227
2228static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr)
2229{
2230 int digit1,digit2;
2231 int idx = 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 while (*str && (idx > 0)) {
2234 digit1 = sbmac_parse_xdigit(*str);
2235 if (digit1 < 0)
2236 return -1;
2237 str++;
2238 if (!*str)
2239 return -1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if ((*str == ':') || (*str == '-')) {
2242 digit2 = digit1;
2243 digit1 = 0;
2244 }
2245 else {
2246 digit2 = sbmac_parse_xdigit(*str);
2247 if (digit2 < 0)
2248 return -1;
2249 str++;
2250 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 *hwaddr++ = (digit1 << 4) | digit2;
2253 idx--;
Ralf Baechle74b02472005-10-19 15:40:02 +01002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (*str == '-')
2256 str++;
2257 if (*str == ':')
2258 str++;
2259 }
2260 return 0;
2261}
2262#endif
2263
2264static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2265{
2266 if (new_mtu > ENET_PACKET_SIZE)
2267 return -EINVAL;
2268 _dev->mtu = new_mtu;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002269 pr_info("changing the mtu to %d\n", new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return 0;
2271}
2272
Alexander Beregalovb4cf3422009-04-15 12:52:57 +00002273static const struct net_device_ops sbmac_netdev_ops = {
2274 .ndo_open = sbmac_open,
2275 .ndo_stop = sbmac_close,
2276 .ndo_start_xmit = sbmac_start_tx,
2277 .ndo_set_multicast_list = sbmac_set_rx_mode,
2278 .ndo_tx_timeout = sbmac_tx_timeout,
2279 .ndo_do_ioctl = sbmac_mii_ioctl,
2280 .ndo_change_mtu = sb1250_change_mtu,
2281 .ndo_validate_addr = eth_validate_addr,
2282 .ndo_set_mac_address = eth_mac_addr,
2283#ifdef CONFIG_NET_POLL_CONTROLLER
2284 .ndo_poll_controller = sbmac_netpoll,
2285#endif
2286};
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288/**********************************************************************
2289 * SBMAC_INIT(dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002290 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 * Attach routine - init hardware and hook ourselves into linux
Ralf Baechle74b02472005-10-19 15:40:02 +01002292 *
2293 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 * dev - net_device structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002295 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 * Return value:
2297 * status
2298 ********************************************************************* */
2299
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002300static int sbmac_init(struct platform_device *pldev, long long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07002302 struct net_device *dev = dev_get_drvdata(&pldev->dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002303 int idx = pldev->id;
2304 struct sbmac_softc *sc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 unsigned char *eaddr;
2306 uint64_t ea_reg;
2307 int i;
2308 int err;
Ralf Baechle74b02472005-10-19 15:40:02 +01002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 sc->sbm_dev = dev;
2311 sc->sbe_idx = idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 eaddr = sc->sbm_hwaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +01002314
2315 /*
Nick Andrew877d0312009-01-26 11:06:57 +01002316 * Read the ethernet address. The firmware left this programmed
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 * for us in the ethernet address register for each mac.
2318 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002319
Ralf Baechle20399732005-10-19 15:39:05 +01002320 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2321 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 for (i = 0; i < 6; i++) {
2323 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2324 ea_reg >>= 8;
2325 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 for (i = 0; i < 6; i++) {
2328 dev->dev_addr[i] = eaddr[i];
2329 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002330
Ralf Baechle74b02472005-10-19 15:40:02 +01002331 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * Initialize context (get pointers to registers and stuff), then
2333 * allocate the memory for the descriptor tables.
2334 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 sbmac_initctx(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * Set up Linux device callins
2340 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 spin_lock_init(&(sc->sbm_lock));
Ralf Baechle74b02472005-10-19 15:40:02 +01002343
Alexander Beregalovb4cf3422009-04-15 12:52:57 +00002344 dev->netdev_ops = &sbmac_netdev_ops;
2345 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002346
2347 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002349 dev->irq = UNIT_INT(idx);
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* This is needed for PASS2 for Rx H/W checksum feature */
2352 sbmac_set_iphdr_offset(sc);
2353
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002354 sc->mii_bus = mdiobus_alloc();
2355 if (sc->mii_bus == NULL) {
2356 sbmac_uninitctx(sc);
2357 return -ENOMEM;
2358 }
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 err = register_netdev(dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002361 if (err) {
2362 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2363 sbmac_string, idx);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002364 mdiobus_free(sc->mii_bus);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002365 sbmac_uninitctx(sc);
2366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002369 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2370
2371 if (sc->rx_hw_checksum == ENABLE)
2372 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Display Ethernet address (this is called during the config
2376 * process so we need to finish off the config message that
2377 * was being displayed)
2378 */
Johannes Berge1749612008-10-27 15:59:26 -07002379 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
2380 dev->name, base, eaddr);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002381
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002382 sc->mii_bus->name = sbmac_mdio_string;
2383 snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%x", idx);
2384 sc->mii_bus->priv = sc;
2385 sc->mii_bus->read = sbmac_mii_read;
2386 sc->mii_bus->write = sbmac_mii_write;
2387 sc->mii_bus->irq = sc->phy_irq;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002388 for (i = 0; i < PHY_MAX_ADDR; ++i)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002389 sc->mii_bus->irq[i] = SBMAC_PHY_INT;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002390
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002391 sc->mii_bus->parent = &pldev->dev;
2392 dev_set_drvdata(&pldev->dev, sc->mii_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
2397
2398static int sbmac_open(struct net_device *dev)
2399{
2400 struct sbmac_softc *sc = netdev_priv(dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002401 int err;
Ralf Baechle74b02472005-10-19 15:40:02 +01002402
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002403 if (debug > 1)
2404 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
Ralf Baechle74b02472005-10-19 15:40:02 +01002405
2406 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 * map/route interrupt (clear status first, in case something
2408 * weird is pending; we haven't initialized the mac registers
2409 * yet)
2410 */
2411
Ralf Baechle20399732005-10-19 15:39:05 +01002412 __raw_readq(sc->sbm_isr);
Joe Perchesa0607fd2009-11-18 23:29:17 -08002413 err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002414 if (err) {
2415 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2416 dev->irq);
2417 goto out_err;
Ralf Baechle59b81822005-10-20 12:01:28 +01002418 }
2419
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002420 /*
2421 * Probe PHY address
2422 */
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002423 err = mdiobus_register(sc->mii_bus);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002424 if (err) {
2425 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2426 dev->name);
2427 goto out_unirq;
2428 }
2429
2430 sc->sbm_speed = sbmac_speed_none;
2431 sc->sbm_duplex = sbmac_duplex_none;
2432 sc->sbm_fc = sbmac_fc_none;
2433 sc->sbm_pause = -1;
2434 sc->sbm_link = 0;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002435
Ralf Baechle59b81822005-10-20 12:01:28 +01002436 /*
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002437 * Attach to the PHY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002439 err = sbmac_mii_probe(dev);
2440 if (err)
2441 goto out_unregister;
Ralf Baechle74b02472005-10-19 15:40:02 +01002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 /*
2444 * Turn on the channel
2445 */
2446
2447 sbmac_set_channel_state(sc,sbmac_state_on);
Ralf Baechle74b02472005-10-19 15:40:02 +01002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 netif_start_queue(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 sbmac_set_rx_mode(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002452
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002453 phy_start(sc->phy_dev);
2454
2455 napi_enable(&sc->napi);
Ralf Baechle74b02472005-10-19 15:40:02 +01002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 0;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002458
2459out_unregister:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002460 mdiobus_unregister(sc->mii_bus);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002461
2462out_unirq:
2463 free_irq(dev->irq, dev);
2464
2465out_err:
2466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Ralf Baechle59b81822005-10-20 12:01:28 +01002469static int sbmac_mii_probe(struct net_device *dev)
2470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct sbmac_softc *sc = netdev_priv(dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002472 struct phy_device *phy_dev;
2473 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002475 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002476 phy_dev = sc->mii_bus->phy_map[i];
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002477 if (phy_dev)
2478 break;
2479 }
2480 if (!phy_dev) {
2481 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2482 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002484
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08002485 phy_dev = phy_connect(dev, dev_name(&phy_dev->dev), &sbmac_mii_poll, 0,
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002486 PHY_INTERFACE_MODE_GMII);
2487 if (IS_ERR(phy_dev)) {
2488 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2489 return PTR_ERR(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002491
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002492 /* Remove any features not supported by the controller */
2493 phy_dev->supported &= SUPPORTED_10baseT_Half |
2494 SUPPORTED_10baseT_Full |
2495 SUPPORTED_100baseT_Half |
2496 SUPPORTED_100baseT_Full |
2497 SUPPORTED_1000baseT_Half |
2498 SUPPORTED_1000baseT_Full |
2499 SUPPORTED_Autoneg |
2500 SUPPORTED_MII |
2501 SUPPORTED_Pause |
2502 SUPPORTED_Asym_Pause;
2503 phy_dev->advertising = phy_dev->supported;
Ralf Baechle74b02472005-10-19 15:40:02 +01002504
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002505 pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2506 dev->name, phy_dev->drv->name,
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08002507 dev_name(&phy_dev->dev), phy_dev->irq);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002508
2509 sc->phy_dev = phy_dev;
2510
2511 return 0;
2512}
2513
2514
2515static void sbmac_mii_poll(struct net_device *dev)
2516{
2517 struct sbmac_softc *sc = netdev_priv(dev);
2518 struct phy_device *phy_dev = sc->phy_dev;
2519 unsigned long flags;
2520 enum sbmac_fc fc;
2521 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2522
2523 link_chg = (sc->sbm_link != phy_dev->link);
2524 speed_chg = (sc->sbm_speed != phy_dev->speed);
2525 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2526 pause_chg = (sc->sbm_pause != phy_dev->pause);
2527
2528 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2529 return; /* Hmmm... */
2530
2531 if (!phy_dev->link) {
2532 if (link_chg) {
2533 sc->sbm_link = phy_dev->link;
2534 sc->sbm_speed = sbmac_speed_none;
2535 sc->sbm_duplex = sbmac_duplex_none;
2536 sc->sbm_fc = sbmac_fc_disabled;
2537 sc->sbm_pause = -1;
2538 pr_info("%s: link unavailable\n", dev->name);
2539 }
2540 return;
2541 }
2542
2543 if (phy_dev->duplex == DUPLEX_FULL) {
2544 if (phy_dev->pause)
2545 fc = sbmac_fc_frame;
2546 else
2547 fc = sbmac_fc_disabled;
2548 } else
2549 fc = sbmac_fc_collision;
2550 fc_chg = (sc->sbm_fc != fc);
2551
2552 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2553 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2554
2555 spin_lock_irqsave(&sc->sbm_lock, flags);
2556
2557 sc->sbm_speed = phy_dev->speed;
2558 sc->sbm_duplex = phy_dev->duplex;
2559 sc->sbm_fc = fc;
2560 sc->sbm_pause = phy_dev->pause;
2561 sc->sbm_link = phy_dev->link;
2562
2563 if ((speed_chg || duplex_chg || fc_chg) &&
2564 sc->sbm_state != sbmac_state_off) {
2565 /*
2566 * something changed, restart the channel
2567 */
2568 if (debug > 1)
2569 pr_debug("%s: restarting channel "
2570 "because PHY state changed\n", dev->name);
2571 sbmac_channel_stop(sc);
2572 sbmac_channel_start(sc);
2573 }
2574
2575 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
2578
2579static void sbmac_tx_timeout (struct net_device *dev)
2580{
2581 struct sbmac_softc *sc = netdev_priv(dev);
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002582 unsigned long flags;
Ralf Baechle74b02472005-10-19 15:40:02 +01002583
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002584 spin_lock_irqsave(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002585
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002588 dev->stats.tx_errors++;
Ralf Baechle74b02472005-10-19 15:40:02 +01002589
Weiwei Wangbe61ea52008-09-18 08:23:48 +08002590 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2593}
2594
2595
2596
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598static void sbmac_set_rx_mode(struct net_device *dev)
2599{
2600 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 struct sbmac_softc *sc = netdev_priv(dev);
2602
2603 spin_lock_irqsave(&sc->sbm_lock, flags);
2604 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2605 /*
2606 * Promiscuous changed.
2607 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002608
2609 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 sbmac_promiscuous_mode(sc,1);
2611 }
2612 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 sbmac_promiscuous_mode(sc,0);
2614 }
2615 }
2616 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /*
2619 * Program the multicasts. Do this every time.
2620 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 sbmac_setmulti(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
2626static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2627{
2628 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002629
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002630 if (!netif_running(dev) || !sc->phy_dev)
2631 return -EINVAL;
Ralf Baechle74b02472005-10-19 15:40:02 +01002632
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002633 return phy_mii_ioctl(sc->phy_dev, if_mii(rq), cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
2636static int sbmac_close(struct net_device *dev)
2637{
2638 struct sbmac_softc *sc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002640 napi_disable(&sc->napi);
2641
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002642 phy_stop(sc->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002644 sbmac_set_channel_state(sc, sbmac_state_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 netif_stop_queue(dev);
2647
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002648 if (debug > 1)
2649 pr_debug("%s: Shutting down ethercard\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002651 phy_disconnect(sc->phy_dev);
2652 sc->phy_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002654 mdiobus_unregister(sc->mii_bus);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002655
2656 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 sbdma_emptyring(&(sc->sbm_txdma));
2659 sbdma_emptyring(&(sc->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 return 0;
2662}
2663
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002664static int sbmac_poll(struct napi_struct *napi, int budget)
Mark Mason693aa942007-04-26 00:23:22 -07002665{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002666 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2667 struct net_device *dev = sc->sbm_dev;
Mark Mason693aa942007-04-26 00:23:22 -07002668 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002670 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
Mark Mason693aa942007-04-26 00:23:22 -07002671 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2672
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002673 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08002674 napi_complete(napi);
Mark Mason693aa942007-04-26 00:23:22 -07002675
2676#ifdef CONFIG_SBMAC_COALESCE
2677 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2678 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2679 sc->sbm_imr);
2680#else
2681 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2682 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2683#endif
2684 }
2685
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002686 return work_done;
Mark Mason693aa942007-04-26 00:23:22 -07002687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002689
Uwe Kleine-Königffe85532009-07-21 11:25:25 +00002690static int __devinit sbmac_probe(struct platform_device *pldev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002692 struct net_device *dev;
2693 struct sbmac_softc *sc;
2694 void __iomem *sbm_base;
2695 struct resource *res;
2696 u64 sbmac_orig_hwaddr;
2697 int err;
2698
2699 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2700 BUG_ON(!res);
2701 sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
2702 if (!sbm_base) {
2703 printk(KERN_ERR "%s: unable to map device registers\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08002704 dev_name(&pldev->dev));
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002705 err = -ENOMEM;
2706 goto out_out;
2707 }
2708
2709 /*
2710 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2711 * value for us by the firmware if we're going to use this MAC.
2712 * If we find a zero, skip this MAC.
2713 */
2714 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08002715 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002716 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2717 if (sbmac_orig_hwaddr == 0) {
2718 err = 0;
2719 goto out_unmap;
2720 }
2721
2722 /*
2723 * Okay, cool. Initialize this MAC.
2724 */
2725 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2726 if (!dev) {
2727 printk(KERN_ERR "%s: unable to allocate etherdev\n",
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08002728 dev_name(&pldev->dev));
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002729 err = -ENOMEM;
2730 goto out_unmap;
2731 }
2732
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07002733 dev_set_drvdata(&pldev->dev, dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002734 SET_NETDEV_DEV(dev, &pldev->dev);
2735
2736 sc = netdev_priv(dev);
2737 sc->sbm_base = sbm_base;
2738
2739 err = sbmac_init(pldev, res->start);
2740 if (err)
2741 goto out_kfree;
2742
2743 return 0;
2744
2745out_kfree:
2746 free_netdev(dev);
2747 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2748
2749out_unmap:
2750 iounmap(sbm_base);
2751
2752out_out:
2753 return err;
2754}
2755
2756static int __exit sbmac_remove(struct platform_device *pldev)
2757{
Greg Kroah-Hartmanc7ae0112009-05-04 21:33:19 -07002758 struct net_device *dev = dev_get_drvdata(&pldev->dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002759 struct sbmac_softc *sc = netdev_priv(dev);
2760
2761 unregister_netdev(dev);
2762 sbmac_uninitctx(sc);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002763 mdiobus_free(sc->mii_bus);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002764 iounmap(sc->sbm_base);
2765 free_netdev(dev);
2766
2767 return 0;
2768}
2769
2770
2771static struct platform_device **sbmac_pldev;
2772static int sbmac_max_units;
2773
2774#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
2775static void __init sbmac_setup_hwaddr(int idx, char *addr)
2776{
2777 void __iomem *sbm_base;
2778 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 uint8_t eaddr[6];
2780 uint64_t val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002782 if (idx >= sbmac_max_units)
2783 return;
2784
2785 start = A_MAC_CHANNEL_BASE(idx);
2786 end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2787
2788 sbm_base = ioremap_nocache(start, end - start + 1);
2789 if (!sbm_base) {
2790 printk(KERN_ERR "%s: unable to map device registers\n",
2791 sbmac_string);
2792 return;
2793 }
2794
2795 sbmac_parse_hwaddr(addr, eaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 val = sbmac_addr2reg(eaddr);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002797 __raw_writeq(val, sbm_base + R_MAC_ETHERNET_ADDR);
2798 val = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2799
2800 iounmap(sbm_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802#endif
2803
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002804static int __init sbmac_platform_probe_one(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002806 struct platform_device *pldev;
2807 struct {
2808 struct resource r;
2809 char name[strlen(sbmac_pretty) + 4];
2810 } *res;
2811 int err;
2812
2813 res = kzalloc(sizeof(*res), GFP_KERNEL);
2814 if (!res) {
2815 printk(KERN_ERR "%s.%d: unable to allocate memory\n",
2816 sbmac_string, idx);
2817 err = -ENOMEM;
2818 goto out_err;
2819 }
2820
2821 /*
2822 * This is the base address of the MAC.
2823 */
2824 snprintf(res->name, sizeof(res->name), "%s %d", sbmac_pretty, idx);
2825 res->r.name = res->name;
2826 res->r.flags = IORESOURCE_MEM;
2827 res->r.start = A_MAC_CHANNEL_BASE(idx);
2828 res->r.end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2829
2830 pldev = platform_device_register_simple(sbmac_string, idx, &res->r, 1);
2831 if (IS_ERR(pldev)) {
2832 printk(KERN_ERR "%s.%d: unable to register platform device\n",
2833 sbmac_string, idx);
2834 err = PTR_ERR(pldev);
2835 goto out_kfree;
2836 }
2837
2838 if (!pldev->dev.driver) {
2839 err = 0; /* No hardware at this address. */
2840 goto out_unregister;
2841 }
2842
2843 sbmac_pldev[idx] = pldev;
2844 return 0;
2845
2846out_unregister:
2847 platform_device_unregister(pldev);
2848
2849out_kfree:
2850 kfree(res);
2851
2852out_err:
2853 return err;
2854}
2855
2856static void __init sbmac_platform_probe(void)
2857{
2858 int i;
Ralf Baechle74b02472005-10-19 15:40:02 +01002859
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002860 /* Set the number of available units based on the SOC type. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 switch (soc_type) {
2862 case K_SYS_SOC_TYPE_BCM1250:
2863 case K_SYS_SOC_TYPE_BCM1250_ALT:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002864 sbmac_max_units = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 break;
2866 case K_SYS_SOC_TYPE_BCM1120:
2867 case K_SYS_SOC_TYPE_BCM1125:
2868 case K_SYS_SOC_TYPE_BCM1125H:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002869 case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
2870 sbmac_max_units = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 break;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002872 case K_SYS_SOC_TYPE_BCM1x55:
2873 case K_SYS_SOC_TYPE_BCM1x80:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002874 sbmac_max_units = 4;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002875 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 default:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002877 return; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002880 /*
2881 * For bringup when not using the firmware, we can pre-fill
2882 * the MAC addresses using the environment variables
2883 * specified in this file (or maybe from the config file?)
2884 */
2885#ifdef SBMAC_ETH0_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002886 sbmac_setup_hwaddr(0, SBMAC_ETH0_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002887#endif
2888#ifdef SBMAC_ETH1_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002889 sbmac_setup_hwaddr(1, SBMAC_ETH1_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002890#endif
2891#ifdef SBMAC_ETH2_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002892 sbmac_setup_hwaddr(2, SBMAC_ETH2_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002893#endif
2894#ifdef SBMAC_ETH3_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002895 sbmac_setup_hwaddr(3, SBMAC_ETH3_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002896#endif
2897
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002898 sbmac_pldev = kcalloc(sbmac_max_units, sizeof(*sbmac_pldev),
2899 GFP_KERNEL);
2900 if (!sbmac_pldev) {
2901 printk(KERN_ERR "%s: unable to allocate memory\n",
2902 sbmac_string);
2903 return;
2904 }
2905
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002906 /*
2907 * Walk through the Ethernet controllers and find
2908 * those who have their MAC addresses set.
2909 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002910 for (i = 0; i < sbmac_max_units; i++)
2911 if (sbmac_platform_probe_one(i))
2912 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
2915
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002916static void __exit sbmac_platform_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002918 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002920 for (i = 0; i < sbmac_max_units; i++)
2921 platform_device_unregister(sbmac_pldev[i]);
2922 kfree(sbmac_pldev);
2923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002925
2926static struct platform_driver sbmac_driver = {
2927 .probe = sbmac_probe,
2928 .remove = __exit_p(sbmac_remove),
2929 .driver = {
2930 .name = sbmac_string,
2931 },
2932};
2933
2934static int __init sbmac_init_module(void)
2935{
2936 int err;
2937
2938 err = platform_driver_register(&sbmac_driver);
2939 if (err)
2940 return err;
2941
2942 sbmac_platform_probe();
2943
2944 return err;
2945}
2946
2947static void __exit sbmac_cleanup_module(void)
2948{
2949 sbmac_platform_cleanup();
2950 platform_driver_unregister(&sbmac_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
2953module_init(sbmac_init_module);
2954module_exit(sbmac_cleanup_module);