blob: 220f724c3377dcfb8a152174100f80e1395674d7 [file] [log] [blame]
David S. Miller8ef21752008-08-26 23:40:25 -07001/* sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller8ef21752008-08-26 23:40:25 -07003 * Copyright (C) 1997, 1998, 1999, 2003, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <linux/module.h>
7
8#include <linux/kernel.h>
9#include <linux/types.h>
10#include <linux/fcntl.h>
11#include <linux/interrupt.h>
12#include <linux/ioport.h>
13#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/string.h>
15#include <linux/delay.h>
16#include <linux/init.h>
17#include <linux/crc32.h>
18#include <linux/errno.h>
19#include <linux/ethtool.h>
Francois Romieucd2967802011-08-21 16:17:22 +020020#include <linux/mii.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/bitops.h>
David S. Miller738f2b72008-08-27 18:09:11 -070025#include <linux/dma-mapping.h>
David S. Miller8ef21752008-08-26 23:40:25 -070026#include <linux/of.h>
27#include <linux/of_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/auxio.h>
31#include <asm/byteorder.h>
32#include <asm/dma.h>
33#include <asm/idprom.h>
34#include <asm/io.h>
35#include <asm/openprom.h>
36#include <asm/oplib.h>
37#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/system.h>
39
40#include "sunbmac.h"
41
Tom 'spot' Callaway10158282005-04-24 20:35:20 -070042#define DRV_NAME "sunbmac"
David S. Miller8ef21752008-08-26 23:40:25 -070043#define DRV_VERSION "2.1"
44#define DRV_RELDATE "August 26, 2008"
45#define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
Tom 'spot' Callaway10158282005-04-24 20:35:20 -070046
Ben Collinsb48194b2006-10-17 19:11:31 -070047static char version[] =
Tom 'spot' Callaway10158282005-04-24 20:35:20 -070048 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
49
50MODULE_VERSION(DRV_VERSION);
51MODULE_AUTHOR(DRV_AUTHOR);
52MODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
53MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#undef DEBUG_PROBE
56#undef DEBUG_TX
57#undef DEBUG_IRQ
58
59#ifdef DEBUG_PROBE
60#define DP(x) printk x
61#else
62#define DP(x)
63#endif
64
65#ifdef DEBUG_TX
66#define DTX(x) printk x
67#else
68#define DTX(x)
69#endif
70
71#ifdef DEBUG_IRQ
72#define DIRQ(x) printk x
73#else
74#define DIRQ(x)
75#endif
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define DEFAULT_JAMSIZE 4 /* Toe jam */
78
79#define QEC_RESET_TRIES 200
80
81static int qec_global_reset(void __iomem *gregs)
82{
83 int tries = QEC_RESET_TRIES;
84
85 sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
86 while (--tries) {
87 if (sbus_readl(gregs + GLOB_CTRL) & GLOB_CTRL_RESET) {
88 udelay(20);
89 continue;
90 }
91 break;
92 }
93 if (tries)
94 return 0;
95 printk(KERN_ERR "BigMAC: Cannot reset the QEC.\n");
96 return -1;
97}
98
99static void qec_init(struct bigmac *bp)
100{
Grant Likely2dc11582010-08-06 09:25:50 -0600101 struct platform_device *qec_op = bp->qec_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 void __iomem *gregs = bp->gregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 u8 bsizes = bp->bigmac_bursts;
104 u32 regval;
105
106 /* 64byte bursts do not work at the moment, do
107 * not even try to enable them. -DaveM
108 */
109 if (bsizes & DMA_BURST32)
110 regval = GLOB_CTRL_B32;
111 else
112 regval = GLOB_CTRL_B16;
113 sbus_writel(regval | GLOB_CTRL_BMODE, gregs + GLOB_CTRL);
114 sbus_writel(GLOB_PSIZE_2048, gregs + GLOB_PSIZE);
115
116 /* All of memsize is given to bigmac. */
David S. Miller8ef21752008-08-26 23:40:25 -0700117 sbus_writel(resource_size(&qec_op->resource[1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 gregs + GLOB_MSIZE);
119
120 /* Half to the transmitter, half to the receiver. */
David S. Miller8ef21752008-08-26 23:40:25 -0700121 sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 gregs + GLOB_TSIZE);
David S. Miller8ef21752008-08-26 23:40:25 -0700123 sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 gregs + GLOB_RSIZE);
125}
126
127#define TX_RESET_TRIES 32
128#define RX_RESET_TRIES 32
129
130static void bigmac_tx_reset(void __iomem *bregs)
131{
132 int tries = TX_RESET_TRIES;
133
134 sbus_writel(0, bregs + BMAC_TXCFG);
135
136 /* The fifo threshold bit is read-only and does
137 * not clear. -DaveM
138 */
139 while ((sbus_readl(bregs + BMAC_TXCFG) & ~(BIGMAC_TXCFG_FIFO)) != 0 &&
140 --tries != 0)
141 udelay(20);
142
143 if (!tries) {
144 printk(KERN_ERR "BIGMAC: Transmitter will not reset.\n");
145 printk(KERN_ERR "BIGMAC: tx_cfg is %08x\n",
146 sbus_readl(bregs + BMAC_TXCFG));
147 }
148}
149
150static void bigmac_rx_reset(void __iomem *bregs)
151{
152 int tries = RX_RESET_TRIES;
153
154 sbus_writel(0, bregs + BMAC_RXCFG);
155 while (sbus_readl(bregs + BMAC_RXCFG) && --tries)
156 udelay(20);
157
158 if (!tries) {
159 printk(KERN_ERR "BIGMAC: Receiver will not reset.\n");
160 printk(KERN_ERR "BIGMAC: rx_cfg is %08x\n",
161 sbus_readl(bregs + BMAC_RXCFG));
162 }
163}
164
165/* Reset the transmitter and receiver. */
166static void bigmac_stop(struct bigmac *bp)
167{
168 bigmac_tx_reset(bp->bregs);
169 bigmac_rx_reset(bp->bregs);
170}
171
172static void bigmac_get_counters(struct bigmac *bp, void __iomem *bregs)
173{
174 struct net_device_stats *stats = &bp->enet_stats;
175
176 stats->rx_crc_errors += sbus_readl(bregs + BMAC_RCRCECTR);
177 sbus_writel(0, bregs + BMAC_RCRCECTR);
178
179 stats->rx_frame_errors += sbus_readl(bregs + BMAC_UNALECTR);
180 sbus_writel(0, bregs + BMAC_UNALECTR);
181
182 stats->rx_length_errors += sbus_readl(bregs + BMAC_GLECTR);
183 sbus_writel(0, bregs + BMAC_GLECTR);
184
185 stats->tx_aborted_errors += sbus_readl(bregs + BMAC_EXCTR);
186
187 stats->collisions +=
188 (sbus_readl(bregs + BMAC_EXCTR) +
189 sbus_readl(bregs + BMAC_LTCTR));
190 sbus_writel(0, bregs + BMAC_EXCTR);
191 sbus_writel(0, bregs + BMAC_LTCTR);
192}
193
194static void bigmac_clean_rings(struct bigmac *bp)
195{
196 int i;
197
198 for (i = 0; i < RX_RING_SIZE; i++) {
199 if (bp->rx_skbs[i] != NULL) {
200 dev_kfree_skb_any(bp->rx_skbs[i]);
201 bp->rx_skbs[i] = NULL;
202 }
203 }
204
205 for (i = 0; i < TX_RING_SIZE; i++) {
206 if (bp->tx_skbs[i] != NULL) {
207 dev_kfree_skb_any(bp->tx_skbs[i]);
208 bp->tx_skbs[i] = NULL;
209 }
210 }
211}
212
213static void bigmac_init_rings(struct bigmac *bp, int from_irq)
214{
215 struct bmac_init_block *bb = bp->bmac_block;
216 struct net_device *dev = bp->dev;
Al Viro9e249742005-10-21 03:22:29 -0400217 int i;
218 gfp_t gfp_flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (from_irq || in_interrupt())
221 gfp_flags = GFP_ATOMIC;
222
223 bp->rx_new = bp->rx_old = bp->tx_new = bp->tx_old = 0;
224
225 /* Free any skippy bufs left around in the rings. */
226 bigmac_clean_rings(bp);
227
228 /* Now get new skbufs for the receive ring. */
229 for (i = 0; i < RX_RING_SIZE; i++) {
230 struct sk_buff *skb;
231
232 skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags);
233 if (!skb)
234 continue;
235
236 bp->rx_skbs[i] = skb;
237 skb->dev = dev;
238
239 /* Because we reserve afterwards. */
240 skb_put(skb, ETH_FRAME_LEN);
241 skb_reserve(skb, 34);
242
243 bb->be_rxd[i].rx_addr =
David S. Miller8ef21752008-08-26 23:40:25 -0700244 dma_map_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700245 skb->data,
246 RX_BUF_ALLOC_SIZE - 34,
247 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 bb->be_rxd[i].rx_flags =
249 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
250 }
251
252 for (i = 0; i < TX_RING_SIZE; i++)
253 bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
254}
255
256#define MGMT_CLKON (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
257#define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
258
259static void idle_transceiver(void __iomem *tregs)
260{
261 int i = 20;
262
263 while (i--) {
264 sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
265 sbus_readl(tregs + TCVR_MPAL);
266 sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
267 sbus_readl(tregs + TCVR_MPAL);
268 }
269}
270
271static void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
272{
273 if (bp->tcvr_type == internal) {
274 bit = (bit & 1) << 3;
275 sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
276 tregs + TCVR_MPAL);
277 sbus_readl(tregs + TCVR_MPAL);
278 sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
279 tregs + TCVR_MPAL);
280 sbus_readl(tregs + TCVR_MPAL);
281 } else if (bp->tcvr_type == external) {
282 bit = (bit & 1) << 2;
283 sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
284 tregs + TCVR_MPAL);
285 sbus_readl(tregs + TCVR_MPAL);
286 sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
287 tregs + TCVR_MPAL);
288 sbus_readl(tregs + TCVR_MPAL);
289 } else {
290 printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
291 }
292}
293
294static int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
295{
296 int retval = 0;
297
298 if (bp->tcvr_type == internal) {
299 sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
300 sbus_readl(tregs + TCVR_MPAL);
301 sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
302 tregs + TCVR_MPAL);
303 sbus_readl(tregs + TCVR_MPAL);
304 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
305 } else if (bp->tcvr_type == external) {
306 sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
307 sbus_readl(tregs + TCVR_MPAL);
308 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
309 sbus_readl(tregs + TCVR_MPAL);
310 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
311 } else {
312 printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
313 }
314 return retval;
315}
316
317static int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
318{
319 int retval = 0;
320
321 if (bp->tcvr_type == internal) {
322 sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
323 sbus_readl(tregs + TCVR_MPAL);
324 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
325 sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
326 sbus_readl(tregs + TCVR_MPAL);
327 } else if (bp->tcvr_type == external) {
328 sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
329 sbus_readl(tregs + TCVR_MPAL);
330 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
331 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
332 sbus_readl(tregs + TCVR_MPAL);
333 } else {
334 printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
335 }
336 return retval;
337}
338
339static void put_tcvr_byte(struct bigmac *bp,
340 void __iomem *tregs,
341 unsigned int byte)
342{
343 int shift = 4;
344
345 do {
346 write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
347 shift -= 1;
348 } while (shift >= 0);
349}
350
351static void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
352 int reg, unsigned short val)
353{
354 int shift;
355
356 reg &= 0xff;
357 val &= 0xffff;
358 switch(bp->tcvr_type) {
359 case internal:
360 case external:
361 break;
362
363 default:
364 printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
365 return;
Joe Perchesee289b62010-05-17 22:47:34 -0700366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 idle_transceiver(tregs);
369 write_tcvr_bit(bp, tregs, 0);
370 write_tcvr_bit(bp, tregs, 1);
371 write_tcvr_bit(bp, tregs, 0);
372 write_tcvr_bit(bp, tregs, 1);
373
374 put_tcvr_byte(bp, tregs,
375 ((bp->tcvr_type == internal) ?
376 BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
377
378 put_tcvr_byte(bp, tregs, reg);
379
380 write_tcvr_bit(bp, tregs, 1);
381 write_tcvr_bit(bp, tregs, 0);
382
383 shift = 15;
384 do {
385 write_tcvr_bit(bp, tregs, (val >> shift) & 1);
386 shift -= 1;
387 } while (shift >= 0);
388}
389
390static unsigned short bigmac_tcvr_read(struct bigmac *bp,
391 void __iomem *tregs,
392 int reg)
393{
394 unsigned short retval = 0;
395
396 reg &= 0xff;
397 switch(bp->tcvr_type) {
398 case internal:
399 case external:
400 break;
401
402 default:
403 printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
404 return 0xffff;
Joe Perchesee289b62010-05-17 22:47:34 -0700405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 idle_transceiver(tregs);
408 write_tcvr_bit(bp, tregs, 0);
409 write_tcvr_bit(bp, tregs, 1);
410 write_tcvr_bit(bp, tregs, 1);
411 write_tcvr_bit(bp, tregs, 0);
412
413 put_tcvr_byte(bp, tregs,
414 ((bp->tcvr_type == internal) ?
415 BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
416
417 put_tcvr_byte(bp, tregs, reg);
418
419 if (bp->tcvr_type == external) {
420 int shift = 15;
421
422 (void) read_tcvr_bit2(bp, tregs);
423 (void) read_tcvr_bit2(bp, tregs);
424
425 do {
426 int tmp;
427
428 tmp = read_tcvr_bit2(bp, tregs);
429 retval |= ((tmp & 1) << shift);
430 shift -= 1;
431 } while (shift >= 0);
432
433 (void) read_tcvr_bit2(bp, tregs);
434 (void) read_tcvr_bit2(bp, tregs);
435 (void) read_tcvr_bit2(bp, tregs);
436 } else {
437 int shift = 15;
438
439 (void) read_tcvr_bit(bp, tregs);
440 (void) read_tcvr_bit(bp, tregs);
441
442 do {
443 int tmp;
444
445 tmp = read_tcvr_bit(bp, tregs);
446 retval |= ((tmp & 1) << shift);
447 shift -= 1;
448 } while (shift >= 0);
449
450 (void) read_tcvr_bit(bp, tregs);
451 (void) read_tcvr_bit(bp, tregs);
452 (void) read_tcvr_bit(bp, tregs);
453 }
454 return retval;
455}
456
457static void bigmac_tcvr_init(struct bigmac *bp)
458{
459 void __iomem *tregs = bp->tregs;
460 u32 mpal;
461
462 idle_transceiver(tregs);
463 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
464 tregs + TCVR_MPAL);
465 sbus_readl(tregs + TCVR_MPAL);
466
467 /* Only the bit for the present transceiver (internal or
468 * external) will stick, set them both and see what stays.
469 */
470 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
471 sbus_readl(tregs + TCVR_MPAL);
472 udelay(20);
473
474 mpal = sbus_readl(tregs + TCVR_MPAL);
475 if (mpal & MGMT_PAL_EXT_MDIO) {
476 bp->tcvr_type = external;
477 sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
478 tregs + TCVR_TPAL);
479 sbus_readl(tregs + TCVR_TPAL);
480 } else if (mpal & MGMT_PAL_INT_MDIO) {
481 bp->tcvr_type = internal;
482 sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
483 TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
484 tregs + TCVR_TPAL);
485 sbus_readl(tregs + TCVR_TPAL);
486 } else {
487 printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
488 "external MDIO available!\n");
489 printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
490 sbus_readl(tregs + TCVR_MPAL),
491 sbus_readl(tregs + TCVR_TPAL));
492 }
493}
494
David S. Miller52a34c72006-06-23 18:48:04 -0700495static int bigmac_init_hw(struct bigmac *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497static int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
498{
499 if (bp->sw_bmcr & BMCR_SPEED100) {
500 int timeout;
501
502 /* Reset the PHY. */
503 bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
Francois Romieucd2967802011-08-21 16:17:22 +0200504 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 bp->sw_bmcr = (BMCR_RESET);
Francois Romieucd2967802011-08-21 16:17:22 +0200506 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 timeout = 64;
509 while (--timeout) {
Francois Romieucd2967802011-08-21 16:17:22 +0200510 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if ((bp->sw_bmcr & BMCR_RESET) == 0)
512 break;
513 udelay(20);
514 }
515 if (timeout == 0)
516 printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
517
Francois Romieucd2967802011-08-21 16:17:22 +0200518 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Now we try 10baseT. */
521 bp->sw_bmcr &= ~(BMCR_SPEED100);
Francois Romieucd2967802011-08-21 16:17:22 +0200522 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return 0;
524 }
525
526 /* We've tried them all. */
527 return -1;
528}
529
530static void bigmac_timer(unsigned long data)
531{
532 struct bigmac *bp = (struct bigmac *) data;
533 void __iomem *tregs = bp->tregs;
534 int restart_timer = 0;
535
536 bp->timer_ticks++;
537 if (bp->timer_state == ltrywait) {
Francois Romieucd2967802011-08-21 16:17:22 +0200538 bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
539 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (bp->sw_bmsr & BMSR_LSTATUS) {
541 printk(KERN_INFO "%s: Link is now up at %s.\n",
542 bp->dev->name,
543 (bp->sw_bmcr & BMCR_SPEED100) ?
544 "100baseT" : "10baseT");
545 bp->timer_state = asleep;
546 restart_timer = 0;
547 } else {
548 if (bp->timer_ticks >= 4) {
549 int ret;
550
551 ret = try_next_permutation(bp, tregs);
552 if (ret == -1) {
553 printk(KERN_ERR "%s: Link down, cable problem?\n",
554 bp->dev->name);
David S. Miller52a34c72006-06-23 18:48:04 -0700555 ret = bigmac_init_hw(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (ret) {
557 printk(KERN_ERR "%s: Error, cannot re-init the "
558 "BigMAC.\n", bp->dev->name);
559 }
560 return;
561 }
562 bp->timer_ticks = 0;
563 restart_timer = 1;
564 } else {
565 restart_timer = 1;
566 }
567 }
568 } else {
569 /* Can't happens.... */
570 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
571 bp->dev->name);
572 restart_timer = 0;
573 bp->timer_ticks = 0;
574 bp->timer_state = asleep; /* foo on you */
575 }
576
577 if (restart_timer != 0) {
578 bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
579 add_timer(&bp->bigmac_timer);
580 }
581}
582
583/* Well, really we just force the chip into 100baseT then
584 * 10baseT, each time checking for a link status.
585 */
586static void bigmac_begin_auto_negotiation(struct bigmac *bp)
587{
588 void __iomem *tregs = bp->tregs;
589 int timeout;
590
591 /* Grab new software copies of PHY registers. */
Francois Romieucd2967802011-08-21 16:17:22 +0200592 bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
593 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* Reset the PHY. */
596 bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
Francois Romieucd2967802011-08-21 16:17:22 +0200597 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 bp->sw_bmcr = (BMCR_RESET);
Francois Romieucd2967802011-08-21 16:17:22 +0200599 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 timeout = 64;
602 while (--timeout) {
Francois Romieucd2967802011-08-21 16:17:22 +0200603 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if ((bp->sw_bmcr & BMCR_RESET) == 0)
605 break;
606 udelay(20);
607 }
608 if (timeout == 0)
609 printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
610
Francois Romieucd2967802011-08-21 16:17:22 +0200611 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* First we try 100baseT. */
614 bp->sw_bmcr |= BMCR_SPEED100;
Francois Romieucd2967802011-08-21 16:17:22 +0200615 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 bp->timer_state = ltrywait;
618 bp->timer_ticks = 0;
619 bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
620 bp->bigmac_timer.data = (unsigned long) bp;
Joe Perchesc061b182010-08-23 18:20:03 +0000621 bp->bigmac_timer.function = bigmac_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 add_timer(&bp->bigmac_timer);
623}
624
David S. Miller52a34c72006-06-23 18:48:04 -0700625static int bigmac_init_hw(struct bigmac *bp, int from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 void __iomem *gregs = bp->gregs;
628 void __iomem *cregs = bp->creg;
629 void __iomem *bregs = bp->bregs;
630 unsigned char *e = &bp->dev->dev_addr[0];
631
632 /* Latch current counters into statistics. */
633 bigmac_get_counters(bp, bregs);
634
635 /* Reset QEC. */
636 qec_global_reset(gregs);
637
638 /* Init QEC. */
639 qec_init(bp);
640
641 /* Alloc and reset the tx/rx descriptor chains. */
642 bigmac_init_rings(bp, from_irq);
643
644 /* Initialize the PHY. */
645 bigmac_tcvr_init(bp);
646
647 /* Stop transmitter and receiver. */
648 bigmac_stop(bp);
649
650 /* Set hardware ethernet address. */
651 sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
652 sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
653 sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
654
655 /* Clear the hash table until mc upload occurs. */
656 sbus_writel(0, bregs + BMAC_HTABLE3);
657 sbus_writel(0, bregs + BMAC_HTABLE2);
658 sbus_writel(0, bregs + BMAC_HTABLE1);
659 sbus_writel(0, bregs + BMAC_HTABLE0);
660
661 /* Enable Big Mac hash table filter. */
662 sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
663 bregs + BMAC_RXCFG);
664 udelay(20);
665
666 /* Ok, configure the Big Mac transmitter. */
667 sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
668
669 /* The HME docs recommend to use the 10LSB of our MAC here. */
670 sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
671 bregs + BMAC_RSEED);
672
673 /* Enable the output drivers no matter what. */
674 sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
675 bregs + BMAC_XIFCFG);
676
677 /* Tell the QEC where the ring descriptors are. */
678 sbus_writel(bp->bblock_dvma + bib_offset(be_rxd, 0),
679 cregs + CREG_RXDS);
680 sbus_writel(bp->bblock_dvma + bib_offset(be_txd, 0),
681 cregs + CREG_TXDS);
682
683 /* Setup the FIFO pointers into QEC local memory. */
684 sbus_writel(0, cregs + CREG_RXRBUFPTR);
685 sbus_writel(0, cregs + CREG_RXWBUFPTR);
686 sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
687 cregs + CREG_TXRBUFPTR);
688 sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
689 cregs + CREG_TXWBUFPTR);
690
691 /* Tell bigmac what interrupts we don't want to hear about. */
692 sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
693 bregs + BMAC_IMASK);
694
695 /* Enable the various other irq's. */
696 sbus_writel(0, cregs + CREG_RIMASK);
697 sbus_writel(0, cregs + CREG_TIMASK);
698 sbus_writel(0, cregs + CREG_QMASK);
699 sbus_writel(0, cregs + CREG_BMASK);
700
701 /* Set jam size to a reasonable default. */
702 sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
703
704 /* Clear collision counter. */
705 sbus_writel(0, cregs + CREG_CCNT);
706
707 /* Enable transmitter and receiver. */
708 sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
709 bregs + BMAC_TXCFG);
710 sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
711 bregs + BMAC_RXCFG);
712
713 /* Ok, start detecting link speed/duplex. */
714 bigmac_begin_auto_negotiation(bp);
715
716 /* Success. */
717 return 0;
718}
719
720/* Error interrupts get sent here. */
721static void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
722{
723 printk(KERN_ERR "bigmac_is_medium_rare: ");
724 if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
725 if (qec_status & GLOB_STAT_ER)
726 printk("QEC_ERROR, ");
727 if (qec_status & GLOB_STAT_BM)
728 printk("QEC_BMAC_ERROR, ");
729 }
730 if (bmac_status & CREG_STAT_ERRORS) {
731 if (bmac_status & CREG_STAT_BERROR)
732 printk("BMAC_ERROR, ");
733 if (bmac_status & CREG_STAT_TXDERROR)
734 printk("TXD_ERROR, ");
735 if (bmac_status & CREG_STAT_TXLERR)
736 printk("TX_LATE_ERROR, ");
737 if (bmac_status & CREG_STAT_TXPERR)
738 printk("TX_PARITY_ERROR, ");
739 if (bmac_status & CREG_STAT_TXSERR)
740 printk("TX_SBUS_ERROR, ");
741
742 if (bmac_status & CREG_STAT_RXDROP)
743 printk("RX_DROP_ERROR, ");
744
745 if (bmac_status & CREG_STAT_RXSMALL)
746 printk("RX_SMALL_ERROR, ");
747 if (bmac_status & CREG_STAT_RXLERR)
748 printk("RX_LATE_ERROR, ");
749 if (bmac_status & CREG_STAT_RXPERR)
750 printk("RX_PARITY_ERROR, ");
751 if (bmac_status & CREG_STAT_RXSERR)
752 printk("RX_SBUS_ERROR, ");
753 }
754
755 printk(" RESET\n");
David S. Miller52a34c72006-06-23 18:48:04 -0700756 bigmac_init_hw(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/* BigMAC transmit complete service routines. */
760static void bigmac_tx(struct bigmac *bp)
761{
762 struct be_txd *txbase = &bp->bmac_block->be_txd[0];
763 struct net_device *dev = bp->dev;
764 int elem;
765
766 spin_lock(&bp->lock);
767
768 elem = bp->tx_old;
769 DTX(("bigmac_tx: tx_old[%d] ", elem));
770 while (elem != bp->tx_new) {
771 struct sk_buff *skb;
772 struct be_txd *this = &txbase[elem];
773
774 DTX(("this(%p) [flags(%08x)addr(%08x)]",
775 this, this->tx_flags, this->tx_addr));
776
777 if (this->tx_flags & TXD_OWN)
778 break;
779 skb = bp->tx_skbs[elem];
780 bp->enet_stats.tx_packets++;
781 bp->enet_stats.tx_bytes += skb->len;
David S. Miller8ef21752008-08-26 23:40:25 -0700782 dma_unmap_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700783 this->tx_addr, skb->len,
784 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 DTX(("skb(%p) ", skb));
787 bp->tx_skbs[elem] = NULL;
788 dev_kfree_skb_irq(skb);
789
790 elem = NEXT_TX(elem);
791 }
792 DTX((" DONE, tx_old=%d\n", elem));
793 bp->tx_old = elem;
794
795 if (netif_queue_stopped(dev) &&
796 TX_BUFFS_AVAIL(bp) > 0)
797 netif_wake_queue(bp->dev);
798
799 spin_unlock(&bp->lock);
800}
801
802/* BigMAC receive complete service routines. */
803static void bigmac_rx(struct bigmac *bp)
804{
805 struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
806 struct be_rxd *this;
807 int elem = bp->rx_new, drops = 0;
808 u32 flags;
809
810 this = &rxbase[elem];
811 while (!((flags = this->rx_flags) & RXD_OWN)) {
812 struct sk_buff *skb;
813 int len = (flags & RXD_LENGTH); /* FCS not included */
814
815 /* Check for errors. */
816 if (len < ETH_ZLEN) {
817 bp->enet_stats.rx_errors++;
818 bp->enet_stats.rx_length_errors++;
819
820 drop_it:
821 /* Return it to the BigMAC. */
822 bp->enet_stats.rx_dropped++;
823 this->rx_flags =
824 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
825 goto next;
826 }
827 skb = bp->rx_skbs[elem];
828 if (len > RX_COPY_THRESHOLD) {
829 struct sk_buff *new_skb;
830
831 /* Now refill the entry, if we can. */
832 new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
833 if (new_skb == NULL) {
834 drops++;
835 goto drop_it;
836 }
David S. Miller8ef21752008-08-26 23:40:25 -0700837 dma_unmap_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700838 this->rx_addr,
839 RX_BUF_ALLOC_SIZE - 34,
840 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 bp->rx_skbs[elem] = new_skb;
842 new_skb->dev = bp->dev;
843 skb_put(new_skb, ETH_FRAME_LEN);
844 skb_reserve(new_skb, 34);
David S. Miller7a715f42008-08-27 18:37:58 -0700845 this->rx_addr =
David S. Miller8ef21752008-08-26 23:40:25 -0700846 dma_map_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700847 new_skb->data,
848 RX_BUF_ALLOC_SIZE - 34,
849 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 this->rx_flags =
851 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
852
853 /* Trim the original skb for the netif. */
854 skb_trim(skb, len);
855 } else {
856 struct sk_buff *copy_skb = dev_alloc_skb(len + 2);
857
858 if (copy_skb == NULL) {
859 drops++;
860 goto drop_it;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 skb_reserve(copy_skb, 2);
863 skb_put(copy_skb, len);
David S. Miller8ef21752008-08-26 23:40:25 -0700864 dma_sync_single_for_cpu(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700865 this->rx_addr, len,
866 DMA_FROM_DEVICE);
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700867 skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
David S. Miller8ef21752008-08-26 23:40:25 -0700868 dma_sync_single_for_device(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700869 this->rx_addr, len,
870 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* Reuse original ring buffer. */
873 this->rx_flags =
874 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
875
876 skb = copy_skb;
877 }
878
879 /* No checksums done by the BigMAC ;-( */
880 skb->protocol = eth_type_trans(skb, bp->dev);
881 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 bp->enet_stats.rx_packets++;
883 bp->enet_stats.rx_bytes += len;
884 next:
885 elem = NEXT_RX(elem);
886 this = &rxbase[elem];
887 }
888 bp->rx_new = elem;
889 if (drops)
890 printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
891}
892
David Howells7d12e782006-10-05 14:55:46 +0100893static irqreturn_t bigmac_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct bigmac *bp = (struct bigmac *) dev_id;
896 u32 qec_status, bmac_status;
897
898 DIRQ(("bigmac_interrupt: "));
899
900 /* Latch status registers now. */
901 bmac_status = sbus_readl(bp->creg + CREG_STAT);
902 qec_status = sbus_readl(bp->gregs + GLOB_STAT);
903
904 DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
905 if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
906 (bmac_status & CREG_STAT_ERRORS))
907 bigmac_is_medium_rare(bp, qec_status, bmac_status);
908
909 if (bmac_status & CREG_STAT_TXIRQ)
910 bigmac_tx(bp);
911
912 if (bmac_status & CREG_STAT_RXIRQ)
913 bigmac_rx(bp);
914
915 return IRQ_HANDLED;
916}
917
918static int bigmac_open(struct net_device *dev)
919{
Wang Chen8f15ea42008-11-12 23:38:36 -0800920 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int ret;
922
Joe Perchesa0607fd2009-11-18 23:29:17 -0800923 ret = request_irq(dev->irq, bigmac_interrupt, IRQF_SHARED, dev->name, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (ret) {
925 printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
926 return ret;
927 }
928 init_timer(&bp->bigmac_timer);
David S. Miller52a34c72006-06-23 18:48:04 -0700929 ret = bigmac_init_hw(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (ret)
931 free_irq(dev->irq, bp);
932 return ret;
933}
934
935static int bigmac_close(struct net_device *dev)
936{
Wang Chen8f15ea42008-11-12 23:38:36 -0800937 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 del_timer(&bp->bigmac_timer);
940 bp->timer_state = asleep;
941 bp->timer_ticks = 0;
942
943 bigmac_stop(bp);
944 bigmac_clean_rings(bp);
945 free_irq(dev->irq, bp);
946 return 0;
947}
948
949static void bigmac_tx_timeout(struct net_device *dev)
950{
Wang Chen8f15ea42008-11-12 23:38:36 -0800951 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
David S. Miller52a34c72006-06-23 18:48:04 -0700953 bigmac_init_hw(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 netif_wake_queue(dev);
955}
956
957/* Put a packet on the wire. */
958static int bigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
959{
Wang Chen8f15ea42008-11-12 23:38:36 -0800960 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int len, entry;
962 u32 mapping;
963
964 len = skb->len;
David S. Miller8ef21752008-08-26 23:40:25 -0700965 mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
David S. Miller738f2b72008-08-27 18:09:11 -0700966 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* Avoid a race... */
969 spin_lock_irq(&bp->lock);
970 entry = bp->tx_new;
971 DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
972 bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
973 bp->tx_skbs[entry] = skb;
974 bp->bmac_block->be_txd[entry].tx_addr = mapping;
975 bp->bmac_block->be_txd[entry].tx_flags =
976 (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
977 bp->tx_new = NEXT_TX(entry);
978 if (TX_BUFFS_AVAIL(bp) <= 0)
979 netif_stop_queue(dev);
980 spin_unlock_irq(&bp->lock);
981
982 /* Get it going. */
983 sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
984
985
Patrick McHardy6ed10652009-06-23 06:03:08 +0000986 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static struct net_device_stats *bigmac_get_stats(struct net_device *dev)
990{
Wang Chen8f15ea42008-11-12 23:38:36 -0800991 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 bigmac_get_counters(bp, bp->bregs);
994 return &bp->enet_stats;
995}
996
997static void bigmac_set_multicast(struct net_device *dev)
998{
Wang Chen8f15ea42008-11-12 23:38:36 -0800999 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 void __iomem *bregs = bp->bregs;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001001 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int i;
1003 u32 tmp, crc;
1004
1005 /* Disable the receiver. The bit self-clears when
1006 * the operation is complete.
1007 */
1008 tmp = sbus_readl(bregs + BMAC_RXCFG);
1009 tmp &= ~(BIGMAC_RXCFG_ENABLE);
1010 sbus_writel(tmp, bregs + BMAC_RXCFG);
1011 while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
1012 udelay(20);
1013
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001014 if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 sbus_writel(0xffff, bregs + BMAC_HTABLE0);
1016 sbus_writel(0xffff, bregs + BMAC_HTABLE1);
1017 sbus_writel(0xffff, bregs + BMAC_HTABLE2);
1018 sbus_writel(0xffff, bregs + BMAC_HTABLE3);
1019 } else if (dev->flags & IFF_PROMISC) {
1020 tmp = sbus_readl(bregs + BMAC_RXCFG);
1021 tmp |= BIGMAC_RXCFG_PMISC;
1022 sbus_writel(tmp, bregs + BMAC_RXCFG);
1023 } else {
1024 u16 hash_table[4];
1025
1026 for (i = 0; i < 4; i++)
1027 hash_table[i] = 0;
1028
Jiri Pirko22bedad32010-04-01 21:22:57 +00001029 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +00001030 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 crc >>= 26;
1032 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1033 }
1034 sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
1035 sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
1036 sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
1037 sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
1038 }
1039
1040 /* Re-enable the receiver. */
1041 tmp = sbus_readl(bregs + BMAC_RXCFG);
1042 tmp |= BIGMAC_RXCFG_ENABLE;
1043 sbus_writel(tmp, bregs + BMAC_RXCFG);
1044}
1045
1046/* Ethtool support... */
1047static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 strcpy(info->driver, "sunbmac");
1050 strcpy(info->version, "2.0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053static u32 bigmac_get_link(struct net_device *dev)
1054{
Wang Chen8f15ea42008-11-12 23:38:36 -08001055 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 spin_lock_irq(&bp->lock);
Francois Romieucd2967802011-08-21 16:17:22 +02001058 bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, MII_BMSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_unlock_irq(&bp->lock);
1060
1061 return (bp->sw_bmsr & BMSR_LSTATUS);
1062}
1063
Jeff Garzik7282d492006-09-13 14:30:00 -04001064static const struct ethtool_ops bigmac_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 .get_drvinfo = bigmac_get_drvinfo,
1066 .get_link = bigmac_get_link,
1067};
1068
David S. Miller2199b872009-03-23 13:33:21 -07001069static const struct net_device_ops bigmac_ops = {
1070 .ndo_open = bigmac_open,
1071 .ndo_stop = bigmac_close,
1072 .ndo_start_xmit = bigmac_start_xmit,
1073 .ndo_get_stats = bigmac_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001074 .ndo_set_rx_mode = bigmac_set_multicast,
David S. Miller2199b872009-03-23 13:33:21 -07001075 .ndo_tx_timeout = bigmac_tx_timeout,
David S. Millerdac46962009-03-23 14:29:24 -07001076 .ndo_change_mtu = eth_change_mtu,
1077 .ndo_set_mac_address = eth_mac_addr,
1078 .ndo_validate_addr = eth_validate_addr,
David S. Miller2199b872009-03-23 13:33:21 -07001079};
1080
Grant Likely2dc11582010-08-06 09:25:50 -06001081static int __devinit bigmac_ether_init(struct platform_device *op,
1082 struct platform_device *qec_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 static int version_printed;
David S. Miller8ef21752008-08-26 23:40:25 -07001085 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 u8 bsizes, bsizes_more;
David S. Miller8ef21752008-08-26 23:40:25 -07001087 struct bigmac *bp;
1088 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 /* Get a new device struct for this interface. */
1091 dev = alloc_etherdev(sizeof(struct bigmac));
1092 if (!dev)
1093 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 if (version_printed++ == 0)
1096 printk(KERN_INFO "%s", version);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 for (i = 0; i < 6; i++)
1099 dev->dev_addr[i] = idprom->id_ethaddr[i];
1100
1101 /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
David S. Miller8ef21752008-08-26 23:40:25 -07001102 bp = netdev_priv(dev);
1103 bp->qec_op = qec_op;
1104 bp->bigmac_op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
David S. Miller8ef21752008-08-26 23:40:25 -07001106 SET_NETDEV_DEV(dev, &op->dev);
David S. Miller52a34c72006-06-23 18:48:04 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 spin_lock_init(&bp->lock);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /* Map in QEC global control registers. */
David S. Miller8ef21752008-08-26 23:40:25 -07001111 bp->gregs = of_ioremap(&qec_op->resource[0], 0,
1112 GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!bp->gregs) {
1114 printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
1115 goto fail_and_cleanup;
1116 }
1117
1118 /* Make sure QEC is in BigMAC mode. */
1119 if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
1120 printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
1121 goto fail_and_cleanup;
1122 }
1123
1124 /* Reset the QEC. */
1125 if (qec_global_reset(bp->gregs))
1126 goto fail_and_cleanup;
1127
1128 /* Get supported SBUS burst sizes. */
Grant Likely61c7a082010-04-13 16:12:29 -07001129 bsizes = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
1130 bsizes_more = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 bsizes &= 0xff;
1133 if (bsizes_more != 0xff)
1134 bsizes &= bsizes_more;
1135 if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
1136 (bsizes & DMA_BURST32) == 0)
1137 bsizes = (DMA_BURST32 - 1);
1138 bp->bigmac_bursts = bsizes;
1139
1140 /* Perform QEC initialization. */
1141 qec_init(bp);
1142
1143 /* Map in the BigMAC channel registers. */
David S. Miller8ef21752008-08-26 23:40:25 -07001144 bp->creg = of_ioremap(&op->resource[0], 0,
1145 CREG_REG_SIZE, "BigMAC QEC Channel Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!bp->creg) {
1147 printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
1148 goto fail_and_cleanup;
1149 }
1150
1151 /* Map in the BigMAC control registers. */
David S. Miller8ef21752008-08-26 23:40:25 -07001152 bp->bregs = of_ioremap(&op->resource[1], 0,
1153 BMAC_REG_SIZE, "BigMAC Primary Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!bp->bregs) {
1155 printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
1156 goto fail_and_cleanup;
1157 }
1158
1159 /* Map in the BigMAC transceiver registers, this is how you poke at
1160 * the BigMAC's PHY.
1161 */
David S. Miller8ef21752008-08-26 23:40:25 -07001162 bp->tregs = of_ioremap(&op->resource[2], 0,
1163 TCVR_REG_SIZE, "BigMAC Transceiver Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (!bp->tregs) {
1165 printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
1166 goto fail_and_cleanup;
1167 }
1168
1169 /* Stop the BigMAC. */
1170 bigmac_stop(bp);
1171
1172 /* Allocate transmit/receive descriptor DVMA block. */
David S. Miller8ef21752008-08-26 23:40:25 -07001173 bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -07001174 PAGE_SIZE,
1175 &bp->bblock_dvma, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (bp->bmac_block == NULL || bp->bblock_dvma == 0) {
1177 printk(KERN_ERR "BIGMAC: Cannot allocate consistent DMA.\n");
1178 goto fail_and_cleanup;
1179 }
1180
1181 /* Get the board revision of this BigMAC. */
Grant Likely61c7a082010-04-13 16:12:29 -07001182 bp->board_rev = of_getintprop_default(bp->bigmac_op->dev.of_node,
David S. Miller8ef21752008-08-26 23:40:25 -07001183 "board-version", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* Init auto-negotiation timer state. */
1186 init_timer(&bp->bigmac_timer);
1187 bp->timer_state = asleep;
1188 bp->timer_ticks = 0;
1189
1190 /* Backlink to generic net device struct. */
1191 bp->dev = dev;
1192
1193 /* Set links to our BigMAC open and close routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 dev->ethtool_ops = &bigmac_ethtool_ops;
David S. Miller2199b872009-03-23 13:33:21 -07001195 dev->netdev_ops = &bigmac_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 dev->watchdog_timeo = 5*HZ;
1197
1198 /* Finish net device registration. */
Grant Likely1636f8a2010-06-18 11:09:58 -06001199 dev->irq = bp->bigmac_op->archdata.irqs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 dev->dma = 0;
1201
1202 if (register_netdev(dev)) {
1203 printk(KERN_ERR "BIGMAC: Cannot register device.\n");
1204 goto fail_and_cleanup;
1205 }
1206
David S. Miller8ef21752008-08-26 23:40:25 -07001207 dev_set_drvdata(&bp->bigmac_op->dev, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Johannes Berge1749612008-10-27 15:59:26 -07001209 printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
1210 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 return 0;
1213
1214fail_and_cleanup:
1215 /* Something went wrong, undo whatever we did so far. */
1216 /* Free register mappings if any. */
1217 if (bp->gregs)
David S. Miller8ef21752008-08-26 23:40:25 -07001218 of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (bp->creg)
David S. Miller8ef21752008-08-26 23:40:25 -07001220 of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (bp->bregs)
David S. Miller8ef21752008-08-26 23:40:25 -07001222 of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (bp->tregs)
David S. Miller8ef21752008-08-26 23:40:25 -07001224 of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (bp->bmac_block)
David S. Miller8ef21752008-08-26 23:40:25 -07001227 dma_free_coherent(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -07001228 PAGE_SIZE,
1229 bp->bmac_block,
1230 bp->bblock_dvma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Wang Chenb74ca3a2008-12-08 01:14:16 -08001232 /* This also frees the co-located private data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 free_netdev(dev);
1234 return -ENODEV;
1235}
1236
David S. Miller8ef21752008-08-26 23:40:25 -07001237/* QEC can be the parent of either QuadEthernet or a BigMAC. We want
1238 * the latter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
Grant Likely74888762011-02-22 21:05:51 -07001240static int __devinit bigmac_sbus_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
David S. Miller8ef21752008-08-26 23:40:25 -07001242 struct device *parent = op->dev.parent;
Grant Likely2dc11582010-08-06 09:25:50 -06001243 struct platform_device *qec_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Grant Likely2dc11582010-08-06 09:25:50 -06001245 qec_op = to_platform_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
David S. Miller8ef21752008-08-26 23:40:25 -07001247 return bigmac_ether_init(op, qec_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Grant Likely2dc11582010-08-06 09:25:50 -06001250static int __devexit bigmac_sbus_remove(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
David S. Miller8ef21752008-08-26 23:40:25 -07001252 struct bigmac *bp = dev_get_drvdata(&op->dev);
1253 struct device *parent = op->dev.parent;
David S. Miller52a34c72006-06-23 18:48:04 -07001254 struct net_device *net_dev = bp->dev;
Grant Likely2dc11582010-08-06 09:25:50 -06001255 struct platform_device *qec_op;
David S. Miller8ef21752008-08-26 23:40:25 -07001256
Grant Likely2dc11582010-08-06 09:25:50 -06001257 qec_op = to_platform_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Herbert Xu7afb9dc2008-10-05 09:20:28 -07001259 unregister_netdev(net_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
David S. Miller8ef21752008-08-26 23:40:25 -07001261 of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
1262 of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
1263 of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
1264 of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
1265 dma_free_coherent(&op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -07001266 PAGE_SIZE,
1267 bp->bmac_block,
1268 bp->bblock_dvma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
David S. Miller52a34c72006-06-23 18:48:04 -07001270 free_netdev(net_dev);
1271
David S. Miller8ef21752008-08-26 23:40:25 -07001272 dev_set_drvdata(&op->dev, NULL);
David S. Miller52a34c72006-06-23 18:48:04 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return 0;
1275}
1276
David S. Millerfd098312008-08-31 01:23:17 -07001277static const struct of_device_id bigmac_sbus_match[] = {
David S. Miller52a34c72006-06-23 18:48:04 -07001278 {
David S. Miller52a34c72006-06-23 18:48:04 -07001279 .name = "be",
1280 },
1281 {},
1282};
1283
1284MODULE_DEVICE_TABLE(of, bigmac_sbus_match);
1285
Grant Likely74888762011-02-22 21:05:51 -07001286static struct platform_driver bigmac_sbus_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001287 .driver = {
1288 .name = "sunbmac",
1289 .owner = THIS_MODULE,
1290 .of_match_table = bigmac_sbus_match,
1291 },
David S. Miller52a34c72006-06-23 18:48:04 -07001292 .probe = bigmac_sbus_probe,
1293 .remove = __devexit_p(bigmac_sbus_remove),
1294};
1295
Axel Lindb62f682011-11-27 16:44:17 +00001296module_platform_driver(bigmac_sbus_driver);