blob: 7b53d658e33712fa65d11e4932e260a6a8c3beca [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
182#define ETHER_ALIGN 2
183#define ETHER_ADDR_LEN 6
Ralf Baechle74b02472005-10-19 15:40:02 +0100184#define ENET_PACKET_SIZE 1518
185/*#define ENET_PACKET_SIZE 9216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/**********************************************************************
188 * DMA Descriptor structure
189 ********************************************************************* */
190
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100191struct sbdmadscr {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 uint64_t dscr_a;
193 uint64_t dscr_b;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/**********************************************************************
197 * DMA Controller structure
198 ********************************************************************* */
199
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100200struct sbmacdma {
Ralf Baechle74b02472005-10-19 15:40:02 +0100201
202 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * This stuff is used to identify the channel and the registers
204 * associated with it.
205 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100206 struct sbmac_softc *sbdma_eth; /* back pointer to associated
207 MAC */
208 int sbdma_channel; /* channel number */
209 int sbdma_txdir; /* direction (1=transmit) */
210 int sbdma_maxdescr; /* total # of descriptors
211 in ring */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#ifdef CONFIG_SBMAC_COALESCE
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100213 int sbdma_int_pktcnt;
214 /* # descriptors rx/tx
215 before interrupt */
216 int sbdma_int_timeout;
217 /* # usec rx/tx interrupt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#endif
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100219 void __iomem *sbdma_config0; /* DMA config register 0 */
220 void __iomem *sbdma_config1; /* DMA config register 1 */
221 void __iomem *sbdma_dscrbase;
222 /* descriptor base address */
223 void __iomem *sbdma_dscrcnt; /* descriptor count register */
224 void __iomem *sbdma_curdscr; /* current descriptor
225 address */
226 void __iomem *sbdma_oodpktlost;
227 /* pkt drop (rx only) */
Ralf Baechle74b02472005-10-19 15:40:02 +0100228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /*
230 * This stuff is for maintenance of the ring
231 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100232 void *sbdma_dscrtable_unaligned;
233 struct sbdmadscr *sbdma_dscrtable;
234 /* base of descriptor table */
235 struct sbdmadscr *sbdma_dscrtable_end;
236 /* end of descriptor table */
237 struct sk_buff **sbdma_ctxtable;
238 /* context table, one
239 per descr */
240 dma_addr_t sbdma_dscrtable_phys;
241 /* and also the phys addr */
242 struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
243 struct sbdmadscr *sbdma_remptr; /* next dscr for sw
244 to remove */
245};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247
248/**********************************************************************
249 * Ethernet softc structure
250 ********************************************************************* */
251
252struct sbmac_softc {
Ralf Baechle74b02472005-10-19 15:40:02 +0100253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
255 * Linux-specific things
256 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100257 struct net_device *sbm_dev; /* pointer to linux device */
258 struct napi_struct napi;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100259 struct phy_device *phy_dev; /* the associated PHY device */
260 struct mii_bus mii_bus; /* the MII bus */
261 int phy_irq[PHY_MAX_ADDR];
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100262 spinlock_t sbm_lock; /* spin lock */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100263 int sbm_devflags; /* current device flags */
Ralf Baechle74b02472005-10-19 15:40:02 +0100264
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100265 int sbm_buffersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 /*
268 * Controller-specific things
269 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100270 void __iomem *sbm_base; /* MAC's base address */
271 enum sbmac_state sbm_state; /* current state */
Ralf Baechle74b02472005-10-19 15:40:02 +0100272
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100273 void __iomem *sbm_macenable; /* MAC Enable Register */
274 void __iomem *sbm_maccfg; /* MAC Config Register */
275 void __iomem *sbm_fifocfg; /* FIFO Config Register */
276 void __iomem *sbm_framecfg; /* Frame Config Register */
277 void __iomem *sbm_rxfilter; /* Receive Filter Register */
278 void __iomem *sbm_isr; /* Interrupt Status Register */
279 void __iomem *sbm_imr; /* Interrupt Mask Register */
280 void __iomem *sbm_mdio; /* MDIO Register */
Ralf Baechle74b02472005-10-19 15:40:02 +0100281
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100282 enum sbmac_speed sbm_speed; /* current speed */
283 enum sbmac_duplex sbm_duplex; /* current duplex */
284 enum sbmac_fc sbm_fc; /* cur. flow control setting */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100285 int sbm_pause; /* current pause setting */
286 int sbm_link; /* current link state */
Ralf Baechle74b02472005-10-19 15:40:02 +0100287
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100288 unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
Ralf Baechle74b02472005-10-19 15:40:02 +0100289
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100290 struct sbmacdma sbm_txdma; /* only channel 0 for now */
291 struct sbmacdma sbm_rxdma;
292 int rx_hw_checksum;
293 int sbe_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294};
295
296
297/**********************************************************************
298 * Externs
299 ********************************************************************* */
300
301/**********************************************************************
302 * Prototypes
303 ********************************************************************* */
304
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100305static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
306 int txrx, int maxdescr);
307static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
308static int sbdma_add_rcvbuffer(struct sbmacdma *d, struct sk_buff *m);
309static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
310static void sbdma_emptyring(struct sbmacdma *d);
311static void sbdma_fillring(struct sbmacdma *d);
312static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
313 int work_to_do, int poll);
314static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
315 int poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316static int sbmac_initctx(struct sbmac_softc *s);
317static void sbmac_channel_start(struct sbmac_softc *s);
318static void sbmac_channel_stop(struct sbmac_softc *s);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100319static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
320 enum sbmac_state);
321static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static uint64_t sbmac_addr2reg(unsigned char *ptr);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100323static irqreturn_t sbmac_intr(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
325static void sbmac_setmulti(struct sbmac_softc *sc);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100326static int sbmac_init(struct platform_device *pldev, long long base);
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100327static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
328static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
329 enum sbmac_fc fc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331static int sbmac_open(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static void sbmac_tx_timeout (struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void sbmac_set_rx_mode(struct net_device *dev);
334static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
335static int sbmac_close(struct net_device *dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700336static int sbmac_poll(struct napi_struct *napi, int budget);
Mark Mason693aa942007-04-26 00:23:22 -0700337
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100338static void sbmac_mii_poll(struct net_device *dev);
Ralf Baechle59b81822005-10-20 12:01:28 +0100339static int sbmac_mii_probe(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100341static void sbmac_mii_sync(void __iomem *sbm_mdio);
342static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100343 int bitcnt);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100344static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
345static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
346 u16 val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348
349/**********************************************************************
350 * Globals
351 ********************************************************************* */
352
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100353static char sbmac_string[] = "sb1250-mac";
354static char sbmac_pretty[] = "SB1250 MAC";
355
356static char sbmac_mdio_string[] = "sb1250-mac-mdio";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358
359/**********************************************************************
360 * MDIO constants
361 ********************************************************************* */
362
363#define MII_COMMAND_START 0x01
364#define MII_COMMAND_READ 0x02
365#define MII_COMMAND_WRITE 0x01
366#define MII_COMMAND_ACK 0x02
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
369
370#define ENABLE 1
371#define DISABLE 0
372
373/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100374 * SBMAC_MII_SYNC(sbm_mdio)
Ralf Baechle74b02472005-10-19 15:40:02 +0100375 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Synchronize with the MII - send a pattern of bits to the MII
377 * that will guarantee that it is ready to accept a command.
Ralf Baechle74b02472005-10-19 15:40:02 +0100378 *
379 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100380 * sbm_mdio - address of the MAC's MDIO register
Ralf Baechle74b02472005-10-19 15:40:02 +0100381 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * Return value:
383 * nothing
384 ********************************************************************* */
385
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100386static void sbmac_mii_sync(void __iomem *sbm_mdio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 int cnt;
389 uint64_t bits;
390 int mac_mdio_genc;
391
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100392 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
Ralf Baechle74b02472005-10-19 15:40:02 +0100395
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100396 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 for (cnt = 0; cnt < 32; cnt++) {
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100399 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
400 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402}
403
404/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100405 * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
Ralf Baechle74b02472005-10-19 15:40:02 +0100406 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * Send some bits to the MII. The bits to be sent are right-
408 * justified in the 'data' parameter.
Ralf Baechle74b02472005-10-19 15:40:02 +0100409 *
410 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100411 * sbm_mdio - address of the MAC's MDIO register
412 * data - data to send
413 * bitcnt - number of bits to send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 ********************************************************************* */
415
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100416static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
417 int bitcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 int i;
420 uint64_t bits;
421 unsigned int curmask;
422 int mac_mdio_genc;
423
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100424 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 bits = M_MAC_MDIO_DIR_OUTPUT;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100427 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 curmask = 1 << (bitcnt - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 for (i = 0; i < bitcnt; i++) {
432 if (data & curmask)
433 bits |= M_MAC_MDIO_OUT;
434 else bits &= ~M_MAC_MDIO_OUT;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100435 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
436 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
437 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 curmask >>= 1;
439 }
440}
441
442
443
444/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100445 * SBMAC_MII_READ(bus, phyaddr, regidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * Read a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100447 *
448 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100449 * bus - MDIO bus handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * phyaddr - PHY's address
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100451 * regnum - index of register to read
Ralf Baechle74b02472005-10-19 15:40:02 +0100452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * Return value:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100454 * value read, or 0xffff if an error occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ********************************************************************* */
456
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100457static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100459 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
460 void __iomem *sbm_mdio = sc->sbm_mdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int idx;
462 int error;
463 int regval;
464 int mac_mdio_genc;
465
466 /*
467 * Synchronize ourselves so that the PHY knows the next
468 * thing coming down is a command
469 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100470 sbmac_mii_sync(sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * Send the data to the PHY. The sequence is
474 * a "start" command (2 bits)
475 * a "read" command (2 bits)
476 * the PHY addr (5 bits)
477 * the register index (5 bits)
478 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100479 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
480 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
481 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
482 sbmac_mii_senddata(sbm_mdio, regidx, 5);
Ralf Baechle74b02472005-10-19 15:40:02 +0100483
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100484 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Ralf Baechle74b02472005-10-19 15:40:02 +0100485
486 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * Switch the port around without a clock transition.
488 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100489 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /*
492 * Send out a clock pulse to signal we want the status
493 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100494 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
495 sbm_mdio);
496 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100497
498 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * If an error occurred, the PHY will signal '1' back
500 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100501 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
Ralf Baechle74b02472005-10-19 15:40:02 +0100502
503 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * Issue an 'idle' clock pulse, but keep the direction
505 * the same.
506 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100507 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
508 sbm_mdio);
509 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 regval = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +0100512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 for (idx = 0; idx < 16; idx++) {
514 regval <<= 1;
Ralf Baechle74b02472005-10-19 15:40:02 +0100515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (error == 0) {
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100517 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 regval |= 1;
519 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100520
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100521 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
522 sbm_mdio);
523 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Switch back to output */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100527 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (error == 0)
530 return regval;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100531 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534
535/**********************************************************************
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100536 * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
Ralf Baechle74b02472005-10-19 15:40:02 +0100537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Write a value to a PHY register.
Ralf Baechle74b02472005-10-19 15:40:02 +0100539 *
540 * Input parameters:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100541 * bus - MDIO bus handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * phyaddr - PHY to use
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100543 * regidx - register within the PHY
544 * regval - data to write to register
Ralf Baechle74b02472005-10-19 15:40:02 +0100545 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * Return value:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100547 * 0 for success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ********************************************************************* */
549
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100550static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
551 u16 regval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100553 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
554 void __iomem *sbm_mdio = sc->sbm_mdio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int mac_mdio_genc;
556
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100557 sbmac_mii_sync(sbm_mdio);
Ralf Baechle74b02472005-10-19 15:40:02 +0100558
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100559 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
560 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
561 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
562 sbmac_mii_senddata(sbm_mdio, regidx, 5);
563 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
564 sbmac_mii_senddata(sbm_mdio, regval, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100566 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100568 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
569
570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573
574
575/**********************************************************************
576 * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
Ralf Baechle74b02472005-10-19 15:40:02 +0100577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * Initialize a DMA channel context. Since there are potentially
579 * eight DMA channels per MAC, it's nice to do this in a standard
Ralf Baechle74b02472005-10-19 15:40:02 +0100580 * way.
581 *
582 * Input parameters:
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100583 * d - struct sbmacdma (DMA channel context)
584 * s - struct sbmac_softc (pointer to a MAC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 * chan - channel number (0..1 right now)
586 * txrx - Identifies DMA_TX or DMA_RX for channel direction
587 * maxdescr - number of descriptors
Ralf Baechle74b02472005-10-19 15:40:02 +0100588 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * Return value:
590 * nothing
591 ********************************************************************* */
592
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100593static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
594 int txrx, int maxdescr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Mark Mason693aa942007-04-26 00:23:22 -0700596#ifdef CONFIG_SBMAC_COALESCE
597 int int_pktcnt, int_timeout;
598#endif
599
Ralf Baechle74b02472005-10-19 15:40:02 +0100600 /*
601 * Save away interesting stuff in the structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 d->sbdma_eth = s;
605 d->sbdma_channel = chan;
606 d->sbdma_txdir = txrx;
Ralf Baechle74b02472005-10-19 15:40:02 +0100607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#if 0
609 /* RMON clearing */
610 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
611#endif
612
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +0100613 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
614 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
615 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
616 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
617 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
618 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
619 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
620 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
621 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
622 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
623 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
624 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
625 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
626 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
627 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
628 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
629 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
630 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
631 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
632 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
633 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Ralf Baechle74b02472005-10-19 15:40:02 +0100635 /*
636 * initialize register pointers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100638
639 d->sbdma_config0 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100641 d->sbdma_config1 =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100643 d->sbdma_dscrbase =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
Ralf Baechle74b02472005-10-19 15:40:02 +0100645 d->sbdma_dscrcnt =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
Ralf Baechle74b02472005-10-19 15:40:02 +0100647 d->sbdma_curdscr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
Mark Mason693aa942007-04-26 00:23:22 -0700649 if (d->sbdma_txdir)
650 d->sbdma_oodpktlost = NULL;
651 else
652 d->sbdma_oodpktlost =
653 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
Ralf Baechle74b02472005-10-19 15:40:02 +0100654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
656 * Allocate memory for the ring
657 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 d->sbdma_maxdescr = maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100660
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100661 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
662 sizeof(*d->sbdma_dscrtable),
663 GFP_KERNEL);
Ralf Baechle04115de2005-10-10 14:50:36 +0100664
665 /*
666 * The descriptor table must be aligned to at least 16 bytes or the
667 * MAC will corrupt it.
668 */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100669 d->sbdma_dscrtable = (struct sbdmadscr *)
670 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
671 sizeof(*d->sbdma_dscrtable));
Ralf Baechle74b02472005-10-19 15:40:02 +0100672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
Ralf Baechle74b02472005-10-19 15:40:02 +0100676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /*
678 * And context table
679 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100680
Mariusz Kozlowskic477f332007-07-31 23:58:36 +0200681 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100682 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
Ralf Baechle74b02472005-10-19 15:40:02 +0100683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#ifdef CONFIG_SBMAC_COALESCE
685 /*
686 * Setup Rx/Tx DMA coalescing defaults
687 */
688
Mark Mason693aa942007-04-26 00:23:22 -0700689 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if ( int_pktcnt ) {
691 d->sbdma_int_pktcnt = int_pktcnt;
692 } else {
693 d->sbdma_int_pktcnt = 1;
694 }
Ralf Baechle74b02472005-10-19 15:40:02 +0100695
Mark Mason693aa942007-04-26 00:23:22 -0700696 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if ( int_timeout ) {
698 d->sbdma_int_timeout = int_timeout;
699 } else {
700 d->sbdma_int_timeout = 0;
701 }
702#endif
703
704}
705
706/**********************************************************************
707 * SBDMA_CHANNEL_START(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100708 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100710 *
711 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 * d - DMA channel to init (context must be previously init'd
713 * rxtx - DMA_RX or DMA_TX depending on what type of channel
Ralf Baechle74b02472005-10-19 15:40:02 +0100714 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * Return value:
716 * nothing
717 ********************************************************************* */
718
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100719static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 /*
722 * Turn on the DMA channel
723 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +0100726 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
727 0, d->sbdma_config1);
728 __raw_writeq(M_DMA_EOP_INT_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 V_DMA_RINGSZ(d->sbdma_maxdescr) |
730 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
Ralf Baechle20399732005-10-19 15:39:05 +0100731 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732#else
Ralf Baechle20399732005-10-19 15:39:05 +0100733 __raw_writeq(0, d->sbdma_config1);
734 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
735 0, d->sbdma_config0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736#endif
737
Ralf Baechle20399732005-10-19 15:39:05 +0100738 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 /*
741 * Initialize ring pointers
742 */
743
744 d->sbdma_addptr = d->sbdma_dscrtable;
745 d->sbdma_remptr = d->sbdma_dscrtable;
746}
747
748/**********************************************************************
749 * SBDMA_CHANNEL_STOP(d)
Ralf Baechle74b02472005-10-19 15:40:02 +0100750 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * Initialize the hardware registers for a DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +0100752 *
753 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * d - DMA channel to init (context must be previously init'd
Ralf Baechle74b02472005-10-19 15:40:02 +0100755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * Return value:
757 * nothing
758 ********************************************************************* */
759
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100760static void sbdma_channel_stop(struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 /*
763 * Turn off the DMA channel
764 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100765
Ralf Baechle20399732005-10-19 15:39:05 +0100766 __raw_writeq(0, d->sbdma_config1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100767
Ralf Baechle20399732005-10-19 15:39:05 +0100768 __raw_writeq(0, d->sbdma_dscrbase);
Ralf Baechle74b02472005-10-19 15:40:02 +0100769
Ralf Baechle20399732005-10-19 15:39:05 +0100770 __raw_writeq(0, d->sbdma_config0);
Ralf Baechle74b02472005-10-19 15:40:02 +0100771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /*
773 * Zero ring pointers
774 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100775
Ralf Baechle20399732005-10-19 15:39:05 +0100776 d->sbdma_addptr = NULL;
777 d->sbdma_remptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780static void sbdma_align_skb(struct sk_buff *skb,int power2,int offset)
781{
782 unsigned long addr;
783 unsigned long newaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +0100784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 addr = (unsigned long) skb->data;
Ralf Baechle74b02472005-10-19 15:40:02 +0100786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 newaddr = (addr + power2 - 1) & ~(power2 - 1);
Ralf Baechle74b02472005-10-19 15:40:02 +0100788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 skb_reserve(skb,newaddr-addr+offset);
790}
791
792
793/**********************************************************************
794 * SBDMA_ADD_RCVBUFFER(d,sb)
Ralf Baechle74b02472005-10-19 15:40:02 +0100795 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * Add a buffer to the specified DMA channel. For receive channels,
797 * this queues a buffer for inbound packets.
Ralf Baechle74b02472005-10-19 15:40:02 +0100798 *
799 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * d - DMA channel descriptor
801 * sb - sk_buff to add, or NULL if we should allocate one
Ralf Baechle74b02472005-10-19 15:40:02 +0100802 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * Return value:
804 * 0 if buffer could not be added (ring is full)
805 * 1 if buffer added successfully
806 ********************************************************************* */
807
808
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100809static int sbdma_add_rcvbuffer(struct sbmacdma *d, struct sk_buff *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Maciej W. Rozycki73d73962007-09-20 19:14:01 +0100811 struct sbdmadscr *dsc;
812 struct sbdmadscr *nextdsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct sk_buff *sb_new = NULL;
814 int pktsize = ENET_PACKET_SIZE;
Ralf Baechle74b02472005-10-19 15:40:02 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* get pointer to our current place in the ring */
Ralf Baechle74b02472005-10-19 15:40:02 +0100817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dsc = d->sbdma_addptr;
819 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
Ralf Baechle74b02472005-10-19 15:40:02 +0100820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
822 * figure out if the ring is full - if the next descriptor
823 * is the same as the one that we're going to remove from
824 * the ring, the ring is full
825 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (nextdsc == d->sbdma_remptr) {
828 return -ENOSPC;
829 }
830
Ralf Baechle74b02472005-10-19 15:40:02 +0100831 /*
832 * Allocate a sk_buff if we don't already have one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * If we do have an sk_buff, reset it so that it's empty.
834 *
835 * Note: sk_buffs don't seem to be guaranteed to have any sort
836 * of alignment when they are allocated. Therefore, allocate enough
837 * extra space to make sure that:
838 *
839 * 1. the data does not start in the middle of a cache line.
840 * 2. The data does not end in the middle of a cache line
Ralf Baechle74b02472005-10-19 15:40:02 +0100841 * 3. The buffer can be aligned such that the IP addresses are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * naturally aligned.
843 *
844 * Remember, the SOCs MAC writes whole cache lines at a time,
845 * without reading the old contents first. So, if the sk_buff's
846 * data portion starts in the middle of a cache line, the SOC
847 * DMA will trash the beginning (and ending) portions.
848 */
Ralf Baechle74b02472005-10-19 15:40:02 +0100849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (sb == NULL) {
851 sb_new = dev_alloc_skb(ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN);
852 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
858 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, ETHER_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) |
Ralf Baechle20399732005-10-19 15:39:05 +0100877 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_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) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize+ETHER_ALIGN)) |
881 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * d - DMA channel
Ralf Baechle74b02472005-10-19 15:40:02 +01001036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * Return value:
1038 * nothing
1039 ********************************************************************* */
1040
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001041static void sbdma_fillring(struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 int idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 for (idx = 0; idx < SBMAC_MAX_RXDESCR-1; idx++) {
1046 if (sbdma_add_rcvbuffer(d,NULL) != 0)
1047 break;
1048 }
1049}
1050
Deepak Saxenad6830012007-03-19 15:43:11 -07001051#ifdef CONFIG_NET_POLL_CONTROLLER
1052static void sbmac_netpoll(struct net_device *netdev)
1053{
1054 struct sbmac_softc *sc = netdev_priv(netdev);
1055 int irq = sc->sbm_dev->irq;
1056
1057 __raw_writeq(0, sc->sbm_imr);
1058
Yoann Padioleau0da2f0f2007-07-06 02:39:56 -07001059 sbmac_intr(irq, netdev);
Deepak Saxenad6830012007-03-19 15:43:11 -07001060
1061#ifdef CONFIG_SBMAC_COALESCE
1062 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1063 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1064 sc->sbm_imr);
1065#else
1066 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1067 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1068#endif
1069}
1070#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072/**********************************************************************
Mark Mason693aa942007-04-26 00:23:22 -07001073 * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001075 * Process "completed" receive buffers on the specified DMA channel.
Ralf Baechle74b02472005-10-19 15:40:02 +01001076 *
1077 * Input parameters:
Mark Mason693aa942007-04-26 00:23:22 -07001078 * sc - softc structure
1079 * d - DMA channel context
1080 * work_to_do - no. of packets to process before enabling interrupt
1081 * again (for NAPI)
1082 * poll - 1: using polling (for NAPI)
Ralf Baechle74b02472005-10-19 15:40:02 +01001083 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * Return value:
1085 * nothing
1086 ********************************************************************* */
1087
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001088static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1089 int work_to_do, int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001091 struct net_device *dev = sc->sbm_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int curidx;
1093 int hwidx;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001094 struct sbdmadscr *dsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct sk_buff *sb;
1096 int len;
Mark Mason693aa942007-04-26 00:23:22 -07001097 int work_done = 0;
1098 int dropped = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001099
Mark Mason693aa942007-04-26 00:23:22 -07001100 prefetch(d);
1101
1102again:
1103 /* Check if the HW dropped any frames */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001104 dev->stats.rx_fifo_errors
Mark Mason693aa942007-04-26 00:23:22 -07001105 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1106 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1107
1108 while (work_to_do-- > 0) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001109 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * figure out where we are (as an index) and where
1111 * the hardware is (also as an index)
1112 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001113 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * descriptor table was page-aligned and contiguous in
1115 * both virtual and physical memory -- you could then
1116 * just compare the low-order bits of the virtual address
1117 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1118 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001119
Mark Mason693aa942007-04-26 00:23:22 -07001120 dsc = d->sbdma_remptr;
1121 curidx = dsc - d->sbdma_dscrtable;
1122
1123 prefetch(dsc);
1124 prefetch(&d->sbdma_ctxtable[curidx]);
1125
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001126 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1127 d->sbdma_dscrtable_phys) /
1128 sizeof(*d->sbdma_dscrtable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
1131 * If they're the same, that means we've processed all
1132 * of the descriptors up to (but not including) the one that
1133 * the hardware is working on right now.
1134 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (curidx == hwidx)
Mark Mason693aa942007-04-26 00:23:22 -07001137 goto done;
Ralf Baechle74b02472005-10-19 15:40:02 +01001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 /*
1140 * Otherwise, get the packet's sk_buff ptr back
1141 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sb = d->sbdma_ctxtable[curidx];
1144 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
Ralf Baechle74b02472005-10-19 15:40:02 +01001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Check packet status. If good, process it.
1150 * If not, silently drop it and put it back on the
1151 * receive ring.
1152 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001153
Mark Mason693aa942007-04-26 00:23:22 -07001154 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Add a new buffer to replace the old one. If we fail
1158 * to allocate a buffer, we're going to drop this
1159 * packet and put it right back on the receive ring.
1160 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001161
Mark Mason693aa942007-04-26 00:23:22 -07001162 if (unlikely (sbdma_add_rcvbuffer(d,NULL) ==
1163 -ENOBUFS)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001164 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 sbdma_add_rcvbuffer(d,sb); /* re-add old buffer */
Mark Mason693aa942007-04-26 00:23:22 -07001166 /* No point in continuing at the moment */
1167 printk(KERN_ERR "dropped packet (1)\n");
1168 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1169 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else {
1171 /*
1172 * Set length into the packet
1173 */
1174 skb_put(sb,len);
Ralf Baechle74b02472005-10-19 15:40:02 +01001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /*
1177 * Buffer has been replaced on the
1178 * receive ring. Pass the buffer to
1179 * the kernel
1180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1182 /* Check hw IPv4/TCP checksum if supported */
1183 if (sc->rx_hw_checksum == ENABLE) {
1184 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1185 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1186 sb->ip_summed = CHECKSUM_UNNECESSARY;
1187 /* don't need to set sb->csum */
1188 } else {
1189 sb->ip_summed = CHECKSUM_NONE;
1190 }
1191 }
Mark Mason693aa942007-04-26 00:23:22 -07001192 prefetch(sb->data);
1193 prefetch((const void *)(((char *)sb->data)+32));
1194 if (poll)
1195 dropped = netif_receive_skb(sb);
1196 else
1197 dropped = netif_rx(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001198
Mark Mason693aa942007-04-26 00:23:22 -07001199 if (dropped == NET_RX_DROP) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001200 dev->stats.rx_dropped++;
Mark Mason693aa942007-04-26 00:23:22 -07001201 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1202 goto done;
1203 }
1204 else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001205 dev->stats.rx_bytes += len;
1206 dev->stats.rx_packets++;
Mark Mason693aa942007-04-26 00:23:22 -07001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 } else {
1210 /*
1211 * Packet was mangled somehow. Just drop it and
1212 * put it back on the receive ring.
1213 */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001214 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sbdma_add_rcvbuffer(d,sb);
1216 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001217
1218
1219 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * .. and advance to the next buffer.
1221 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Mark Mason693aa942007-04-26 00:23:22 -07001224 work_done++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Mark Mason693aa942007-04-26 00:23:22 -07001226 if (!poll) {
1227 work_to_do = 32;
1228 goto again; /* collect fifo drop statistics again */
1229 }
1230done:
1231 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/**********************************************************************
1235 * SBDMA_TX_PROCESS(sc,d)
Ralf Baechle74b02472005-10-19 15:40:02 +01001236 *
1237 * Process "completed" transmit buffers on the specified DMA channel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * This is normally called within the interrupt service routine.
1239 * Note that this isn't really ideal for priority channels, since
Ralf Baechle74b02472005-10-19 15:40:02 +01001240 * it processes all of the packets on a given channel before
1241 * returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001243 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * sc - softc structure
Mark Mason693aa942007-04-26 00:23:22 -07001245 * d - DMA channel context
1246 * poll - 1: using polling (for NAPI)
Ralf Baechle74b02472005-10-19 15:40:02 +01001247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 * Return value:
1249 * nothing
1250 ********************************************************************* */
1251
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001252static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1253 int poll)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001255 struct net_device *dev = sc->sbm_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int curidx;
1257 int hwidx;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001258 struct sbdmadscr *dsc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct sk_buff *sb;
1260 unsigned long flags;
Mark Mason693aa942007-04-26 00:23:22 -07001261 int packets_handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 spin_lock_irqsave(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001264
Mark Mason693aa942007-04-26 00:23:22 -07001265 if (d->sbdma_remptr == d->sbdma_addptr)
1266 goto end_unlock;
1267
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001268 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1269 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
Mark Mason693aa942007-04-26 00:23:22 -07001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 for (;;) {
Ralf Baechle74b02472005-10-19 15:40:02 +01001272 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * figure out where we are (as an index) and where
1274 * the hardware is (also as an index)
1275 *
Ralf Baechle74b02472005-10-19 15:40:02 +01001276 * This could be done faster if (for example) the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 * descriptor table was page-aligned and contiguous in
1278 * both virtual and physical memory -- you could then
1279 * just compare the low-order bits of the virtual address
1280 * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1281 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * If they're the same, that means we've processed all
1287 * of the descriptors up to (but not including) the one that
1288 * the hardware is working on right now.
1289 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (curidx == hwidx)
1292 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /*
1295 * Otherwise, get the packet's sk_buff ptr back
1296 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 dsc = &(d->sbdma_dscrtable[curidx]);
1299 sb = d->sbdma_ctxtable[curidx];
1300 d->sbdma_ctxtable[curidx] = NULL;
Ralf Baechle74b02472005-10-19 15:40:02 +01001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Stats
1304 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001305
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001306 dev->stats.tx_bytes += sb->len;
1307 dev->stats.tx_packets++;
Ralf Baechle74b02472005-10-19 15:40:02 +01001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * for transmits, we just free buffers.
1311 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 dev_kfree_skb_irq(sb);
Ralf Baechle74b02472005-10-19 15:40:02 +01001314
1315 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * .. and advance to the next buffer.
1317 */
1318
1319 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001320
Mark Mason693aa942007-04-26 00:23:22 -07001321 packets_handled++;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /*
1326 * Decide if we should wake up the protocol or not.
1327 * Other drivers seem to do this when we reach a low
1328 * watermark on the transmit queue.
1329 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001330
Mark Mason693aa942007-04-26 00:23:22 -07001331 if (packets_handled)
1332 netif_wake_queue(d->sbdma_eth->sbm_dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01001333
Mark Mason693aa942007-04-26 00:23:22 -07001334end_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
1339
1340
1341/**********************************************************************
1342 * SBMAC_INITCTX(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001343 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * Initialize an Ethernet context structure - this is called
1345 * once per MAC on the 1250. Memory is allocated here, so don't
1346 * call it again from inside the ioctl routines that bring the
1347 * interface up/down
Ralf Baechle74b02472005-10-19 15:40:02 +01001348 *
1349 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * s - sbmac context structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001351 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * Return value:
1353 * 0
1354 ********************************************************************* */
1355
1356static int sbmac_initctx(struct sbmac_softc *s)
1357{
Ralf Baechle74b02472005-10-19 15:40:02 +01001358
1359 /*
1360 * figure out the addresses of some ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1364 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1365 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1366 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1367 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1368 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1369 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1370 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /*
1373 * Initialize the DMA channels. Right now, only one per MAC is used
1374 * Note: Only do this _once_, as it allocates memory from the kernel!
1375 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1378 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
Ralf Baechle74b02472005-10-19 15:40:02 +01001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 /*
1381 * initial state is OFF
1382 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return 0;
1387}
1388
1389
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001390static void sbdma_uninitctx(struct sbmacdma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Mark Mason693aa942007-04-26 00:23:22 -07001392 if (d->sbdma_dscrtable_unaligned) {
1393 kfree(d->sbdma_dscrtable_unaligned);
1394 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (d->sbdma_ctxtable) {
1398 kfree(d->sbdma_ctxtable);
1399 d->sbdma_ctxtable = NULL;
1400 }
1401}
1402
1403
1404static void sbmac_uninitctx(struct sbmac_softc *sc)
1405{
1406 sbdma_uninitctx(&(sc->sbm_txdma));
1407 sbdma_uninitctx(&(sc->sbm_rxdma));
1408}
1409
1410
1411/**********************************************************************
1412 * SBMAC_CHANNEL_START(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * Start packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001415 *
1416 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001418 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * Return value:
1420 * nothing
1421 ********************************************************************* */
1422
1423static void sbmac_channel_start(struct sbmac_softc *s)
1424{
1425 uint64_t reg;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001426 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 uint64_t cfg,fifo,framecfg;
1428 int idx, th_value;
Ralf Baechle74b02472005-10-19 15:40:02 +01001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * Don't do this if running
1432 */
1433
1434 if (s->sbm_state == sbmac_state_on)
1435 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /*
1438 * Bring the controller out of reset, but leave it off.
1439 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001440
Ralf Baechle20399732005-10-19 15:39:05 +01001441 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /*
1444 * Ignore all received packets
1445 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001446
Ralf Baechle20399732005-10-19 15:39:05 +01001447 __raw_writeq(0, s->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001448
1449 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 * Calculate values for various control registers.
1451 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 cfg = M_MAC_RETRY_EN |
Ralf Baechle74b02472005-10-19 15:40:02 +01001454 M_MAC_TX_HOLD_SOP_EN |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 V_MAC_TX_PAUSE_CNT_16K |
1456 M_MAC_AP_STAT_EN |
1457 M_MAC_FAST_SYNC |
1458 M_MAC_SS_EN |
1459 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001460
1461 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1463 * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1464 * Use a larger RD_THRSH for gigabit
1465 */
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001466 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 th_value = 28;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001468 else
1469 th_value = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1472 ((s->sbm_speed == sbmac_speed_1000)
1473 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1474 V_MAC_TX_RL_THRSH(4) |
1475 V_MAC_RX_PL_THRSH(4) |
1476 V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
1477 V_MAC_RX_PL_THRSH(4) |
1478 V_MAC_RX_RL_THRSH(8) |
1479 0;
1480
1481 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1482 V_MAC_MAX_FRAMESZ_DEFAULT |
1483 V_MAC_BACKOFF_SEL(1);
1484
1485 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001486 * Clear out the hash address map
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 port = s->sbm_base + R_MAC_HASH_BASE;
1490 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001491 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 port += sizeof(uint64_t);
1493 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 /*
1496 * Clear out the exact-match table
1497 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 port = s->sbm_base + R_MAC_ADDR_BASE;
1500 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001501 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 port += sizeof(uint64_t);
1503 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /*
1506 * Clear out the DMA Channel mapping table registers
1507 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 port = s->sbm_base + R_MAC_CHUP0_BASE;
1510 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001511 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 port += sizeof(uint64_t);
1513 }
1514
1515
1516 port = s->sbm_base + R_MAC_CHLO0_BASE;
1517 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
Ralf Baechle20399732005-10-19 15:39:05 +01001518 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 port += sizeof(uint64_t);
1520 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * Program the hardware address. It goes into the hardware-address
1524 * register as well as the first filter register.
1525 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 reg = sbmac_addr2reg(s->sbm_hwaddr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 port = s->sbm_base + R_MAC_ADDR_BASE;
Ralf Baechle20399732005-10-19 15:39:05 +01001530 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1532
1533#ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
1534 /*
1535 * Pass1 SOCs do not receive packets addressed to the
1536 * destination address in the R_MAC_ETHERNET_ADDR register.
1537 * Set the value to zero.
1538 */
Ralf Baechle20399732005-10-19 15:39:05 +01001539 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540#else
Ralf Baechle20399732005-10-19 15:39:05 +01001541 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /*
1545 * Set the receive filter for no packets, and write values
1546 * to the various config registers
1547 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001548
Ralf Baechle20399732005-10-19 15:39:05 +01001549 __raw_writeq(0, s->sbm_rxfilter);
1550 __raw_writeq(0, s->sbm_imr);
1551 __raw_writeq(framecfg, s->sbm_framecfg);
1552 __raw_writeq(fifo, s->sbm_fifocfg);
1553 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 /*
1556 * Initialize DMA channels (rings should be ok now)
1557 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1560 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
Ralf Baechle74b02472005-10-19 15:40:02 +01001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Configure the speed, duplex, and flow control
1564 */
1565
1566 sbmac_set_speed(s,s->sbm_speed);
1567 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
Ralf Baechle74b02472005-10-19 15:40:02 +01001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 /*
1570 * Fill the receive ring
1571 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 sbdma_fillring(&(s->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001574
1575 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 * Turn on the rest of the bits in the enable register
Ralf Baechle74b02472005-10-19 15:40:02 +01001577 */
1578
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001579#if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1580 __raw_writeq(M_MAC_RXDMA_EN0 |
1581 M_MAC_TXDMA_EN0, s->sbm_macenable);
1582#elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
Ralf Baechle20399732005-10-19 15:39:05 +01001583 __raw_writeq(M_MAC_RXDMA_EN0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 M_MAC_TXDMA_EN0 |
1585 M_MAC_RX_ENABLE |
Ralf Baechle20399732005-10-19 15:39:05 +01001586 M_MAC_TX_ENABLE, s->sbm_macenable);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001587#else
1588#error invalid SiByte MAC configuation
1589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591#ifdef CONFIG_SBMAC_COALESCE
Ralf Baechle20399732005-10-19 15:39:05 +01001592 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1593 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594#else
Ralf Baechle20399732005-10-19 15:39:05 +01001595 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1596 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597#endif
Ralf Baechle74b02472005-10-19 15:40:02 +01001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001600 * Enable receiving unicasts and broadcasts
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001602
1603 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1604
1605 /*
1606 * we're running now.
1607 */
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 s->sbm_state = sbmac_state_on;
Ralf Baechle74b02472005-10-19 15:40:02 +01001610
1611 /*
1612 * Program multicast addresses
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 sbmac_setmulti(s);
Ralf Baechle74b02472005-10-19 15:40:02 +01001616
1617 /*
1618 * If channel was in promiscuous mode before, turn that on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (s->sbm_devflags & IFF_PROMISC) {
1622 sbmac_promiscuous_mode(s,1);
1623 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627
1628/**********************************************************************
1629 * SBMAC_CHANNEL_STOP(s)
Ralf Baechle74b02472005-10-19 15:40:02 +01001630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 * Stop packet processing on this MAC.
Ralf Baechle74b02472005-10-19 15:40:02 +01001632 *
1633 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * s - sbmac structure
Ralf Baechle74b02472005-10-19 15:40:02 +01001635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * Return value:
1637 * nothing
1638 ********************************************************************* */
1639
1640static void sbmac_channel_stop(struct sbmac_softc *s)
1641{
1642 /* don't do this if already stopped */
Ralf Baechle74b02472005-10-19 15:40:02 +01001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (s->sbm_state == sbmac_state_off)
1645 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /* don't accept any packets, disable all interrupts */
Ralf Baechle74b02472005-10-19 15:40:02 +01001648
Ralf Baechle20399732005-10-19 15:39:05 +01001649 __raw_writeq(0, s->sbm_rxfilter);
1650 __raw_writeq(0, s->sbm_imr);
Ralf Baechle74b02472005-10-19 15:40:02 +01001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* Turn off ticker */
Ralf Baechle74b02472005-10-19 15:40:02 +01001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /* XXX */
Ralf Baechle74b02472005-10-19 15:40:02 +01001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* turn off receiver and transmitter */
Ralf Baechle74b02472005-10-19 15:40:02 +01001657
Ralf Baechle20399732005-10-19 15:39:05 +01001658 __raw_writeq(0, s->sbm_macenable);
Ralf Baechle74b02472005-10-19 15:40:02 +01001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /* We're stopped now. */
Ralf Baechle74b02472005-10-19 15:40:02 +01001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 s->sbm_state = sbmac_state_off;
Ralf Baechle74b02472005-10-19 15:40:02 +01001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
1665 * Stop DMA channels (rings should be ok now)
1666 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 sbdma_channel_stop(&(s->sbm_rxdma));
1669 sbdma_channel_stop(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /* Empty the receive and transmit rings */
Ralf Baechle74b02472005-10-19 15:40:02 +01001672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 sbdma_emptyring(&(s->sbm_rxdma));
1674 sbdma_emptyring(&(s->sbm_txdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678/**********************************************************************
1679 * SBMAC_SET_CHANNEL_STATE(state)
Ralf Baechle74b02472005-10-19 15:40:02 +01001680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * Set the channel's state ON or OFF
Ralf Baechle74b02472005-10-19 15:40:02 +01001682 *
1683 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 * state - new state
Ralf Baechle74b02472005-10-19 15:40:02 +01001685 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * Return value:
1687 * old state
1688 ********************************************************************* */
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001689static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1690 enum sbmac_state state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001692 enum sbmac_state oldstate = sc->sbm_state;
Ralf Baechle74b02472005-10-19 15:40:02 +01001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
1695 * If same as previous state, return
1696 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (state == oldstate) {
1699 return oldstate;
1700 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001703 * If new state is ON, turn channel on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (state == sbmac_state_on) {
1707 sbmac_channel_start(sc);
1708 }
1709 else {
1710 sbmac_channel_stop(sc);
1711 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
1714 * Return previous state
1715 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return oldstate;
1718}
1719
1720
1721/**********************************************************************
1722 * SBMAC_PROMISCUOUS_MODE(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001723 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * Turn on or off promiscuous mode
Ralf Baechle74b02472005-10-19 15:40:02 +01001725 *
1726 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * sc - softc
1728 * onoff - 1 to turn on, 0 to turn off
Ralf Baechle74b02472005-10-19 15:40:02 +01001729 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * Return value:
1731 * nothing
1732 ********************************************************************* */
1733
1734static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1735{
1736 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (sc->sbm_state != sbmac_state_on)
1739 return;
Ralf Baechle74b02472005-10-19 15:40:02 +01001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (onoff) {
Ralf Baechle20399732005-10-19 15:39:05 +01001742 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 reg |= M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001744 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 else {
Ralf Baechle20399732005-10-19 15:39:05 +01001747 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 reg &= ~M_MAC_ALLPKT_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01001749 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751}
1752
1753/**********************************************************************
1754 * SBMAC_SETIPHDR_OFFSET(sc,onoff)
Ralf Baechle74b02472005-10-19 15:40:02 +01001755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * Set the iphdr offset as 15 assuming ethernet encapsulation
Ralf Baechle74b02472005-10-19 15:40:02 +01001757 *
1758 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01001760 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * Return value:
1762 * nothing
1763 ********************************************************************* */
1764
1765static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1766{
1767 uint64_t reg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* Hard code the off set to 15 for now */
Ralf Baechle20399732005-10-19 15:39:05 +01001770 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
Ralf Baechle20399732005-10-19 15:39:05 +01001772 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01001773
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001774 /* BCM1250 pass1 didn't have hardware checksum. Everything
1775 later does. */
1776 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 sc->rx_hw_checksum = DISABLE;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00001778 } else {
1779 sc->rx_hw_checksum = ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781}
1782
1783
1784/**********************************************************************
1785 * SBMAC_ADDR2REG(ptr)
Ralf Baechle74b02472005-10-19 15:40:02 +01001786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * Convert six bytes into the 64-bit register value that
1788 * we typically write into the SBMAC's address/mcast registers
Ralf Baechle74b02472005-10-19 15:40:02 +01001789 *
1790 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * ptr - pointer to 6 bytes
Ralf Baechle74b02472005-10-19 15:40:02 +01001792 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 * Return value:
1794 * register value
1795 ********************************************************************* */
1796
1797static uint64_t sbmac_addr2reg(unsigned char *ptr)
1798{
1799 uint64_t reg = 0;
Ralf Baechle74b02472005-10-19 15:40:02 +01001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 ptr += 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01001802
1803 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001805 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001807 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001809 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001811 reg |= (uint64_t) *(--ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 reg <<= 8;
Ralf Baechle74b02472005-10-19 15:40:02 +01001813 reg |= (uint64_t) *(--ptr);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return reg;
1816}
1817
1818
1819/**********************************************************************
1820 * SBMAC_SET_SPEED(s,speed)
Ralf Baechle74b02472005-10-19 15:40:02 +01001821 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 * Configure LAN speed for the specified MAC.
1823 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001824 *
1825 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 * s - sbmac structure
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001827 * speed - speed to set MAC to (see enum sbmac_speed)
Ralf Baechle74b02472005-10-19 15:40:02 +01001828 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * Return value:
1830 * 1 if successful
1831 * 0 indicates invalid parameters
1832 ********************************************************************* */
1833
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001834static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 uint64_t cfg;
1837 uint64_t framecfg;
1838
1839 /*
1840 * Save new current values
1841 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 s->sbm_speed = speed;
Ralf Baechle74b02472005-10-19 15:40:02 +01001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (s->sbm_state == sbmac_state_on)
1846 return 0; /* save for next restart */
1847
1848 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001849 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001851
Ralf Baechle20399732005-10-19 15:39:05 +01001852 cfg = __raw_readq(s->sbm_maccfg);
1853 framecfg = __raw_readq(s->sbm_framecfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * Mask out the stuff we want to change
1857 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1860 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1861 M_MAC_SLOT_SIZE);
Ralf Baechle74b02472005-10-19 15:40:02 +01001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /*
1864 * Now add in the new bits
1865 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 switch (speed) {
1868 case sbmac_speed_10:
1869 framecfg |= V_MAC_IFG_RX_10 |
1870 V_MAC_IFG_TX_10 |
1871 K_MAC_IFG_THRSH_10 |
1872 V_MAC_SLOT_SIZE_10;
1873 cfg |= V_MAC_SPEED_SEL_10MBPS;
1874 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 case sbmac_speed_100:
1877 framecfg |= V_MAC_IFG_RX_100 |
1878 V_MAC_IFG_TX_100 |
1879 V_MAC_IFG_THRSH_100 |
1880 V_MAC_SLOT_SIZE_100;
1881 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1882 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 case sbmac_speed_1000:
1885 framecfg |= V_MAC_IFG_RX_1000 |
1886 V_MAC_IFG_TX_1000 |
1887 V_MAC_IFG_THRSH_1000 |
1888 V_MAC_SLOT_SIZE_1000;
1889 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1890 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 default:
1893 return 0;
1894 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001897 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001899
Ralf Baechle20399732005-10-19 15:39:05 +01001900 __raw_writeq(framecfg, s->sbm_framecfg);
1901 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return 1;
1904}
1905
1906/**********************************************************************
1907 * SBMAC_SET_DUPLEX(s,duplex,fc)
Ralf Baechle74b02472005-10-19 15:40:02 +01001908 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 * Set Ethernet duplex and flow control options for this MAC
1910 * Warning: must be called when MAC is off!
Ralf Baechle74b02472005-10-19 15:40:02 +01001911 *
1912 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * s - sbmac structure
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001914 * duplex - duplex setting (see enum sbmac_duplex)
1915 * fc - flow control setting (see enum sbmac_fc)
Ralf Baechle74b02472005-10-19 15:40:02 +01001916 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 * Return value:
1918 * 1 if ok
1919 * 0 if an invalid parameter combination was specified
1920 ********************************************************************* */
1921
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01001922static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1923 enum sbmac_fc fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 uint64_t cfg;
Ralf Baechle74b02472005-10-19 15:40:02 +01001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /*
1928 * Save new current values
1929 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 s->sbm_duplex = duplex;
1932 s->sbm_fc = fc;
Ralf Baechle74b02472005-10-19 15:40:02 +01001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (s->sbm_state == sbmac_state_on)
1935 return 0; /* save for next restart */
Ralf Baechle74b02472005-10-19 15:40:02 +01001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001938 * Read current register values
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001940
Ralf Baechle20399732005-10-19 15:39:05 +01001941 cfg = __raw_readq(s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /*
1944 * Mask off the stuff we're about to change
1945 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
Ralf Baechle74b02472005-10-19 15:40:02 +01001948
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 switch (duplex) {
1951 case sbmac_duplex_half:
1952 switch (fc) {
1953 case sbmac_fc_disabled:
1954 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1955 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 case sbmac_fc_collision:
1958 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1959 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 case sbmac_fc_carrier:
1962 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1963 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 case sbmac_fc_frame: /* not valid in half duplex */
1966 default: /* invalid selection */
1967 return 0;
1968 }
1969 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 case sbmac_duplex_full:
1972 switch (fc) {
1973 case sbmac_fc_disabled:
1974 cfg |= V_MAC_FC_CMD_DISABLED;
1975 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 case sbmac_fc_frame:
1978 cfg |= V_MAC_FC_CMD_ENABLED;
1979 break;
Ralf Baechle74b02472005-10-19 15:40:02 +01001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 case sbmac_fc_collision: /* not valid in full duplex */
1982 case sbmac_fc_carrier: /* not valid in full duplex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 default:
1984 return 0;
1985 }
1986 break;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01001987 default:
1988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Ralf Baechle74b02472005-10-19 15:40:02 +01001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01001992 * Send the bits back to the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 */
Ralf Baechle74b02472005-10-19 15:40:02 +01001994
Ralf Baechle20399732005-10-19 15:39:05 +01001995 __raw_writeq(cfg, s->sbm_maccfg);
Ralf Baechle74b02472005-10-19 15:40:02 +01001996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return 1;
1998}
1999
2000
2001
2002
2003/**********************************************************************
2004 * SBMAC_INTR()
Ralf Baechle74b02472005-10-19 15:40:02 +01002005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 * Interrupt handler for MAC interrupts
Ralf Baechle74b02472005-10-19 15:40:02 +01002007 *
2008 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 * MAC structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 * Return value:
2012 * nothing
2013 ********************************************************************* */
David Howells7d12e782006-10-05 14:55:46 +01002014static irqreturn_t sbmac_intr(int irq,void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 struct net_device *dev = (struct net_device *) dev_instance;
2017 struct sbmac_softc *sc = netdev_priv(dev);
2018 uint64_t isr;
2019 int handled = 0;
2020
Mark Mason693aa942007-04-26 00:23:22 -07002021 /*
2022 * Read the ISR (this clears the bits in the real
2023 * register, except for counter addr)
2024 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002025
Mark Mason693aa942007-04-26 00:23:22 -07002026 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
Ralf Baechle74b02472005-10-19 15:40:02 +01002027
Mark Mason693aa942007-04-26 00:23:22 -07002028 if (isr == 0)
2029 return IRQ_RETVAL(0);
2030 handled = 1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002031
Mark Mason693aa942007-04-26 00:23:22 -07002032 /*
2033 * Transmits on channel 0
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002036 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
Mark Mason693aa942007-04-26 00:23:22 -07002037 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
Ralf Baechle74b02472005-10-19 15:40:02 +01002038
Mark Mason693aa942007-04-26 00:23:22 -07002039 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002040 if (netif_rx_schedule_prep(dev, &sc->napi)) {
Mark Mason693aa942007-04-26 00:23:22 -07002041 __raw_writeq(0, sc->sbm_imr);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002042 __netif_rx_schedule(dev, &sc->napi);
Mark Mason693aa942007-04-26 00:23:22 -07002043 /* Depend on the exit from poll to reenable intr */
2044 }
2045 else {
2046 /* may leave some packets behind */
2047 sbdma_rx_process(sc,&(sc->sbm_rxdma),
2048 SBMAC_MAX_RXDESCR * 2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 }
2051 return IRQ_RETVAL(handled);
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/**********************************************************************
2055 * SBMAC_START_TX(skb,dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002056 *
2057 * Start output on the specified interface. Basically, we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 * queue as many buffers as we can until the ring fills up, or
2059 * we run off the end of the queue, whichever comes first.
Ralf Baechle74b02472005-10-19 15:40:02 +01002060 *
2061 * Input parameters:
2062 *
2063 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 * Return value:
2065 * nothing
2066 ********************************************************************* */
2067static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2068{
2069 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* lock eth irq */
2072 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002075 * Put the buffer on the transmit ring. If we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * don't have room, stop the queue.
2077 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2080 /* XXX save skb that we could not send */
2081 netif_stop_queue(dev);
2082 spin_unlock_irq(&sc->sbm_lock);
2083
2084 return 1;
2085 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 dev->trans_start = jiffies;
Ralf Baechle74b02472005-10-19 15:40:02 +01002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 spin_unlock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return 0;
2092}
2093
2094/**********************************************************************
2095 * SBMAC_SETMULTI(sc)
Ralf Baechle74b02472005-10-19 15:40:02 +01002096 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 * Reprogram the multicast table into the hardware, given
2098 * the list of multicasts associated with the interface
2099 * structure.
Ralf Baechle74b02472005-10-19 15:40:02 +01002100 *
2101 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 * sc - softc
Ralf Baechle74b02472005-10-19 15:40:02 +01002103 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 * Return value:
2105 * nothing
2106 ********************************************************************* */
2107
2108static void sbmac_setmulti(struct sbmac_softc *sc)
2109{
2110 uint64_t reg;
Maciej W. Rozycki73d73962007-09-20 19:14:01 +01002111 void __iomem *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int idx;
2113 struct dev_mc_list *mclist;
2114 struct net_device *dev = sc->sbm_dev;
Ralf Baechle74b02472005-10-19 15:40:02 +01002115
2116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * Clear out entire multicast table. We do this by nuking
2118 * the entire hash table and all the direct matches except
Ralf Baechle74b02472005-10-19 15:40:02 +01002119 * the first one, which is used for our station address
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2123 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002124 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2128 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002129 __raw_writeq(0, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /*
2133 * Clear the filter to say we don't want any multicasts.
2134 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002135
Ralf Baechle20399732005-10-19 15:39:05 +01002136 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002138 __raw_writeq(reg, sc->sbm_rxfilter);
Ralf Baechle74b02472005-10-19 15:40:02 +01002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (dev->flags & IFF_ALLMULTI) {
Ralf Baechle74b02472005-10-19 15:40:02 +01002141 /*
2142 * Enable ALL multicasts. Do this by inverting the
2143 * multicast enable bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
Ralf Baechle20399732005-10-19 15:39:05 +01002145 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
Ralf Baechle20399732005-10-19 15:39:05 +01002147 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Ralf Baechle74b02472005-10-19 15:40:02 +01002151
2152 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 * Progam new multicast entries. For now, only use the
2154 * perfect filter. In the future we'll need to use the
2155 * hash filter if the perfect filter overflows
2156 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* XXX only using perfect filter for now, need to use hash
2159 * XXX if the table overflows */
Ralf Baechle74b02472005-10-19 15:40:02 +01002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 idx = 1; /* skip station address */
2162 mclist = dev->mc_list;
2163 while (mclist && (idx < MAC_ADDR_COUNT)) {
2164 reg = sbmac_addr2reg(mclist->dmi_addr);
2165 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
Ralf Baechle20399732005-10-19 15:39:05 +01002166 __raw_writeq(reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 idx++;
2168 mclist = mclist->next;
2169 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002170
2171 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * Enable the "accept multicast bits" if we programmed at least one
Ralf Baechle74b02472005-10-19 15:40:02 +01002173 * multicast.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (idx > 1) {
Ralf Baechle20399732005-10-19 15:39:05 +01002177 reg = __raw_readq(sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 reg |= M_MAC_MCAST_EN;
Ralf Baechle20399732005-10-19 15:39:05 +01002179 __raw_writeq(reg, sc->sbm_rxfilter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181}
2182
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002183#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/**********************************************************************
2185 * SBMAC_PARSE_XDIGIT(str)
Ralf Baechle74b02472005-10-19 15:40:02 +01002186 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 * Parse a hex digit, returning its value
Ralf Baechle74b02472005-10-19 15:40:02 +01002188 *
2189 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * str - character
Ralf Baechle74b02472005-10-19 15:40:02 +01002191 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 * Return value:
2193 * hex value, or -1 if invalid
2194 ********************************************************************* */
2195
2196static int sbmac_parse_xdigit(char str)
2197{
2198 int digit;
Ralf Baechle74b02472005-10-19 15:40:02 +01002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if ((str >= '0') && (str <= '9'))
2201 digit = str - '0';
2202 else if ((str >= 'a') && (str <= 'f'))
2203 digit = str - 'a' + 10;
2204 else if ((str >= 'A') && (str <= 'F'))
2205 digit = str - 'A' + 10;
2206 else
2207 return -1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return digit;
2210}
2211
2212/**********************************************************************
2213 * SBMAC_PARSE_HWADDR(str,hwaddr)
Ralf Baechle74b02472005-10-19 15:40:02 +01002214 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
2216 * Ethernet address.
Ralf Baechle74b02472005-10-19 15:40:02 +01002217 *
2218 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 * str - string
2220 * hwaddr - pointer to hardware address
Ralf Baechle74b02472005-10-19 15:40:02 +01002221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * Return value:
2223 * 0 if ok, else -1
2224 ********************************************************************* */
2225
2226static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr)
2227{
2228 int digit1,digit2;
2229 int idx = 6;
Ralf Baechle74b02472005-10-19 15:40:02 +01002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 while (*str && (idx > 0)) {
2232 digit1 = sbmac_parse_xdigit(*str);
2233 if (digit1 < 0)
2234 return -1;
2235 str++;
2236 if (!*str)
2237 return -1;
Ralf Baechle74b02472005-10-19 15:40:02 +01002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if ((*str == ':') || (*str == '-')) {
2240 digit2 = digit1;
2241 digit1 = 0;
2242 }
2243 else {
2244 digit2 = sbmac_parse_xdigit(*str);
2245 if (digit2 < 0)
2246 return -1;
2247 str++;
2248 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 *hwaddr++ = (digit1 << 4) | digit2;
2251 idx--;
Ralf Baechle74b02472005-10-19 15:40:02 +01002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (*str == '-')
2254 str++;
2255 if (*str == ':')
2256 str++;
2257 }
2258 return 0;
2259}
2260#endif
2261
2262static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
2263{
2264 if (new_mtu > ENET_PACKET_SIZE)
2265 return -EINVAL;
2266 _dev->mtu = new_mtu;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002267 pr_info("changing the mtu to %d\n", new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return 0;
2269}
2270
2271/**********************************************************************
2272 * SBMAC_INIT(dev)
Ralf Baechle74b02472005-10-19 15:40:02 +01002273 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 * Attach routine - init hardware and hook ourselves into linux
Ralf Baechle74b02472005-10-19 15:40:02 +01002275 *
2276 * Input parameters:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * dev - net_device structure
Ralf Baechle74b02472005-10-19 15:40:02 +01002278 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 * Return value:
2280 * status
2281 ********************************************************************* */
2282
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002283static int sbmac_init(struct platform_device *pldev, long long base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002285 struct net_device *dev = pldev->dev.driver_data;
2286 int idx = pldev->id;
2287 struct sbmac_softc *sc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 unsigned char *eaddr;
2289 uint64_t ea_reg;
2290 int i;
2291 int err;
Joe Perches0795af52007-10-03 17:59:30 -07002292 DECLARE_MAC_BUF(mac);
Ralf Baechle74b02472005-10-19 15:40:02 +01002293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 sc->sbm_dev = dev;
2295 sc->sbe_idx = idx;
Ralf Baechle74b02472005-10-19 15:40:02 +01002296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 eaddr = sc->sbm_hwaddr;
Ralf Baechle74b02472005-10-19 15:40:02 +01002298
2299 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 * Read the ethernet address. The firwmare left this programmed
2301 * for us in the ethernet address register for each mac.
2302 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002303
Ralf Baechle20399732005-10-19 15:39:05 +01002304 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2305 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 for (i = 0; i < 6; i++) {
2307 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2308 ea_reg >>= 8;
2309 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 for (i = 0; i < 6; i++) {
2312 dev->dev_addr[i] = eaddr[i];
2313 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002314
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /*
Ralf Baechle74b02472005-10-19 15:40:02 +01002317 * Init packet size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 sc->sbm_buffersize = ENET_PACKET_SIZE + SMP_CACHE_BYTES * 2 + ETHER_ALIGN;
2321
Ralf Baechle74b02472005-10-19 15:40:02 +01002322 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 * Initialize context (get pointers to registers and stuff), then
2324 * allocate the memory for the descriptor tables.
2325 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 sbmac_initctx(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /*
2330 * Set up Linux device callins
2331 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 spin_lock_init(&(sc->sbm_lock));
Ralf Baechle74b02472005-10-19 15:40:02 +01002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 dev->open = sbmac_open;
2336 dev->hard_start_xmit = sbmac_start_tx;
2337 dev->stop = sbmac_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 dev->set_multicast_list = sbmac_set_rx_mode;
2339 dev->do_ioctl = sbmac_mii_ioctl;
2340 dev->tx_timeout = sbmac_tx_timeout;
2341 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002342
2343 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 dev->change_mtu = sb1250_change_mtu;
Deepak Saxenad6830012007-03-19 15:43:11 -07002346#ifdef CONFIG_NET_POLL_CONTROLLER
2347 dev->poll_controller = sbmac_netpoll;
2348#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002350 dev->irq = UNIT_INT(idx);
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /* This is needed for PASS2 for Rx H/W checksum feature */
2353 sbmac_set_iphdr_offset(sc);
2354
2355 err = register_netdev(dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002356 if (err) {
2357 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2358 sbmac_string, idx);
2359 sbmac_uninitctx(sc);
2360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002363 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2364
2365 if (sc->rx_hw_checksum == ENABLE)
2366 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 /*
2369 * Display Ethernet address (this is called during the config
2370 * process so we need to finish off the config message that
2371 * was being displayed)
2372 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002373 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %s\n",
2374 dev->name, base, print_mac(mac, eaddr));
2375
2376 sc->mii_bus.name = sbmac_mdio_string;
2377 sc->mii_bus.id = idx;
2378 sc->mii_bus.priv = sc;
2379 sc->mii_bus.read = sbmac_mii_read;
2380 sc->mii_bus.write = sbmac_mii_write;
2381 sc->mii_bus.irq = sc->phy_irq;
2382 for (i = 0; i < PHY_MAX_ADDR; ++i)
2383 sc->mii_bus.irq[i] = SBMAC_PHY_INT;
2384
2385 sc->mii_bus.dev = &pldev->dev;
2386 dev_set_drvdata(&pldev->dev, &sc->mii_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
2391
2392static int sbmac_open(struct net_device *dev)
2393{
2394 struct sbmac_softc *sc = netdev_priv(dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002395 int err;
Ralf Baechle74b02472005-10-19 15:40:02 +01002396
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002397 if (debug > 1)
2398 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
Ralf Baechle74b02472005-10-19 15:40:02 +01002399
2400 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * map/route interrupt (clear status first, in case something
2402 * weird is pending; we haven't initialized the mac registers
2403 * yet)
2404 */
2405
Ralf Baechle20399732005-10-19 15:39:05 +01002406 __raw_readq(sc->sbm_isr);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002407 err = request_irq(dev->irq, &sbmac_intr, IRQF_SHARED, dev->name, dev);
2408 if (err) {
2409 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2410 dev->irq);
2411 goto out_err;
Ralf Baechle59b81822005-10-20 12:01:28 +01002412 }
2413
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002414 /*
2415 * Probe PHY address
2416 */
2417 err = mdiobus_register(&sc->mii_bus);
2418 if (err) {
2419 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2420 dev->name);
2421 goto out_unirq;
2422 }
2423
2424 sc->sbm_speed = sbmac_speed_none;
2425 sc->sbm_duplex = sbmac_duplex_none;
2426 sc->sbm_fc = sbmac_fc_none;
2427 sc->sbm_pause = -1;
2428 sc->sbm_link = 0;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002429
Ralf Baechle59b81822005-10-20 12:01:28 +01002430 /*
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002431 * Attach to the PHY
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002433 err = sbmac_mii_probe(dev);
2434 if (err)
2435 goto out_unregister;
Ralf Baechle74b02472005-10-19 15:40:02 +01002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /*
2438 * Turn on the channel
2439 */
2440
2441 sbmac_set_channel_state(sc,sbmac_state_on);
Ralf Baechle74b02472005-10-19 15:40:02 +01002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 netif_start_queue(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 sbmac_set_rx_mode(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002446
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002447 phy_start(sc->phy_dev);
2448
2449 napi_enable(&sc->napi);
Ralf Baechle74b02472005-10-19 15:40:02 +01002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return 0;
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002452
2453out_unregister:
2454 mdiobus_unregister(&sc->mii_bus);
2455
2456out_unirq:
2457 free_irq(dev->irq, dev);
2458
2459out_err:
2460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Ralf Baechle59b81822005-10-20 12:01:28 +01002463static int sbmac_mii_probe(struct net_device *dev)
2464{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct sbmac_softc *sc = netdev_priv(dev);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002466 struct phy_device *phy_dev;
2467 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002469 for (i = 0; i < PHY_MAX_ADDR; i++) {
2470 phy_dev = sc->mii_bus.phy_map[i];
2471 if (phy_dev)
2472 break;
2473 }
2474 if (!phy_dev) {
2475 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2476 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002478
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002479 phy_dev = phy_connect(dev, phy_dev->dev.bus_id, &sbmac_mii_poll, 0,
2480 PHY_INTERFACE_MODE_GMII);
2481 if (IS_ERR(phy_dev)) {
2482 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2483 return PTR_ERR(phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Ralf Baechle74b02472005-10-19 15:40:02 +01002485
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002486 /* Remove any features not supported by the controller */
2487 phy_dev->supported &= SUPPORTED_10baseT_Half |
2488 SUPPORTED_10baseT_Full |
2489 SUPPORTED_100baseT_Half |
2490 SUPPORTED_100baseT_Full |
2491 SUPPORTED_1000baseT_Half |
2492 SUPPORTED_1000baseT_Full |
2493 SUPPORTED_Autoneg |
2494 SUPPORTED_MII |
2495 SUPPORTED_Pause |
2496 SUPPORTED_Asym_Pause;
2497 phy_dev->advertising = phy_dev->supported;
Ralf Baechle74b02472005-10-19 15:40:02 +01002498
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002499 pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2500 dev->name, phy_dev->drv->name,
2501 phy_dev->dev.bus_id, phy_dev->irq);
2502
2503 sc->phy_dev = phy_dev;
2504
2505 return 0;
2506}
2507
2508
2509static void sbmac_mii_poll(struct net_device *dev)
2510{
2511 struct sbmac_softc *sc = netdev_priv(dev);
2512 struct phy_device *phy_dev = sc->phy_dev;
2513 unsigned long flags;
2514 enum sbmac_fc fc;
2515 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2516
2517 link_chg = (sc->sbm_link != phy_dev->link);
2518 speed_chg = (sc->sbm_speed != phy_dev->speed);
2519 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2520 pause_chg = (sc->sbm_pause != phy_dev->pause);
2521
2522 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2523 return; /* Hmmm... */
2524
2525 if (!phy_dev->link) {
2526 if (link_chg) {
2527 sc->sbm_link = phy_dev->link;
2528 sc->sbm_speed = sbmac_speed_none;
2529 sc->sbm_duplex = sbmac_duplex_none;
2530 sc->sbm_fc = sbmac_fc_disabled;
2531 sc->sbm_pause = -1;
2532 pr_info("%s: link unavailable\n", dev->name);
2533 }
2534 return;
2535 }
2536
2537 if (phy_dev->duplex == DUPLEX_FULL) {
2538 if (phy_dev->pause)
2539 fc = sbmac_fc_frame;
2540 else
2541 fc = sbmac_fc_disabled;
2542 } else
2543 fc = sbmac_fc_collision;
2544 fc_chg = (sc->sbm_fc != fc);
2545
2546 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2547 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2548
2549 spin_lock_irqsave(&sc->sbm_lock, flags);
2550
2551 sc->sbm_speed = phy_dev->speed;
2552 sc->sbm_duplex = phy_dev->duplex;
2553 sc->sbm_fc = fc;
2554 sc->sbm_pause = phy_dev->pause;
2555 sc->sbm_link = phy_dev->link;
2556
2557 if ((speed_chg || duplex_chg || fc_chg) &&
2558 sc->sbm_state != sbmac_state_off) {
2559 /*
2560 * something changed, restart the channel
2561 */
2562 if (debug > 1)
2563 pr_debug("%s: restarting channel "
2564 "because PHY state changed\n", dev->name);
2565 sbmac_channel_stop(sc);
2566 sbmac_channel_start(sc);
2567 }
2568
2569 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572
2573static void sbmac_tx_timeout (struct net_device *dev)
2574{
2575 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 spin_lock_irq (&sc->sbm_lock);
Ralf Baechle74b02472005-10-19 15:40:02 +01002578
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002581 dev->stats.tx_errors++;
Ralf Baechle74b02472005-10-19 15:40:02 +01002582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 spin_unlock_irq (&sc->sbm_lock);
2584
2585 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2586}
2587
2588
2589
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591static void sbmac_set_rx_mode(struct net_device *dev)
2592{
2593 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 struct sbmac_softc *sc = netdev_priv(dev);
2595
2596 spin_lock_irqsave(&sc->sbm_lock, flags);
2597 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2598 /*
2599 * Promiscuous changed.
2600 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002601
2602 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 sbmac_promiscuous_mode(sc,1);
2604 }
2605 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 sbmac_promiscuous_mode(sc,0);
2607 }
2608 }
2609 spin_unlock_irqrestore(&sc->sbm_lock, flags);
Ralf Baechle74b02472005-10-19 15:40:02 +01002610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /*
2612 * Program the multicasts. Do this every time.
2613 */
Ralf Baechle74b02472005-10-19 15:40:02 +01002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 sbmac_setmulti(sc);
Ralf Baechle74b02472005-10-19 15:40:02 +01002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2620{
2621 struct sbmac_softc *sc = netdev_priv(dev);
Ralf Baechle74b02472005-10-19 15:40:02 +01002622
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002623 if (!netif_running(dev) || !sc->phy_dev)
2624 return -EINVAL;
Ralf Baechle74b02472005-10-19 15:40:02 +01002625
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002626 return phy_mii_ioctl(sc->phy_dev, if_mii(rq), cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
2629static int sbmac_close(struct net_device *dev)
2630{
2631 struct sbmac_softc *sc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002633 napi_disable(&sc->napi);
2634
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002635 phy_stop(sc->phy_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002637 sbmac_set_channel_state(sc, sbmac_state_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 netif_stop_queue(dev);
2640
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002641 if (debug > 1)
2642 pr_debug("%s: Shutting down ethercard\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002644 phy_disconnect(sc->phy_dev);
2645 sc->phy_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002647 mdiobus_unregister(&sc->mii_bus);
2648
2649 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 sbdma_emptyring(&(sc->sbm_txdma));
2652 sbdma_emptyring(&(sc->sbm_rxdma));
Ralf Baechle74b02472005-10-19 15:40:02 +01002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return 0;
2655}
2656
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002657static int sbmac_poll(struct napi_struct *napi, int budget)
Mark Mason693aa942007-04-26 00:23:22 -07002658{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002659 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2660 struct net_device *dev = sc->sbm_dev;
Mark Mason693aa942007-04-26 00:23:22 -07002661 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002663 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
Mark Mason693aa942007-04-26 00:23:22 -07002664 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2665
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002666 if (work_done < budget) {
2667 netif_rx_complete(dev, napi);
Mark Mason693aa942007-04-26 00:23:22 -07002668
2669#ifdef CONFIG_SBMAC_COALESCE
2670 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2671 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2672 sc->sbm_imr);
2673#else
2674 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2675 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2676#endif
2677 }
2678
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002679 return work_done;
Mark Mason693aa942007-04-26 00:23:22 -07002680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002682
2683static int __init sbmac_probe(struct platform_device *pldev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002685 struct net_device *dev;
2686 struct sbmac_softc *sc;
2687 void __iomem *sbm_base;
2688 struct resource *res;
2689 u64 sbmac_orig_hwaddr;
2690 int err;
2691
2692 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2693 BUG_ON(!res);
2694 sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
2695 if (!sbm_base) {
2696 printk(KERN_ERR "%s: unable to map device registers\n",
2697 pldev->dev.bus_id);
2698 err = -ENOMEM;
2699 goto out_out;
2700 }
2701
2702 /*
2703 * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2704 * value for us by the firmware if we're going to use this MAC.
2705 * If we find a zero, skip this MAC.
2706 */
2707 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2708 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", pldev->dev.bus_id,
2709 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2710 if (sbmac_orig_hwaddr == 0) {
2711 err = 0;
2712 goto out_unmap;
2713 }
2714
2715 /*
2716 * Okay, cool. Initialize this MAC.
2717 */
2718 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2719 if (!dev) {
2720 printk(KERN_ERR "%s: unable to allocate etherdev\n",
2721 pldev->dev.bus_id);
2722 err = -ENOMEM;
2723 goto out_unmap;
2724 }
2725
2726 pldev->dev.driver_data = dev;
2727 SET_NETDEV_DEV(dev, &pldev->dev);
2728
2729 sc = netdev_priv(dev);
2730 sc->sbm_base = sbm_base;
2731
2732 err = sbmac_init(pldev, res->start);
2733 if (err)
2734 goto out_kfree;
2735
2736 return 0;
2737
2738out_kfree:
2739 free_netdev(dev);
2740 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2741
2742out_unmap:
2743 iounmap(sbm_base);
2744
2745out_out:
2746 return err;
2747}
2748
2749static int __exit sbmac_remove(struct platform_device *pldev)
2750{
2751 struct net_device *dev = pldev->dev.driver_data;
2752 struct sbmac_softc *sc = netdev_priv(dev);
2753
2754 unregister_netdev(dev);
2755 sbmac_uninitctx(sc);
2756 iounmap(sc->sbm_base);
2757 free_netdev(dev);
2758
2759 return 0;
2760}
2761
2762
2763static struct platform_device **sbmac_pldev;
2764static int sbmac_max_units;
2765
2766#if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
2767static void __init sbmac_setup_hwaddr(int idx, char *addr)
2768{
2769 void __iomem *sbm_base;
2770 unsigned long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 uint8_t eaddr[6];
2772 uint64_t val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002774 if (idx >= sbmac_max_units)
2775 return;
2776
2777 start = A_MAC_CHANNEL_BASE(idx);
2778 end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2779
2780 sbm_base = ioremap_nocache(start, end - start + 1);
2781 if (!sbm_base) {
2782 printk(KERN_ERR "%s: unable to map device registers\n",
2783 sbmac_string);
2784 return;
2785 }
2786
2787 sbmac_parse_hwaddr(addr, eaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 val = sbmac_addr2reg(eaddr);
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002789 __raw_writeq(val, sbm_base + R_MAC_ETHERNET_ADDR);
2790 val = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2791
2792 iounmap(sbm_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794#endif
2795
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002796static int __init sbmac_platform_probe_one(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002798 struct platform_device *pldev;
2799 struct {
2800 struct resource r;
2801 char name[strlen(sbmac_pretty) + 4];
2802 } *res;
2803 int err;
2804
2805 res = kzalloc(sizeof(*res), GFP_KERNEL);
2806 if (!res) {
2807 printk(KERN_ERR "%s.%d: unable to allocate memory\n",
2808 sbmac_string, idx);
2809 err = -ENOMEM;
2810 goto out_err;
2811 }
2812
2813 /*
2814 * This is the base address of the MAC.
2815 */
2816 snprintf(res->name, sizeof(res->name), "%s %d", sbmac_pretty, idx);
2817 res->r.name = res->name;
2818 res->r.flags = IORESOURCE_MEM;
2819 res->r.start = A_MAC_CHANNEL_BASE(idx);
2820 res->r.end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
2821
2822 pldev = platform_device_register_simple(sbmac_string, idx, &res->r, 1);
2823 if (IS_ERR(pldev)) {
2824 printk(KERN_ERR "%s.%d: unable to register platform device\n",
2825 sbmac_string, idx);
2826 err = PTR_ERR(pldev);
2827 goto out_kfree;
2828 }
2829
2830 if (!pldev->dev.driver) {
2831 err = 0; /* No hardware at this address. */
2832 goto out_unregister;
2833 }
2834
2835 sbmac_pldev[idx] = pldev;
2836 return 0;
2837
2838out_unregister:
2839 platform_device_unregister(pldev);
2840
2841out_kfree:
2842 kfree(res);
2843
2844out_err:
2845 return err;
2846}
2847
2848static void __init sbmac_platform_probe(void)
2849{
2850 int i;
Ralf Baechle74b02472005-10-19 15:40:02 +01002851
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002852 /* Set the number of available units based on the SOC type. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 switch (soc_type) {
2854 case K_SYS_SOC_TYPE_BCM1250:
2855 case K_SYS_SOC_TYPE_BCM1250_ALT:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002856 sbmac_max_units = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 break;
2858 case K_SYS_SOC_TYPE_BCM1120:
2859 case K_SYS_SOC_TYPE_BCM1125:
2860 case K_SYS_SOC_TYPE_BCM1125H:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002861 case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
2862 sbmac_max_units = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 break;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002864 case K_SYS_SOC_TYPE_BCM1x55:
2865 case K_SYS_SOC_TYPE_BCM1x80:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002866 sbmac_max_units = 4;
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002867 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 default:
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002869 return; /* none */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002872 /*
2873 * For bringup when not using the firmware, we can pre-fill
2874 * the MAC addresses using the environment variables
2875 * specified in this file (or maybe from the config file?)
2876 */
2877#ifdef SBMAC_ETH0_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002878 sbmac_setup_hwaddr(0, SBMAC_ETH0_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002879#endif
2880#ifdef SBMAC_ETH1_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002881 sbmac_setup_hwaddr(1, SBMAC_ETH1_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002882#endif
2883#ifdef SBMAC_ETH2_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002884 sbmac_setup_hwaddr(2, SBMAC_ETH2_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002885#endif
2886#ifdef SBMAC_ETH3_HWADDR
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002887 sbmac_setup_hwaddr(3, SBMAC_ETH3_HWADDR);
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002888#endif
2889
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002890 sbmac_pldev = kcalloc(sbmac_max_units, sizeof(*sbmac_pldev),
2891 GFP_KERNEL);
2892 if (!sbmac_pldev) {
2893 printk(KERN_ERR "%s: unable to allocate memory\n",
2894 sbmac_string);
2895 return;
2896 }
2897
Ralf Baechlef90fdc32006-02-08 23:23:26 +00002898 /*
2899 * Walk through the Ethernet controllers and find
2900 * those who have their MAC addresses set.
2901 */
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002902 for (i = 0; i < sbmac_max_units; i++)
2903 if (sbmac_platform_probe_one(i))
2904 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
2906
2907
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002908static void __exit sbmac_platform_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002910 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002912 for (i = 0; i < sbmac_max_units; i++)
2913 platform_device_unregister(sbmac_pldev[i]);
2914 kfree(sbmac_pldev);
2915}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Maciej W. Rozyckif5279ff2007-09-21 12:52:10 +01002917
2918static struct platform_driver sbmac_driver = {
2919 .probe = sbmac_probe,
2920 .remove = __exit_p(sbmac_remove),
2921 .driver = {
2922 .name = sbmac_string,
2923 },
2924};
2925
2926static int __init sbmac_init_module(void)
2927{
2928 int err;
2929
2930 err = platform_driver_register(&sbmac_driver);
2931 if (err)
2932 return err;
2933
2934 sbmac_platform_probe();
2935
2936 return err;
2937}
2938
2939static void __exit sbmac_cleanup_module(void)
2940{
2941 sbmac_platform_cleanup();
2942 platform_driver_unregister(&sbmac_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944
2945module_init(sbmac_init_module);
2946module_exit(sbmac_cleanup_module);