blob: 85d3b6bec16047253ae8c123025a1243ae1fd4e7 [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 Romieucd296782011-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
39#include "sunbmac.h"
40
Tom 'spot' Callaway10158282005-04-24 20:35:20 -070041#define DRV_NAME "sunbmac"
David S. Miller8ef21752008-08-26 23:40:25 -070042#define DRV_VERSION "2.1"
43#define DRV_RELDATE "August 26, 2008"
44#define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
Tom 'spot' Callaway10158282005-04-24 20:35:20 -070045
Ben Collinsb48194b2006-10-17 19:11:31 -070046static char version[] =
Tom 'spot' Callaway10158282005-04-24 20:35:20 -070047 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
48
49MODULE_VERSION(DRV_VERSION);
50MODULE_AUTHOR(DRV_AUTHOR);
51MODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
52MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#undef DEBUG_PROBE
55#undef DEBUG_TX
56#undef DEBUG_IRQ
57
58#ifdef DEBUG_PROBE
59#define DP(x) printk x
60#else
61#define DP(x)
62#endif
63
64#ifdef DEBUG_TX
65#define DTX(x) printk x
66#else
67#define DTX(x)
68#endif
69
70#ifdef DEBUG_IRQ
71#define DIRQ(x) printk x
72#else
73#define DIRQ(x)
74#endif
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define DEFAULT_JAMSIZE 4 /* Toe jam */
77
78#define QEC_RESET_TRIES 200
79
80static int qec_global_reset(void __iomem *gregs)
81{
82 int tries = QEC_RESET_TRIES;
83
84 sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
85 while (--tries) {
86 if (sbus_readl(gregs + GLOB_CTRL) & GLOB_CTRL_RESET) {
87 udelay(20);
88 continue;
89 }
90 break;
91 }
92 if (tries)
93 return 0;
94 printk(KERN_ERR "BigMAC: Cannot reset the QEC.\n");
95 return -1;
96}
97
98static void qec_init(struct bigmac *bp)
99{
Grant Likely2dc11582010-08-06 09:25:50 -0600100 struct platform_device *qec_op = bp->qec_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 void __iomem *gregs = bp->gregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 u8 bsizes = bp->bigmac_bursts;
103 u32 regval;
104
105 /* 64byte bursts do not work at the moment, do
106 * not even try to enable them. -DaveM
107 */
108 if (bsizes & DMA_BURST32)
109 regval = GLOB_CTRL_B32;
110 else
111 regval = GLOB_CTRL_B16;
112 sbus_writel(regval | GLOB_CTRL_BMODE, gregs + GLOB_CTRL);
113 sbus_writel(GLOB_PSIZE_2048, gregs + GLOB_PSIZE);
114
115 /* All of memsize is given to bigmac. */
David S. Miller8ef21752008-08-26 23:40:25 -0700116 sbus_writel(resource_size(&qec_op->resource[1]),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 gregs + GLOB_MSIZE);
118
119 /* Half to the transmitter, half to the receiver. */
David S. Miller8ef21752008-08-26 23:40:25 -0700120 sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 gregs + GLOB_TSIZE);
David S. Miller8ef21752008-08-26 23:40:25 -0700122 sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 gregs + GLOB_RSIZE);
124}
125
126#define TX_RESET_TRIES 32
127#define RX_RESET_TRIES 32
128
129static void bigmac_tx_reset(void __iomem *bregs)
130{
131 int tries = TX_RESET_TRIES;
132
133 sbus_writel(0, bregs + BMAC_TXCFG);
134
135 /* The fifo threshold bit is read-only and does
136 * not clear. -DaveM
137 */
138 while ((sbus_readl(bregs + BMAC_TXCFG) & ~(BIGMAC_TXCFG_FIFO)) != 0 &&
139 --tries != 0)
140 udelay(20);
141
142 if (!tries) {
143 printk(KERN_ERR "BIGMAC: Transmitter will not reset.\n");
144 printk(KERN_ERR "BIGMAC: tx_cfg is %08x\n",
145 sbus_readl(bregs + BMAC_TXCFG));
146 }
147}
148
149static void bigmac_rx_reset(void __iomem *bregs)
150{
151 int tries = RX_RESET_TRIES;
152
153 sbus_writel(0, bregs + BMAC_RXCFG);
154 while (sbus_readl(bregs + BMAC_RXCFG) && --tries)
155 udelay(20);
156
157 if (!tries) {
158 printk(KERN_ERR "BIGMAC: Receiver will not reset.\n");
159 printk(KERN_ERR "BIGMAC: rx_cfg is %08x\n",
160 sbus_readl(bregs + BMAC_RXCFG));
161 }
162}
163
164/* Reset the transmitter and receiver. */
165static void bigmac_stop(struct bigmac *bp)
166{
167 bigmac_tx_reset(bp->bregs);
168 bigmac_rx_reset(bp->bregs);
169}
170
171static void bigmac_get_counters(struct bigmac *bp, void __iomem *bregs)
172{
173 struct net_device_stats *stats = &bp->enet_stats;
174
175 stats->rx_crc_errors += sbus_readl(bregs + BMAC_RCRCECTR);
176 sbus_writel(0, bregs + BMAC_RCRCECTR);
177
178 stats->rx_frame_errors += sbus_readl(bregs + BMAC_UNALECTR);
179 sbus_writel(0, bregs + BMAC_UNALECTR);
180
181 stats->rx_length_errors += sbus_readl(bregs + BMAC_GLECTR);
182 sbus_writel(0, bregs + BMAC_GLECTR);
183
184 stats->tx_aborted_errors += sbus_readl(bregs + BMAC_EXCTR);
185
186 stats->collisions +=
187 (sbus_readl(bregs + BMAC_EXCTR) +
188 sbus_readl(bregs + BMAC_LTCTR));
189 sbus_writel(0, bregs + BMAC_EXCTR);
190 sbus_writel(0, bregs + BMAC_LTCTR);
191}
192
193static void bigmac_clean_rings(struct bigmac *bp)
194{
195 int i;
196
197 for (i = 0; i < RX_RING_SIZE; i++) {
198 if (bp->rx_skbs[i] != NULL) {
199 dev_kfree_skb_any(bp->rx_skbs[i]);
200 bp->rx_skbs[i] = NULL;
201 }
202 }
203
204 for (i = 0; i < TX_RING_SIZE; i++) {
205 if (bp->tx_skbs[i] != NULL) {
206 dev_kfree_skb_any(bp->tx_skbs[i]);
207 bp->tx_skbs[i] = NULL;
208 }
209 }
210}
211
212static void bigmac_init_rings(struct bigmac *bp, int from_irq)
213{
214 struct bmac_init_block *bb = bp->bmac_block;
Al Viro9e249742005-10-21 03:22:29 -0400215 int i;
216 gfp_t gfp_flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (from_irq || in_interrupt())
219 gfp_flags = GFP_ATOMIC;
220
221 bp->rx_new = bp->rx_old = bp->tx_new = bp->tx_old = 0;
222
223 /* Free any skippy bufs left around in the rings. */
224 bigmac_clean_rings(bp);
225
226 /* Now get new skbufs for the receive ring. */
227 for (i = 0; i < RX_RING_SIZE; i++) {
228 struct sk_buff *skb;
229
230 skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags);
231 if (!skb)
232 continue;
233
234 bp->rx_skbs[i] = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* Because we reserve afterwards. */
237 skb_put(skb, ETH_FRAME_LEN);
238 skb_reserve(skb, 34);
239
240 bb->be_rxd[i].rx_addr =
David S. Miller8ef21752008-08-26 23:40:25 -0700241 dma_map_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700242 skb->data,
243 RX_BUF_ALLOC_SIZE - 34,
244 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 bb->be_rxd[i].rx_flags =
246 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
247 }
248
249 for (i = 0; i < TX_RING_SIZE; i++)
250 bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
251}
252
253#define MGMT_CLKON (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
254#define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
255
256static void idle_transceiver(void __iomem *tregs)
257{
258 int i = 20;
259
260 while (i--) {
261 sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
262 sbus_readl(tregs + TCVR_MPAL);
263 sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
264 sbus_readl(tregs + TCVR_MPAL);
265 }
266}
267
268static void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
269{
270 if (bp->tcvr_type == internal) {
271 bit = (bit & 1) << 3;
272 sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
273 tregs + TCVR_MPAL);
274 sbus_readl(tregs + TCVR_MPAL);
275 sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
276 tregs + TCVR_MPAL);
277 sbus_readl(tregs + TCVR_MPAL);
278 } else if (bp->tcvr_type == external) {
279 bit = (bit & 1) << 2;
280 sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
281 tregs + TCVR_MPAL);
282 sbus_readl(tregs + TCVR_MPAL);
283 sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
284 tregs + TCVR_MPAL);
285 sbus_readl(tregs + TCVR_MPAL);
286 } else {
287 printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
288 }
289}
290
291static int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
292{
293 int retval = 0;
294
295 if (bp->tcvr_type == internal) {
296 sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
297 sbus_readl(tregs + TCVR_MPAL);
298 sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
299 tregs + TCVR_MPAL);
300 sbus_readl(tregs + TCVR_MPAL);
301 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
302 } else if (bp->tcvr_type == external) {
303 sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
304 sbus_readl(tregs + TCVR_MPAL);
305 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
306 sbus_readl(tregs + TCVR_MPAL);
307 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
308 } else {
309 printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
310 }
311 return retval;
312}
313
314static int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
315{
316 int retval = 0;
317
318 if (bp->tcvr_type == internal) {
319 sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
320 sbus_readl(tregs + TCVR_MPAL);
321 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
322 sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
323 sbus_readl(tregs + TCVR_MPAL);
324 } else if (bp->tcvr_type == external) {
325 sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
326 sbus_readl(tregs + TCVR_MPAL);
327 retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
328 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
329 sbus_readl(tregs + TCVR_MPAL);
330 } else {
331 printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
332 }
333 return retval;
334}
335
336static void put_tcvr_byte(struct bigmac *bp,
337 void __iomem *tregs,
338 unsigned int byte)
339{
340 int shift = 4;
341
342 do {
343 write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
344 shift -= 1;
345 } while (shift >= 0);
346}
347
348static void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
349 int reg, unsigned short val)
350{
351 int shift;
352
353 reg &= 0xff;
354 val &= 0xffff;
355 switch(bp->tcvr_type) {
356 case internal:
357 case external:
358 break;
359
360 default:
361 printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
362 return;
Joe Perchesee289b62010-05-17 22:47:34 -0700363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 idle_transceiver(tregs);
366 write_tcvr_bit(bp, tregs, 0);
367 write_tcvr_bit(bp, tregs, 1);
368 write_tcvr_bit(bp, tregs, 0);
369 write_tcvr_bit(bp, tregs, 1);
370
371 put_tcvr_byte(bp, tregs,
372 ((bp->tcvr_type == internal) ?
373 BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
374
375 put_tcvr_byte(bp, tregs, reg);
376
377 write_tcvr_bit(bp, tregs, 1);
378 write_tcvr_bit(bp, tregs, 0);
379
380 shift = 15;
381 do {
382 write_tcvr_bit(bp, tregs, (val >> shift) & 1);
383 shift -= 1;
384 } while (shift >= 0);
385}
386
387static unsigned short bigmac_tcvr_read(struct bigmac *bp,
388 void __iomem *tregs,
389 int reg)
390{
391 unsigned short retval = 0;
392
393 reg &= 0xff;
394 switch(bp->tcvr_type) {
395 case internal:
396 case external:
397 break;
398
399 default:
400 printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
401 return 0xffff;
Joe Perchesee289b62010-05-17 22:47:34 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 idle_transceiver(tregs);
405 write_tcvr_bit(bp, tregs, 0);
406 write_tcvr_bit(bp, tregs, 1);
407 write_tcvr_bit(bp, tregs, 1);
408 write_tcvr_bit(bp, tregs, 0);
409
410 put_tcvr_byte(bp, tregs,
411 ((bp->tcvr_type == internal) ?
412 BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
413
414 put_tcvr_byte(bp, tregs, reg);
415
416 if (bp->tcvr_type == external) {
417 int shift = 15;
418
419 (void) read_tcvr_bit2(bp, tregs);
420 (void) read_tcvr_bit2(bp, tregs);
421
422 do {
423 int tmp;
424
425 tmp = read_tcvr_bit2(bp, tregs);
426 retval |= ((tmp & 1) << shift);
427 shift -= 1;
428 } while (shift >= 0);
429
430 (void) read_tcvr_bit2(bp, tregs);
431 (void) read_tcvr_bit2(bp, tregs);
432 (void) read_tcvr_bit2(bp, tregs);
433 } else {
434 int shift = 15;
435
436 (void) read_tcvr_bit(bp, tregs);
437 (void) read_tcvr_bit(bp, tregs);
438
439 do {
440 int tmp;
441
442 tmp = read_tcvr_bit(bp, tregs);
443 retval |= ((tmp & 1) << shift);
444 shift -= 1;
445 } while (shift >= 0);
446
447 (void) read_tcvr_bit(bp, tregs);
448 (void) read_tcvr_bit(bp, tregs);
449 (void) read_tcvr_bit(bp, tregs);
450 }
451 return retval;
452}
453
454static void bigmac_tcvr_init(struct bigmac *bp)
455{
456 void __iomem *tregs = bp->tregs;
457 u32 mpal;
458
459 idle_transceiver(tregs);
460 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
461 tregs + TCVR_MPAL);
462 sbus_readl(tregs + TCVR_MPAL);
463
464 /* Only the bit for the present transceiver (internal or
465 * external) will stick, set them both and see what stays.
466 */
467 sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
468 sbus_readl(tregs + TCVR_MPAL);
469 udelay(20);
470
471 mpal = sbus_readl(tregs + TCVR_MPAL);
472 if (mpal & MGMT_PAL_EXT_MDIO) {
473 bp->tcvr_type = external;
474 sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
475 tregs + TCVR_TPAL);
476 sbus_readl(tregs + TCVR_TPAL);
477 } else if (mpal & MGMT_PAL_INT_MDIO) {
478 bp->tcvr_type = internal;
479 sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
480 TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
481 tregs + TCVR_TPAL);
482 sbus_readl(tregs + TCVR_TPAL);
483 } else {
484 printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
485 "external MDIO available!\n");
486 printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
487 sbus_readl(tregs + TCVR_MPAL),
488 sbus_readl(tregs + TCVR_TPAL));
489 }
490}
491
David S. Miller52a34c72006-06-23 18:48:04 -0700492static int bigmac_init_hw(struct bigmac *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494static int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
495{
496 if (bp->sw_bmcr & BMCR_SPEED100) {
497 int timeout;
498
499 /* Reset the PHY. */
500 bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
Francois Romieucd296782011-08-21 16:17:22 +0200501 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 bp->sw_bmcr = (BMCR_RESET);
Francois Romieucd296782011-08-21 16:17:22 +0200503 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 timeout = 64;
506 while (--timeout) {
Francois Romieucd296782011-08-21 16:17:22 +0200507 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if ((bp->sw_bmcr & BMCR_RESET) == 0)
509 break;
510 udelay(20);
511 }
512 if (timeout == 0)
513 printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
514
Francois Romieucd296782011-08-21 16:17:22 +0200515 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* Now we try 10baseT. */
518 bp->sw_bmcr &= ~(BMCR_SPEED100);
Francois Romieucd296782011-08-21 16:17:22 +0200519 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
521 }
522
523 /* We've tried them all. */
524 return -1;
525}
526
527static void bigmac_timer(unsigned long data)
528{
529 struct bigmac *bp = (struct bigmac *) data;
530 void __iomem *tregs = bp->tregs;
531 int restart_timer = 0;
532
533 bp->timer_ticks++;
534 if (bp->timer_state == ltrywait) {
Francois Romieucd296782011-08-21 16:17:22 +0200535 bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
536 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (bp->sw_bmsr & BMSR_LSTATUS) {
538 printk(KERN_INFO "%s: Link is now up at %s.\n",
539 bp->dev->name,
540 (bp->sw_bmcr & BMCR_SPEED100) ?
541 "100baseT" : "10baseT");
542 bp->timer_state = asleep;
543 restart_timer = 0;
544 } else {
545 if (bp->timer_ticks >= 4) {
546 int ret;
547
548 ret = try_next_permutation(bp, tregs);
549 if (ret == -1) {
550 printk(KERN_ERR "%s: Link down, cable problem?\n",
551 bp->dev->name);
David S. Miller52a34c72006-06-23 18:48:04 -0700552 ret = bigmac_init_hw(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (ret) {
554 printk(KERN_ERR "%s: Error, cannot re-init the "
555 "BigMAC.\n", bp->dev->name);
556 }
557 return;
558 }
559 bp->timer_ticks = 0;
560 restart_timer = 1;
561 } else {
562 restart_timer = 1;
563 }
564 }
565 } else {
566 /* Can't happens.... */
567 printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
568 bp->dev->name);
569 restart_timer = 0;
570 bp->timer_ticks = 0;
571 bp->timer_state = asleep; /* foo on you */
572 }
573
574 if (restart_timer != 0) {
575 bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
576 add_timer(&bp->bigmac_timer);
577 }
578}
579
580/* Well, really we just force the chip into 100baseT then
581 * 10baseT, each time checking for a link status.
582 */
583static void bigmac_begin_auto_negotiation(struct bigmac *bp)
584{
585 void __iomem *tregs = bp->tregs;
586 int timeout;
587
588 /* Grab new software copies of PHY registers. */
Francois Romieucd296782011-08-21 16:17:22 +0200589 bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
590 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* Reset the PHY. */
593 bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
Francois Romieucd296782011-08-21 16:17:22 +0200594 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 bp->sw_bmcr = (BMCR_RESET);
Francois Romieucd296782011-08-21 16:17:22 +0200596 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 timeout = 64;
599 while (--timeout) {
Francois Romieucd296782011-08-21 16:17:22 +0200600 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if ((bp->sw_bmcr & BMCR_RESET) == 0)
602 break;
603 udelay(20);
604 }
605 if (timeout == 0)
606 printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
607
Francois Romieucd296782011-08-21 16:17:22 +0200608 bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* First we try 100baseT. */
611 bp->sw_bmcr |= BMCR_SPEED100;
Francois Romieucd296782011-08-21 16:17:22 +0200612 bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 bp->timer_state = ltrywait;
615 bp->timer_ticks = 0;
616 bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
617 bp->bigmac_timer.data = (unsigned long) bp;
Joe Perchesc061b182010-08-23 18:20:03 +0000618 bp->bigmac_timer.function = bigmac_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 add_timer(&bp->bigmac_timer);
620}
621
David S. Miller52a34c72006-06-23 18:48:04 -0700622static int bigmac_init_hw(struct bigmac *bp, int from_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 void __iomem *gregs = bp->gregs;
625 void __iomem *cregs = bp->creg;
626 void __iomem *bregs = bp->bregs;
627 unsigned char *e = &bp->dev->dev_addr[0];
628
629 /* Latch current counters into statistics. */
630 bigmac_get_counters(bp, bregs);
631
632 /* Reset QEC. */
633 qec_global_reset(gregs);
634
635 /* Init QEC. */
636 qec_init(bp);
637
638 /* Alloc and reset the tx/rx descriptor chains. */
639 bigmac_init_rings(bp, from_irq);
640
641 /* Initialize the PHY. */
642 bigmac_tcvr_init(bp);
643
644 /* Stop transmitter and receiver. */
645 bigmac_stop(bp);
646
647 /* Set hardware ethernet address. */
648 sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
649 sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
650 sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
651
652 /* Clear the hash table until mc upload occurs. */
653 sbus_writel(0, bregs + BMAC_HTABLE3);
654 sbus_writel(0, bregs + BMAC_HTABLE2);
655 sbus_writel(0, bregs + BMAC_HTABLE1);
656 sbus_writel(0, bregs + BMAC_HTABLE0);
657
658 /* Enable Big Mac hash table filter. */
659 sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
660 bregs + BMAC_RXCFG);
661 udelay(20);
662
663 /* Ok, configure the Big Mac transmitter. */
664 sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
665
666 /* The HME docs recommend to use the 10LSB of our MAC here. */
667 sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
668 bregs + BMAC_RSEED);
669
670 /* Enable the output drivers no matter what. */
671 sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
672 bregs + BMAC_XIFCFG);
673
674 /* Tell the QEC where the ring descriptors are. */
675 sbus_writel(bp->bblock_dvma + bib_offset(be_rxd, 0),
676 cregs + CREG_RXDS);
677 sbus_writel(bp->bblock_dvma + bib_offset(be_txd, 0),
678 cregs + CREG_TXDS);
679
680 /* Setup the FIFO pointers into QEC local memory. */
681 sbus_writel(0, cregs + CREG_RXRBUFPTR);
682 sbus_writel(0, cregs + CREG_RXWBUFPTR);
683 sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
684 cregs + CREG_TXRBUFPTR);
685 sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
686 cregs + CREG_TXWBUFPTR);
687
688 /* Tell bigmac what interrupts we don't want to hear about. */
689 sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
690 bregs + BMAC_IMASK);
691
692 /* Enable the various other irq's. */
693 sbus_writel(0, cregs + CREG_RIMASK);
694 sbus_writel(0, cregs + CREG_TIMASK);
695 sbus_writel(0, cregs + CREG_QMASK);
696 sbus_writel(0, cregs + CREG_BMASK);
697
698 /* Set jam size to a reasonable default. */
699 sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
700
701 /* Clear collision counter. */
702 sbus_writel(0, cregs + CREG_CCNT);
703
704 /* Enable transmitter and receiver. */
705 sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
706 bregs + BMAC_TXCFG);
707 sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
708 bregs + BMAC_RXCFG);
709
710 /* Ok, start detecting link speed/duplex. */
711 bigmac_begin_auto_negotiation(bp);
712
713 /* Success. */
714 return 0;
715}
716
717/* Error interrupts get sent here. */
718static void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
719{
720 printk(KERN_ERR "bigmac_is_medium_rare: ");
721 if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
722 if (qec_status & GLOB_STAT_ER)
723 printk("QEC_ERROR, ");
724 if (qec_status & GLOB_STAT_BM)
725 printk("QEC_BMAC_ERROR, ");
726 }
727 if (bmac_status & CREG_STAT_ERRORS) {
728 if (bmac_status & CREG_STAT_BERROR)
729 printk("BMAC_ERROR, ");
730 if (bmac_status & CREG_STAT_TXDERROR)
731 printk("TXD_ERROR, ");
732 if (bmac_status & CREG_STAT_TXLERR)
733 printk("TX_LATE_ERROR, ");
734 if (bmac_status & CREG_STAT_TXPERR)
735 printk("TX_PARITY_ERROR, ");
736 if (bmac_status & CREG_STAT_TXSERR)
737 printk("TX_SBUS_ERROR, ");
738
739 if (bmac_status & CREG_STAT_RXDROP)
740 printk("RX_DROP_ERROR, ");
741
742 if (bmac_status & CREG_STAT_RXSMALL)
743 printk("RX_SMALL_ERROR, ");
744 if (bmac_status & CREG_STAT_RXLERR)
745 printk("RX_LATE_ERROR, ");
746 if (bmac_status & CREG_STAT_RXPERR)
747 printk("RX_PARITY_ERROR, ");
748 if (bmac_status & CREG_STAT_RXSERR)
749 printk("RX_SBUS_ERROR, ");
750 }
751
752 printk(" RESET\n");
David S. Miller52a34c72006-06-23 18:48:04 -0700753 bigmac_init_hw(bp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/* BigMAC transmit complete service routines. */
757static void bigmac_tx(struct bigmac *bp)
758{
759 struct be_txd *txbase = &bp->bmac_block->be_txd[0];
760 struct net_device *dev = bp->dev;
761 int elem;
762
763 spin_lock(&bp->lock);
764
765 elem = bp->tx_old;
766 DTX(("bigmac_tx: tx_old[%d] ", elem));
767 while (elem != bp->tx_new) {
768 struct sk_buff *skb;
769 struct be_txd *this = &txbase[elem];
770
771 DTX(("this(%p) [flags(%08x)addr(%08x)]",
772 this, this->tx_flags, this->tx_addr));
773
774 if (this->tx_flags & TXD_OWN)
775 break;
776 skb = bp->tx_skbs[elem];
777 bp->enet_stats.tx_packets++;
778 bp->enet_stats.tx_bytes += skb->len;
David S. Miller8ef21752008-08-26 23:40:25 -0700779 dma_unmap_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700780 this->tx_addr, skb->len,
781 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 DTX(("skb(%p) ", skb));
784 bp->tx_skbs[elem] = NULL;
785 dev_kfree_skb_irq(skb);
786
787 elem = NEXT_TX(elem);
788 }
789 DTX((" DONE, tx_old=%d\n", elem));
790 bp->tx_old = elem;
791
792 if (netif_queue_stopped(dev) &&
793 TX_BUFFS_AVAIL(bp) > 0)
794 netif_wake_queue(bp->dev);
795
796 spin_unlock(&bp->lock);
797}
798
799/* BigMAC receive complete service routines. */
800static void bigmac_rx(struct bigmac *bp)
801{
802 struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
803 struct be_rxd *this;
804 int elem = bp->rx_new, drops = 0;
805 u32 flags;
806
807 this = &rxbase[elem];
808 while (!((flags = this->rx_flags) & RXD_OWN)) {
809 struct sk_buff *skb;
810 int len = (flags & RXD_LENGTH); /* FCS not included */
811
812 /* Check for errors. */
813 if (len < ETH_ZLEN) {
814 bp->enet_stats.rx_errors++;
815 bp->enet_stats.rx_length_errors++;
816
817 drop_it:
818 /* Return it to the BigMAC. */
819 bp->enet_stats.rx_dropped++;
820 this->rx_flags =
821 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
822 goto next;
823 }
824 skb = bp->rx_skbs[elem];
825 if (len > RX_COPY_THRESHOLD) {
826 struct sk_buff *new_skb;
827
828 /* Now refill the entry, if we can. */
829 new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
830 if (new_skb == NULL) {
831 drops++;
832 goto drop_it;
833 }
David S. Miller8ef21752008-08-26 23:40:25 -0700834 dma_unmap_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700835 this->rx_addr,
836 RX_BUF_ALLOC_SIZE - 34,
837 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 bp->rx_skbs[elem] = new_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 skb_put(new_skb, ETH_FRAME_LEN);
840 skb_reserve(new_skb, 34);
David S. Miller7a715f42008-08-27 18:37:58 -0700841 this->rx_addr =
David S. Miller8ef21752008-08-26 23:40:25 -0700842 dma_map_single(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700843 new_skb->data,
844 RX_BUF_ALLOC_SIZE - 34,
845 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 this->rx_flags =
847 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
848
849 /* Trim the original skb for the netif. */
850 skb_trim(skb, len);
851 } else {
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000852 struct sk_buff *copy_skb = netdev_alloc_skb(bp->dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (copy_skb == NULL) {
855 drops++;
856 goto drop_it;
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 skb_reserve(copy_skb, 2);
859 skb_put(copy_skb, len);
David S. Miller8ef21752008-08-26 23:40:25 -0700860 dma_sync_single_for_cpu(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700861 this->rx_addr, len,
862 DMA_FROM_DEVICE);
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700863 skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
David S. Miller8ef21752008-08-26 23:40:25 -0700864 dma_sync_single_for_device(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700865 this->rx_addr, len,
866 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /* Reuse original ring buffer. */
869 this->rx_flags =
870 (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
871
872 skb = copy_skb;
873 }
874
875 /* No checksums done by the BigMAC ;-( */
876 skb->protocol = eth_type_trans(skb, bp->dev);
877 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 bp->enet_stats.rx_packets++;
879 bp->enet_stats.rx_bytes += len;
880 next:
881 elem = NEXT_RX(elem);
882 this = &rxbase[elem];
883 }
884 bp->rx_new = elem;
885 if (drops)
886 printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
887}
888
David Howells7d12e782006-10-05 14:55:46 +0100889static irqreturn_t bigmac_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 struct bigmac *bp = (struct bigmac *) dev_id;
892 u32 qec_status, bmac_status;
893
894 DIRQ(("bigmac_interrupt: "));
895
896 /* Latch status registers now. */
897 bmac_status = sbus_readl(bp->creg + CREG_STAT);
898 qec_status = sbus_readl(bp->gregs + GLOB_STAT);
899
900 DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
901 if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
902 (bmac_status & CREG_STAT_ERRORS))
903 bigmac_is_medium_rare(bp, qec_status, bmac_status);
904
905 if (bmac_status & CREG_STAT_TXIRQ)
906 bigmac_tx(bp);
907
908 if (bmac_status & CREG_STAT_RXIRQ)
909 bigmac_rx(bp);
910
911 return IRQ_HANDLED;
912}
913
914static int bigmac_open(struct net_device *dev)
915{
Wang Chen8f15ea42008-11-12 23:38:36 -0800916 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int ret;
918
Joe Perchesa0607fd2009-11-18 23:29:17 -0800919 ret = request_irq(dev->irq, bigmac_interrupt, IRQF_SHARED, dev->name, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (ret) {
921 printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
922 return ret;
923 }
924 init_timer(&bp->bigmac_timer);
David S. Miller52a34c72006-06-23 18:48:04 -0700925 ret = bigmac_init_hw(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (ret)
927 free_irq(dev->irq, bp);
928 return ret;
929}
930
931static int bigmac_close(struct net_device *dev)
932{
Wang Chen8f15ea42008-11-12 23:38:36 -0800933 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 del_timer(&bp->bigmac_timer);
936 bp->timer_state = asleep;
937 bp->timer_ticks = 0;
938
939 bigmac_stop(bp);
940 bigmac_clean_rings(bp);
941 free_irq(dev->irq, bp);
942 return 0;
943}
944
945static void bigmac_tx_timeout(struct net_device *dev)
946{
Wang Chen8f15ea42008-11-12 23:38:36 -0800947 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
David S. Miller52a34c72006-06-23 18:48:04 -0700949 bigmac_init_hw(bp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 netif_wake_queue(dev);
951}
952
953/* Put a packet on the wire. */
954static int bigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
955{
Wang Chen8f15ea42008-11-12 23:38:36 -0800956 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int len, entry;
958 u32 mapping;
959
960 len = skb->len;
David S. Miller8ef21752008-08-26 23:40:25 -0700961 mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
David S. Miller738f2b72008-08-27 18:09:11 -0700962 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* Avoid a race... */
965 spin_lock_irq(&bp->lock);
966 entry = bp->tx_new;
967 DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
968 bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
969 bp->tx_skbs[entry] = skb;
970 bp->bmac_block->be_txd[entry].tx_addr = mapping;
971 bp->bmac_block->be_txd[entry].tx_flags =
972 (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
973 bp->tx_new = NEXT_TX(entry);
974 if (TX_BUFFS_AVAIL(bp) <= 0)
975 netif_stop_queue(dev);
976 spin_unlock_irq(&bp->lock);
977
978 /* Get it going. */
979 sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
980
981
Patrick McHardy6ed10652009-06-23 06:03:08 +0000982 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static struct net_device_stats *bigmac_get_stats(struct net_device *dev)
986{
Wang Chen8f15ea42008-11-12 23:38:36 -0800987 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 bigmac_get_counters(bp, bp->bregs);
990 return &bp->enet_stats;
991}
992
993static void bigmac_set_multicast(struct net_device *dev)
994{
Wang Chen8f15ea42008-11-12 23:38:36 -0800995 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 void __iomem *bregs = bp->bregs;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000997 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 u32 tmp, crc;
999
1000 /* Disable the receiver. The bit self-clears when
1001 * the operation is complete.
1002 */
1003 tmp = sbus_readl(bregs + BMAC_RXCFG);
1004 tmp &= ~(BIGMAC_RXCFG_ENABLE);
1005 sbus_writel(tmp, bregs + BMAC_RXCFG);
1006 while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
1007 udelay(20);
1008
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001009 if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sbus_writel(0xffff, bregs + BMAC_HTABLE0);
1011 sbus_writel(0xffff, bregs + BMAC_HTABLE1);
1012 sbus_writel(0xffff, bregs + BMAC_HTABLE2);
1013 sbus_writel(0xffff, bregs + BMAC_HTABLE3);
1014 } else if (dev->flags & IFF_PROMISC) {
1015 tmp = sbus_readl(bregs + BMAC_RXCFG);
1016 tmp |= BIGMAC_RXCFG_PMISC;
1017 sbus_writel(tmp, bregs + BMAC_RXCFG);
1018 } else {
1019 u16 hash_table[4];
1020
1021 for (i = 0; i < 4; i++)
1022 hash_table[i] = 0;
1023
Jiri Pirko22bedad32010-04-01 21:22:57 +00001024 netdev_for_each_mc_addr(ha, dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +00001025 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 crc >>= 26;
1027 hash_table[crc >> 4] |= 1 << (crc & 0xf);
1028 }
1029 sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
1030 sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
1031 sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
1032 sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
1033 }
1034
1035 /* Re-enable the receiver. */
1036 tmp = sbus_readl(bregs + BMAC_RXCFG);
1037 tmp |= BIGMAC_RXCFG_ENABLE;
1038 sbus_writel(tmp, bregs + BMAC_RXCFG);
1039}
1040
1041/* Ethtool support... */
1042static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1043{
Jiri Pirko7826d432013-01-06 00:44:26 +00001044 strlcpy(info->driver, "sunbmac", sizeof(info->driver));
1045 strlcpy(info->version, "2.0", sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048static u32 bigmac_get_link(struct net_device *dev)
1049{
Wang Chen8f15ea42008-11-12 23:38:36 -08001050 struct bigmac *bp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 spin_lock_irq(&bp->lock);
Francois Romieucd296782011-08-21 16:17:22 +02001053 bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, MII_BMSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 spin_unlock_irq(&bp->lock);
1055
1056 return (bp->sw_bmsr & BMSR_LSTATUS);
1057}
1058
Jeff Garzik7282d492006-09-13 14:30:00 -04001059static const struct ethtool_ops bigmac_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 .get_drvinfo = bigmac_get_drvinfo,
1061 .get_link = bigmac_get_link,
1062};
1063
David S. Miller2199b872009-03-23 13:33:21 -07001064static const struct net_device_ops bigmac_ops = {
1065 .ndo_open = bigmac_open,
1066 .ndo_stop = bigmac_close,
1067 .ndo_start_xmit = bigmac_start_xmit,
1068 .ndo_get_stats = bigmac_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001069 .ndo_set_rx_mode = bigmac_set_multicast,
David S. Miller2199b872009-03-23 13:33:21 -07001070 .ndo_tx_timeout = bigmac_tx_timeout,
David S. Millerdac46962009-03-23 14:29:24 -07001071 .ndo_change_mtu = eth_change_mtu,
1072 .ndo_set_mac_address = eth_mac_addr,
1073 .ndo_validate_addr = eth_validate_addr,
David S. Miller2199b872009-03-23 13:33:21 -07001074};
1075
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001076static int bigmac_ether_init(struct platform_device *op,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001077 struct platform_device *qec_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 static int version_printed;
David S. Miller8ef21752008-08-26 23:40:25 -07001080 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 u8 bsizes, bsizes_more;
David S. Miller8ef21752008-08-26 23:40:25 -07001082 struct bigmac *bp;
1083 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Get a new device struct for this interface. */
1086 dev = alloc_etherdev(sizeof(struct bigmac));
1087 if (!dev)
1088 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 if (version_printed++ == 0)
1091 printk(KERN_INFO "%s", version);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 for (i = 0; i < 6; i++)
1094 dev->dev_addr[i] = idprom->id_ethaddr[i];
1095
1096 /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
David S. Miller8ef21752008-08-26 23:40:25 -07001097 bp = netdev_priv(dev);
1098 bp->qec_op = qec_op;
1099 bp->bigmac_op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
David S. Miller8ef21752008-08-26 23:40:25 -07001101 SET_NETDEV_DEV(dev, &op->dev);
David S. Miller52a34c72006-06-23 18:48:04 -07001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 spin_lock_init(&bp->lock);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 /* Map in QEC global control registers. */
David S. Miller8ef21752008-08-26 23:40:25 -07001106 bp->gregs = of_ioremap(&qec_op->resource[0], 0,
1107 GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!bp->gregs) {
1109 printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
1110 goto fail_and_cleanup;
1111 }
1112
1113 /* Make sure QEC is in BigMAC mode. */
1114 if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
1115 printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
1116 goto fail_and_cleanup;
1117 }
1118
1119 /* Reset the QEC. */
1120 if (qec_global_reset(bp->gregs))
1121 goto fail_and_cleanup;
1122
1123 /* Get supported SBUS burst sizes. */
Grant Likely61c7a082010-04-13 16:12:29 -07001124 bsizes = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
1125 bsizes_more = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 bsizes &= 0xff;
1128 if (bsizes_more != 0xff)
1129 bsizes &= bsizes_more;
1130 if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
1131 (bsizes & DMA_BURST32) == 0)
1132 bsizes = (DMA_BURST32 - 1);
1133 bp->bigmac_bursts = bsizes;
1134
1135 /* Perform QEC initialization. */
1136 qec_init(bp);
1137
1138 /* Map in the BigMAC channel registers. */
David S. Miller8ef21752008-08-26 23:40:25 -07001139 bp->creg = of_ioremap(&op->resource[0], 0,
1140 CREG_REG_SIZE, "BigMAC QEC Channel Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!bp->creg) {
1142 printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
1143 goto fail_and_cleanup;
1144 }
1145
1146 /* Map in the BigMAC control registers. */
David S. Miller8ef21752008-08-26 23:40:25 -07001147 bp->bregs = of_ioremap(&op->resource[1], 0,
1148 BMAC_REG_SIZE, "BigMAC Primary Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (!bp->bregs) {
1150 printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
1151 goto fail_and_cleanup;
1152 }
1153
1154 /* Map in the BigMAC transceiver registers, this is how you poke at
1155 * the BigMAC's PHY.
1156 */
David S. Miller8ef21752008-08-26 23:40:25 -07001157 bp->tregs = of_ioremap(&op->resource[2], 0,
1158 TCVR_REG_SIZE, "BigMAC Transceiver Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!bp->tregs) {
1160 printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
1161 goto fail_and_cleanup;
1162 }
1163
1164 /* Stop the BigMAC. */
1165 bigmac_stop(bp);
1166
1167 /* Allocate transmit/receive descriptor DVMA block. */
David S. Miller8ef21752008-08-26 23:40:25 -07001168 bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -07001169 PAGE_SIZE,
1170 &bp->bblock_dvma, GFP_ATOMIC);
Joe Perchesd0320f72013-03-14 13:07:21 +00001171 if (bp->bmac_block == NULL || bp->bblock_dvma == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 goto fail_and_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /* Get the board revision of this BigMAC. */
Grant Likely61c7a082010-04-13 16:12:29 -07001175 bp->board_rev = of_getintprop_default(bp->bigmac_op->dev.of_node,
David S. Miller8ef21752008-08-26 23:40:25 -07001176 "board-version", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* Init auto-negotiation timer state. */
1179 init_timer(&bp->bigmac_timer);
1180 bp->timer_state = asleep;
1181 bp->timer_ticks = 0;
1182
1183 /* Backlink to generic net device struct. */
1184 bp->dev = dev;
1185
1186 /* Set links to our BigMAC open and close routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dev->ethtool_ops = &bigmac_ethtool_ops;
David S. Miller2199b872009-03-23 13:33:21 -07001188 dev->netdev_ops = &bigmac_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 dev->watchdog_timeo = 5*HZ;
1190
1191 /* Finish net device registration. */
Grant Likely1636f8a2010-06-18 11:09:58 -06001192 dev->irq = bp->bigmac_op->archdata.irqs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 dev->dma = 0;
1194
1195 if (register_netdev(dev)) {
1196 printk(KERN_ERR "BIGMAC: Cannot register device.\n");
1197 goto fail_and_cleanup;
1198 }
1199
David S. Miller8ef21752008-08-26 23:40:25 -07001200 dev_set_drvdata(&bp->bigmac_op->dev, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Johannes Berge1749612008-10-27 15:59:26 -07001202 printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
1203 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 return 0;
1206
1207fail_and_cleanup:
1208 /* Something went wrong, undo whatever we did so far. */
1209 /* Free register mappings if any. */
1210 if (bp->gregs)
David S. Miller8ef21752008-08-26 23:40:25 -07001211 of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (bp->creg)
David S. Miller8ef21752008-08-26 23:40:25 -07001213 of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (bp->bregs)
David S. Miller8ef21752008-08-26 23:40:25 -07001215 of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (bp->tregs)
David S. Miller8ef21752008-08-26 23:40:25 -07001217 of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 if (bp->bmac_block)
David S. Miller8ef21752008-08-26 23:40:25 -07001220 dma_free_coherent(&bp->bigmac_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -07001221 PAGE_SIZE,
1222 bp->bmac_block,
1223 bp->bblock_dvma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Wang Chenb74ca3a2008-12-08 01:14:16 -08001225 /* This also frees the co-located private data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 free_netdev(dev);
1227 return -ENODEV;
1228}
1229
David S. Miller8ef21752008-08-26 23:40:25 -07001230/* QEC can be the parent of either QuadEthernet or a BigMAC. We want
1231 * the latter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001233static int bigmac_sbus_probe(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
David S. Miller8ef21752008-08-26 23:40:25 -07001235 struct device *parent = op->dev.parent;
Grant Likely2dc11582010-08-06 09:25:50 -06001236 struct platform_device *qec_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Grant Likely2dc11582010-08-06 09:25:50 -06001238 qec_op = to_platform_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
David S. Miller8ef21752008-08-26 23:40:25 -07001240 return bigmac_ether_init(op, qec_op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001243static int bigmac_sbus_remove(struct platform_device *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
David S. Miller8ef21752008-08-26 23:40:25 -07001245 struct bigmac *bp = dev_get_drvdata(&op->dev);
1246 struct device *parent = op->dev.parent;
David S. Miller52a34c72006-06-23 18:48:04 -07001247 struct net_device *net_dev = bp->dev;
Grant Likely2dc11582010-08-06 09:25:50 -06001248 struct platform_device *qec_op;
David S. Miller8ef21752008-08-26 23:40:25 -07001249
Grant Likely2dc11582010-08-06 09:25:50 -06001250 qec_op = to_platform_device(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Herbert Xu7afb9dc2008-10-05 09:20:28 -07001252 unregister_netdev(net_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
David S. Miller8ef21752008-08-26 23:40:25 -07001254 of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
1255 of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
1256 of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
1257 of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
1258 dma_free_coherent(&op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -07001259 PAGE_SIZE,
1260 bp->bmac_block,
1261 bp->bblock_dvma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
David S. Miller52a34c72006-06-23 18:48:04 -07001263 free_netdev(net_dev);
1264
David S. Miller8ef21752008-08-26 23:40:25 -07001265 dev_set_drvdata(&op->dev, NULL);
David S. Miller52a34c72006-06-23 18:48:04 -07001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return 0;
1268}
1269
David S. Millerfd098312008-08-31 01:23:17 -07001270static const struct of_device_id bigmac_sbus_match[] = {
David S. Miller52a34c72006-06-23 18:48:04 -07001271 {
David S. Miller52a34c72006-06-23 18:48:04 -07001272 .name = "be",
1273 },
1274 {},
1275};
1276
1277MODULE_DEVICE_TABLE(of, bigmac_sbus_match);
1278
Grant Likely74888762011-02-22 21:05:51 -07001279static struct platform_driver bigmac_sbus_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001280 .driver = {
1281 .name = "sunbmac",
1282 .owner = THIS_MODULE,
1283 .of_match_table = bigmac_sbus_match,
1284 },
David S. Miller52a34c72006-06-23 18:48:04 -07001285 .probe = bigmac_sbus_probe,
Bill Pembertonf73d12b2012-12-03 09:24:02 -05001286 .remove = bigmac_sbus_remove,
David S. Miller52a34c72006-06-23 18:48:04 -07001287};
1288
Axel Lindb62f682011-11-27 16:44:17 +00001289module_platform_driver(bigmac_sbus_driver);