2 * Microchip ENC28J60 ethernet driver (MAC + PHY)
4 * Copyright (C) 2007 Eurek srl
5 * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com>
6 * based on enc28j60.c written by David Anders for 2.4 kernel version
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * $Id: enc28j60.c,v 1.22 2007/12/20 10:47:01 claudio Exp $
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/fcntl.h>
20 #include <linux/interrupt.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/tcp.h>
28 #include <linux/skbuff.h>
29 #include <linux/delay.h>
30 #include <linux/spi/spi.h>
31 #include <linux/of_net.h>
33 #include "enc28j60_hw.h"
35 #define DRV_NAME "enc28j60"
36 #define DRV_VERSION "1.02"
40 #define ENC28J60_MSG_DEFAULT \
41 (NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN | NETIF_MSG_LINK)
43 /* Buffer size required for the largest SPI transfer (i.e., reading a
45 #define SPI_TRANSFER_BUF_LEN (4 + MAX_FRAMELEN)
47 #define TX_TIMEOUT (4 * HZ)
49 /* Max TX retries in case of collision as suggested by errata datasheet */
50 #define MAX_TX_RETRYCOUNT 16
58 /* Driver local data */
60 struct net_device
*netdev
;
61 struct spi_device
*spi
;
63 struct sk_buff
*tx_skb
;
64 struct work_struct tx_work
;
65 struct work_struct irq_work
;
66 struct work_struct setrx_work
;
67 struct work_struct restart_work
;
68 u8 bank
; /* current register bank selected */
69 u16 next_pk_ptr
; /* next packet pointer within FIFO */
70 u16 max_pk_counter
; /* statistics: max packet counter */
76 u8 spi_transfer_buf
[SPI_TRANSFER_BUF_LEN
];
79 /* use ethtool to change the level for any given device */
86 * wait for the SPI transfer and copy received data to destination
89 spi_read_buf(struct enc28j60_net
*priv
, int len
, u8
*data
)
91 u8
*rx_buf
= priv
->spi_transfer_buf
+ 4;
92 u8
*tx_buf
= priv
->spi_transfer_buf
;
93 struct spi_transfer tx
= {
97 struct spi_transfer rx
= {
101 struct spi_message msg
;
104 tx_buf
[0] = ENC28J60_READ_BUF_MEM
;
106 spi_message_init(&msg
);
107 spi_message_add_tail(&tx
, &msg
);
108 spi_message_add_tail(&rx
, &msg
);
110 ret
= spi_sync(priv
->spi
, &msg
);
112 memcpy(data
, rx_buf
, len
);
115 if (ret
&& netif_msg_drv(priv
))
116 printk(KERN_DEBUG DRV_NAME
": %s() failed: ret = %d\n",
125 static int spi_write_buf(struct enc28j60_net
*priv
, int len
,
130 if (len
> SPI_TRANSFER_BUF_LEN
- 1 || len
<= 0)
133 priv
->spi_transfer_buf
[0] = ENC28J60_WRITE_BUF_MEM
;
134 memcpy(&priv
->spi_transfer_buf
[1], data
, len
);
135 ret
= spi_write(priv
->spi
, priv
->spi_transfer_buf
, len
+ 1);
136 if (ret
&& netif_msg_drv(priv
))
137 printk(KERN_DEBUG DRV_NAME
": %s() failed: ret = %d\n",
144 * basic SPI read operation
146 static u8
spi_read_op(struct enc28j60_net
*priv
, u8 op
,
153 int slen
= SPI_OPLEN
;
155 /* do dummy read if needed */
156 if (addr
& SPRD_MASK
)
159 tx_buf
[0] = op
| (addr
& ADDR_MASK
);
160 ret
= spi_write_then_read(priv
->spi
, tx_buf
, 1, rx_buf
, slen
);
162 printk(KERN_DEBUG DRV_NAME
": %s() failed: ret = %d\n",
165 val
= rx_buf
[slen
- 1];
171 * basic SPI write operation
173 static int spi_write_op(struct enc28j60_net
*priv
, u8 op
,
178 priv
->spi_transfer_buf
[0] = op
| (addr
& ADDR_MASK
);
179 priv
->spi_transfer_buf
[1] = val
;
180 ret
= spi_write(priv
->spi
, priv
->spi_transfer_buf
, 2);
181 if (ret
&& netif_msg_drv(priv
))
182 printk(KERN_DEBUG DRV_NAME
": %s() failed: ret = %d\n",
187 static void enc28j60_soft_reset(struct enc28j60_net
*priv
)
189 if (netif_msg_hw(priv
))
190 printk(KERN_DEBUG DRV_NAME
": %s() enter\n", __func__
);
192 spi_write_op(priv
, ENC28J60_SOFT_RESET
, 0, ENC28J60_SOFT_RESET
);
193 /* Errata workaround #1, CLKRDY check is unreliable,
194 * delay at least 1 mS instead */
199 * select the current register bank if necessary
201 static void enc28j60_set_bank(struct enc28j60_net
*priv
, u8 addr
)
203 u8 b
= (addr
& BANK_MASK
) >> 5;
205 /* These registers (EIE, EIR, ESTAT, ECON2, ECON1)
206 * are present in all banks, no need to switch bank
208 if (addr
>= EIE
&& addr
<= ECON1
)
211 /* Clear or set each bank selection bit as needed */
212 if ((b
& ECON1_BSEL0
) != (priv
->bank
& ECON1_BSEL0
)) {
214 spi_write_op(priv
, ENC28J60_BIT_FIELD_SET
, ECON1
,
217 spi_write_op(priv
, ENC28J60_BIT_FIELD_CLR
, ECON1
,
220 if ((b
& ECON1_BSEL1
) != (priv
->bank
& ECON1_BSEL1
)) {
222 spi_write_op(priv
, ENC28J60_BIT_FIELD_SET
, ECON1
,
225 spi_write_op(priv
, ENC28J60_BIT_FIELD_CLR
, ECON1
,
232 * Register access routines through the SPI bus.
233 * Every register access comes in two flavours:
234 * - nolock_xxx: caller needs to invoke mutex_lock, usually to access
235 * atomically more than one register
236 * - locked_xxx: caller doesn't need to invoke mutex_lock, single access
238 * Some registers can be accessed through the bit field clear and
239 * bit field set to avoid a read modify write cycle.
243 * Register bit field Set
245 static void nolock_reg_bfset(struct enc28j60_net
*priv
,
248 enc28j60_set_bank(priv
, addr
);
249 spi_write_op(priv
, ENC28J60_BIT_FIELD_SET
, addr
, mask
);
252 static void locked_reg_bfset(struct enc28j60_net
*priv
,
255 mutex_lock(&priv
->lock
);
256 nolock_reg_bfset(priv
, addr
, mask
);
257 mutex_unlock(&priv
->lock
);
261 * Register bit field Clear
263 static void nolock_reg_bfclr(struct enc28j60_net
*priv
,
266 enc28j60_set_bank(priv
, addr
);
267 spi_write_op(priv
, ENC28J60_BIT_FIELD_CLR
, addr
, mask
);
270 static void locked_reg_bfclr(struct enc28j60_net
*priv
,
273 mutex_lock(&priv
->lock
);
274 nolock_reg_bfclr(priv
, addr
, mask
);
275 mutex_unlock(&priv
->lock
);
281 static int nolock_regb_read(struct enc28j60_net
*priv
,
284 enc28j60_set_bank(priv
, address
);
285 return spi_read_op(priv
, ENC28J60_READ_CTRL_REG
, address
);
288 static int locked_regb_read(struct enc28j60_net
*priv
,
293 mutex_lock(&priv
->lock
);
294 ret
= nolock_regb_read(priv
, address
);
295 mutex_unlock(&priv
->lock
);
303 static int nolock_regw_read(struct enc28j60_net
*priv
,
308 enc28j60_set_bank(priv
, address
);
309 rl
= spi_read_op(priv
, ENC28J60_READ_CTRL_REG
, address
);
310 rh
= spi_read_op(priv
, ENC28J60_READ_CTRL_REG
, address
+ 1);
312 return (rh
<< 8) | rl
;
315 static int locked_regw_read(struct enc28j60_net
*priv
,
320 mutex_lock(&priv
->lock
);
321 ret
= nolock_regw_read(priv
, address
);
322 mutex_unlock(&priv
->lock
);
328 * Register byte write
330 static void nolock_regb_write(struct enc28j60_net
*priv
,
333 enc28j60_set_bank(priv
, address
);
334 spi_write_op(priv
, ENC28J60_WRITE_CTRL_REG
, address
, data
);
337 static void locked_regb_write(struct enc28j60_net
*priv
,
340 mutex_lock(&priv
->lock
);
341 nolock_regb_write(priv
, address
, data
);
342 mutex_unlock(&priv
->lock
);
346 * Register word write
348 static void nolock_regw_write(struct enc28j60_net
*priv
,
349 u8 address
, u16 data
)
351 enc28j60_set_bank(priv
, address
);
352 spi_write_op(priv
, ENC28J60_WRITE_CTRL_REG
, address
, (u8
) data
);
353 spi_write_op(priv
, ENC28J60_WRITE_CTRL_REG
, address
+ 1,
357 static void locked_regw_write(struct enc28j60_net
*priv
,
358 u8 address
, u16 data
)
360 mutex_lock(&priv
->lock
);
361 nolock_regw_write(priv
, address
, data
);
362 mutex_unlock(&priv
->lock
);
367 * Select the starting address and execute a SPI buffer read
369 static void enc28j60_mem_read(struct enc28j60_net
*priv
,
370 u16 addr
, int len
, u8
*data
)
372 mutex_lock(&priv
->lock
);
373 nolock_regw_write(priv
, ERDPTL
, addr
);
374 #ifdef CONFIG_ENC28J60_WRITEVERIFY
375 if (netif_msg_drv(priv
)) {
377 reg
= nolock_regw_read(priv
, ERDPTL
);
379 printk(KERN_DEBUG DRV_NAME
": %s() error writing ERDPT "
380 "(0x%04x - 0x%04x)\n", __func__
, reg
, addr
);
383 spi_read_buf(priv
, len
, data
);
384 mutex_unlock(&priv
->lock
);
388 * Write packet to enc28j60 TX buffer memory
391 enc28j60_packet_write(struct enc28j60_net
*priv
, int len
, const u8
*data
)
393 mutex_lock(&priv
->lock
);
394 /* Set the write pointer to start of transmit buffer area */
395 nolock_regw_write(priv
, EWRPTL
, TXSTART_INIT
);
396 #ifdef CONFIG_ENC28J60_WRITEVERIFY
397 if (netif_msg_drv(priv
)) {
399 reg
= nolock_regw_read(priv
, EWRPTL
);
400 if (reg
!= TXSTART_INIT
)
401 printk(KERN_DEBUG DRV_NAME
402 ": %s() ERWPT:0x%04x != 0x%04x\n",
403 __func__
, reg
, TXSTART_INIT
);
406 /* Set the TXND pointer to correspond to the packet size given */
407 nolock_regw_write(priv
, ETXNDL
, TXSTART_INIT
+ len
);
408 /* write per-packet control byte */
409 spi_write_op(priv
, ENC28J60_WRITE_BUF_MEM
, 0, 0x00);
410 if (netif_msg_hw(priv
))
411 printk(KERN_DEBUG DRV_NAME
412 ": %s() after control byte ERWPT:0x%04x\n",
413 __func__
, nolock_regw_read(priv
, EWRPTL
));
414 /* copy the packet into the transmit buffer */
415 spi_write_buf(priv
, len
, data
);
416 if (netif_msg_hw(priv
))
417 printk(KERN_DEBUG DRV_NAME
418 ": %s() after write packet ERWPT:0x%04x, len=%d\n",
419 __func__
, nolock_regw_read(priv
, EWRPTL
), len
);
420 mutex_unlock(&priv
->lock
);
423 static unsigned long msec20_to_jiffies
;
425 static int poll_ready(struct enc28j60_net
*priv
, u8 reg
, u8 mask
, u8 val
)
427 unsigned long timeout
= jiffies
+ msec20_to_jiffies
;
429 /* 20 msec timeout read */
430 while ((nolock_regb_read(priv
, reg
) & mask
) != val
) {
431 if (time_after(jiffies
, timeout
)) {
432 if (netif_msg_drv(priv
))
433 dev_dbg(&priv
->spi
->dev
,
434 "reg %02x ready timeout!\n", reg
);
443 * Wait until the PHY operation is complete.
445 static int wait_phy_ready(struct enc28j60_net
*priv
)
447 return poll_ready(priv
, MISTAT
, MISTAT_BUSY
, 0) ? 0 : 1;
452 * PHY registers are not accessed directly, but through the MII
454 static u16
enc28j60_phy_read(struct enc28j60_net
*priv
, u8 address
)
458 mutex_lock(&priv
->lock
);
459 /* set the PHY register address */
460 nolock_regb_write(priv
, MIREGADR
, address
);
461 /* start the register read operation */
462 nolock_regb_write(priv
, MICMD
, MICMD_MIIRD
);
463 /* wait until the PHY read completes */
464 wait_phy_ready(priv
);
466 nolock_regb_write(priv
, MICMD
, 0x00);
467 /* return the data */
468 ret
= nolock_regw_read(priv
, MIRDL
);
469 mutex_unlock(&priv
->lock
);
474 static int enc28j60_phy_write(struct enc28j60_net
*priv
, u8 address
, u16 data
)
478 mutex_lock(&priv
->lock
);
479 /* set the PHY register address */
480 nolock_regb_write(priv
, MIREGADR
, address
);
481 /* write the PHY data */
482 nolock_regw_write(priv
, MIWRL
, data
);
483 /* wait until the PHY write completes and return */
484 ret
= wait_phy_ready(priv
);
485 mutex_unlock(&priv
->lock
);
491 * Program the hardware MAC address from dev->dev_addr.
493 static int enc28j60_set_hw_macaddr(struct net_device
*ndev
)
496 struct enc28j60_net
*priv
= netdev_priv(ndev
);
498 mutex_lock(&priv
->lock
);
499 if (!priv
->hw_enable
) {
500 if (netif_msg_drv(priv
))
501 printk(KERN_INFO DRV_NAME
502 ": %s: Setting MAC address to %pM\n",
503 ndev
->name
, ndev
->dev_addr
);
504 /* NOTE: MAC address in ENC28J60 is byte-backward */
505 nolock_regb_write(priv
, MAADR5
, ndev
->dev_addr
[0]);
506 nolock_regb_write(priv
, MAADR4
, ndev
->dev_addr
[1]);
507 nolock_regb_write(priv
, MAADR3
, ndev
->dev_addr
[2]);
508 nolock_regb_write(priv
, MAADR2
, ndev
->dev_addr
[3]);
509 nolock_regb_write(priv
, MAADR1
, ndev
->dev_addr
[4]);
510 nolock_regb_write(priv
, MAADR0
, ndev
->dev_addr
[5]);
513 if (netif_msg_drv(priv
))
514 printk(KERN_DEBUG DRV_NAME
515 ": %s() Hardware must be disabled to set "
516 "Mac address\n", __func__
);
519 mutex_unlock(&priv
->lock
);
524 * Store the new hardware address in dev->dev_addr, and update the MAC.
526 static int enc28j60_set_mac_address(struct net_device
*dev
, void *addr
)
528 struct sockaddr
*address
= addr
;
530 if (netif_running(dev
))
532 if (!is_valid_ether_addr(address
->sa_data
))
533 return -EADDRNOTAVAIL
;
535 memcpy(dev
->dev_addr
, address
->sa_data
, dev
->addr_len
);
536 return enc28j60_set_hw_macaddr(dev
);
540 * Debug routine to dump useful register contents
542 static void enc28j60_dump_regs(struct enc28j60_net
*priv
, const char *msg
)
544 mutex_lock(&priv
->lock
);
545 printk(KERN_DEBUG DRV_NAME
" %s\n"
547 "Cntrl: ECON1 ECON2 ESTAT EIR EIE\n"
548 " 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n"
549 "MAC : MACON1 MACON3 MACON4\n"
550 " 0x%02x 0x%02x 0x%02x\n"
551 "Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\n"
552 " 0x%04x 0x%04x 0x%04x 0x%04x "
553 "0x%02x 0x%02x 0x%04x\n"
554 "Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\n"
555 " 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x\n",
556 msg
, nolock_regb_read(priv
, EREVID
),
557 nolock_regb_read(priv
, ECON1
), nolock_regb_read(priv
, ECON2
),
558 nolock_regb_read(priv
, ESTAT
), nolock_regb_read(priv
, EIR
),
559 nolock_regb_read(priv
, EIE
), nolock_regb_read(priv
, MACON1
),
560 nolock_regb_read(priv
, MACON3
), nolock_regb_read(priv
, MACON4
),
561 nolock_regw_read(priv
, ERXSTL
), nolock_regw_read(priv
, ERXNDL
),
562 nolock_regw_read(priv
, ERXWRPTL
),
563 nolock_regw_read(priv
, ERXRDPTL
),
564 nolock_regb_read(priv
, ERXFCON
),
565 nolock_regb_read(priv
, EPKTCNT
),
566 nolock_regw_read(priv
, MAMXFLL
), nolock_regw_read(priv
, ETXSTL
),
567 nolock_regw_read(priv
, ETXNDL
),
568 nolock_regb_read(priv
, MACLCON1
),
569 nolock_regb_read(priv
, MACLCON2
),
570 nolock_regb_read(priv
, MAPHSUP
));
571 mutex_unlock(&priv
->lock
);
575 * ERXRDPT need to be set always at odd addresses, refer to errata datasheet
577 static u16
erxrdpt_workaround(u16 next_packet_ptr
, u16 start
, u16 end
)
581 if ((next_packet_ptr
- 1 < start
) || (next_packet_ptr
- 1 > end
))
584 erxrdpt
= next_packet_ptr
- 1;
590 * Calculate wrap around when reading beyond the end of the RX buffer
592 static u16
rx_packet_start(u16 ptr
)
594 if (ptr
+ RSV_SIZE
> RXEND_INIT
)
595 return (ptr
+ RSV_SIZE
) - (RXEND_INIT
- RXSTART_INIT
+ 1);
597 return ptr
+ RSV_SIZE
;
600 static void nolock_rxfifo_init(struct enc28j60_net
*priv
, u16 start
, u16 end
)
604 if (start
> 0x1FFF || end
> 0x1FFF || start
> end
) {
605 if (netif_msg_drv(priv
))
606 printk(KERN_ERR DRV_NAME
": %s(%d, %d) RXFIFO "
607 "bad parameters!\n", __func__
, start
, end
);
610 /* set receive buffer start + end */
611 priv
->next_pk_ptr
= start
;
612 nolock_regw_write(priv
, ERXSTL
, start
);
613 erxrdpt
= erxrdpt_workaround(priv
->next_pk_ptr
, start
, end
);
614 nolock_regw_write(priv
, ERXRDPTL
, erxrdpt
);
615 nolock_regw_write(priv
, ERXNDL
, end
);
618 static void nolock_txfifo_init(struct enc28j60_net
*priv
, u16 start
, u16 end
)
620 if (start
> 0x1FFF || end
> 0x1FFF || start
> end
) {
621 if (netif_msg_drv(priv
))
622 printk(KERN_ERR DRV_NAME
": %s(%d, %d) TXFIFO "
623 "bad parameters!\n", __func__
, start
, end
);
626 /* set transmit buffer start + end */
627 nolock_regw_write(priv
, ETXSTL
, start
);
628 nolock_regw_write(priv
, ETXNDL
, end
);
632 * Low power mode shrinks power consumption about 100x, so we'd like
633 * the chip to be in that mode whenever it's inactive. (However, we
634 * can't stay in lowpower mode during suspend with WOL active.)
636 static void enc28j60_lowpower(struct enc28j60_net
*priv
, bool is_low
)
638 if (netif_msg_drv(priv
))
639 dev_dbg(&priv
->spi
->dev
, "%s power...\n",
640 is_low
? "low" : "high");
642 mutex_lock(&priv
->lock
);
644 nolock_reg_bfclr(priv
, ECON1
, ECON1_RXEN
);
645 poll_ready(priv
, ESTAT
, ESTAT_RXBUSY
, 0);
646 poll_ready(priv
, ECON1
, ECON1_TXRTS
, 0);
647 /* ECON2_VRPS was set during initialization */
648 nolock_reg_bfset(priv
, ECON2
, ECON2_PWRSV
);
650 nolock_reg_bfclr(priv
, ECON2
, ECON2_PWRSV
);
651 poll_ready(priv
, ESTAT
, ESTAT_CLKRDY
, ESTAT_CLKRDY
);
652 /* caller sets ECON1_RXEN */
654 mutex_unlock(&priv
->lock
);
657 static int enc28j60_hw_init(struct enc28j60_net
*priv
)
661 if (netif_msg_drv(priv
))
662 printk(KERN_DEBUG DRV_NAME
": %s() - %s\n", __func__
,
663 priv
->full_duplex
? "FullDuplex" : "HalfDuplex");
665 mutex_lock(&priv
->lock
);
666 /* first reset the chip */
667 enc28j60_soft_reset(priv
);
669 spi_write_op(priv
, ENC28J60_WRITE_CTRL_REG
, ECON1
, 0x00);
671 priv
->hw_enable
= false;
672 priv
->tx_retry_count
= 0;
673 priv
->max_pk_counter
= 0;
674 priv
->rxfilter
= RXFILTER_NORMAL
;
675 /* enable address auto increment and voltage regulator powersave */
676 nolock_regb_write(priv
, ECON2
, ECON2_AUTOINC
| ECON2_VRPS
);
678 nolock_rxfifo_init(priv
, RXSTART_INIT
, RXEND_INIT
);
679 nolock_txfifo_init(priv
, TXSTART_INIT
, TXEND_INIT
);
680 mutex_unlock(&priv
->lock
);
684 * If it's 0x00 or 0xFF probably the enc28j60 is not mounted or
687 reg
= locked_regb_read(priv
, EREVID
);
688 if (netif_msg_drv(priv
))
689 printk(KERN_INFO DRV_NAME
": chip RevID: 0x%02x\n", reg
);
690 if (reg
== 0x00 || reg
== 0xff) {
691 if (netif_msg_drv(priv
))
692 printk(KERN_DEBUG DRV_NAME
": %s() Invalid RevId %d\n",
697 /* default filter mode: (unicast OR broadcast) AND crc valid */
698 locked_regb_write(priv
, ERXFCON
,
699 ERXFCON_UCEN
| ERXFCON_CRCEN
| ERXFCON_BCEN
);
701 /* enable MAC receive */
702 locked_regb_write(priv
, MACON1
,
703 MACON1_MARXEN
| MACON1_TXPAUS
| MACON1_RXPAUS
);
704 /* enable automatic padding and CRC operations */
705 if (priv
->full_duplex
) {
706 locked_regb_write(priv
, MACON3
,
707 MACON3_PADCFG0
| MACON3_TXCRCEN
|
708 MACON3_FRMLNEN
| MACON3_FULDPX
);
709 /* set inter-frame gap (non-back-to-back) */
710 locked_regb_write(priv
, MAIPGL
, 0x12);
711 /* set inter-frame gap (back-to-back) */
712 locked_regb_write(priv
, MABBIPG
, 0x15);
714 locked_regb_write(priv
, MACON3
,
715 MACON3_PADCFG0
| MACON3_TXCRCEN
|
717 locked_regb_write(priv
, MACON4
, 1 << 6); /* DEFER bit */
718 /* set inter-frame gap (non-back-to-back) */
719 locked_regw_write(priv
, MAIPGL
, 0x0C12);
720 /* set inter-frame gap (back-to-back) */
721 locked_regb_write(priv
, MABBIPG
, 0x12);
726 * Set the maximum packet size which the controller will accept
728 locked_regw_write(priv
, MAMXFLL
, MAX_FRAMELEN
);
731 if (!enc28j60_phy_write(priv
, PHLCON
, ENC28J60_LAMPS_MODE
))
734 if (priv
->full_duplex
) {
735 if (!enc28j60_phy_write(priv
, PHCON1
, PHCON1_PDPXMD
))
737 if (!enc28j60_phy_write(priv
, PHCON2
, 0x00))
740 if (!enc28j60_phy_write(priv
, PHCON1
, 0x00))
742 if (!enc28j60_phy_write(priv
, PHCON2
, PHCON2_HDLDIS
))
745 if (netif_msg_hw(priv
))
746 enc28j60_dump_regs(priv
, "Hw initialized.");
751 static void enc28j60_hw_enable(struct enc28j60_net
*priv
)
753 /* enable interrupts */
754 if (netif_msg_hw(priv
))
755 printk(KERN_DEBUG DRV_NAME
": %s() enabling interrupts.\n",
758 enc28j60_phy_write(priv
, PHIE
, PHIE_PGEIE
| PHIE_PLNKIE
);
760 mutex_lock(&priv
->lock
);
761 nolock_reg_bfclr(priv
, EIR
, EIR_DMAIF
| EIR_LINKIF
|
762 EIR_TXIF
| EIR_TXERIF
| EIR_RXERIF
| EIR_PKTIF
);
763 nolock_regb_write(priv
, EIE
, EIE_INTIE
| EIE_PKTIE
| EIE_LINKIE
|
764 EIE_TXIE
| EIE_TXERIE
| EIE_RXERIE
);
766 /* enable receive logic */
767 nolock_reg_bfset(priv
, ECON1
, ECON1_RXEN
);
768 priv
->hw_enable
= true;
769 mutex_unlock(&priv
->lock
);
772 static void enc28j60_hw_disable(struct enc28j60_net
*priv
)
774 mutex_lock(&priv
->lock
);
775 /* disable interrutps and packet reception */
776 nolock_regb_write(priv
, EIE
, 0x00);
777 nolock_reg_bfclr(priv
, ECON1
, ECON1_RXEN
);
778 priv
->hw_enable
= false;
779 mutex_unlock(&priv
->lock
);
783 enc28j60_setlink(struct net_device
*ndev
, u8 autoneg
, u16 speed
, u8 duplex
)
785 struct enc28j60_net
*priv
= netdev_priv(ndev
);
788 if (!priv
->hw_enable
) {
789 /* link is in low power mode now; duplex setting
790 * will take effect on next enc28j60_hw_init().
792 if (autoneg
== AUTONEG_DISABLE
&& speed
== SPEED_10
)
793 priv
->full_duplex
= (duplex
== DUPLEX_FULL
);
795 if (netif_msg_link(priv
))
797 "unsupported link setting\n");
801 if (netif_msg_link(priv
))
802 dev_warn(&ndev
->dev
, "Warning: hw must be disabled "
803 "to set link mode\n");
810 * Read the Transmit Status Vector
812 static void enc28j60_read_tsv(struct enc28j60_net
*priv
, u8 tsv
[TSV_SIZE
])
816 endptr
= locked_regw_read(priv
, ETXNDL
);
817 if (netif_msg_hw(priv
))
818 printk(KERN_DEBUG DRV_NAME
": reading TSV at addr:0x%04x\n",
820 enc28j60_mem_read(priv
, endptr
+ 1, TSV_SIZE
, tsv
);
823 static void enc28j60_dump_tsv(struct enc28j60_net
*priv
, const char *msg
,
828 printk(KERN_DEBUG DRV_NAME
": %s - TSV:\n", msg
);
837 printk(KERN_DEBUG DRV_NAME
": ByteCount: %d, CollisionCount: %d,"
838 " TotByteOnWire: %d\n", tmp1
, tsv
[2] & 0x0f, tmp2
);
839 printk(KERN_DEBUG DRV_NAME
": TxDone: %d, CRCErr:%d, LenChkErr: %d,"
840 " LenOutOfRange: %d\n", TSV_GETBIT(tsv
, TSV_TXDONE
),
841 TSV_GETBIT(tsv
, TSV_TXCRCERROR
),
842 TSV_GETBIT(tsv
, TSV_TXLENCHKERROR
),
843 TSV_GETBIT(tsv
, TSV_TXLENOUTOFRANGE
));
844 printk(KERN_DEBUG DRV_NAME
": Multicast: %d, Broadcast: %d, "
845 "PacketDefer: %d, ExDefer: %d\n",
846 TSV_GETBIT(tsv
, TSV_TXMULTICAST
),
847 TSV_GETBIT(tsv
, TSV_TXBROADCAST
),
848 TSV_GETBIT(tsv
, TSV_TXPACKETDEFER
),
849 TSV_GETBIT(tsv
, TSV_TXEXDEFER
));
850 printk(KERN_DEBUG DRV_NAME
": ExCollision: %d, LateCollision: %d, "
851 "Giant: %d, Underrun: %d\n",
852 TSV_GETBIT(tsv
, TSV_TXEXCOLLISION
),
853 TSV_GETBIT(tsv
, TSV_TXLATECOLLISION
),
854 TSV_GETBIT(tsv
, TSV_TXGIANT
), TSV_GETBIT(tsv
, TSV_TXUNDERRUN
));
855 printk(KERN_DEBUG DRV_NAME
": ControlFrame: %d, PauseFrame: %d, "
856 "BackPressApp: %d, VLanTagFrame: %d\n",
857 TSV_GETBIT(tsv
, TSV_TXCONTROLFRAME
),
858 TSV_GETBIT(tsv
, TSV_TXPAUSEFRAME
),
859 TSV_GETBIT(tsv
, TSV_BACKPRESSUREAPP
),
860 TSV_GETBIT(tsv
, TSV_TXVLANTAGFRAME
));
864 * Receive Status vector
866 static void enc28j60_dump_rsv(struct enc28j60_net
*priv
, const char *msg
,
867 u16 pk_ptr
, int len
, u16 sts
)
869 printk(KERN_DEBUG DRV_NAME
": %s - NextPk: 0x%04x - RSV:\n",
871 printk(KERN_DEBUG DRV_NAME
": ByteCount: %d, DribbleNibble: %d\n", len
,
872 RSV_GETBIT(sts
, RSV_DRIBBLENIBBLE
));
873 printk(KERN_DEBUG DRV_NAME
": RxOK: %d, CRCErr:%d, LenChkErr: %d,"
874 " LenOutOfRange: %d\n", RSV_GETBIT(sts
, RSV_RXOK
),
875 RSV_GETBIT(sts
, RSV_CRCERROR
),
876 RSV_GETBIT(sts
, RSV_LENCHECKERR
),
877 RSV_GETBIT(sts
, RSV_LENOUTOFRANGE
));
878 printk(KERN_DEBUG DRV_NAME
": Multicast: %d, Broadcast: %d, "
879 "LongDropEvent: %d, CarrierEvent: %d\n",
880 RSV_GETBIT(sts
, RSV_RXMULTICAST
),
881 RSV_GETBIT(sts
, RSV_RXBROADCAST
),
882 RSV_GETBIT(sts
, RSV_RXLONGEVDROPEV
),
883 RSV_GETBIT(sts
, RSV_CARRIEREV
));
884 printk(KERN_DEBUG DRV_NAME
": ControlFrame: %d, PauseFrame: %d,"
885 " UnknownOp: %d, VLanTagFrame: %d\n",
886 RSV_GETBIT(sts
, RSV_RXCONTROLFRAME
),
887 RSV_GETBIT(sts
, RSV_RXPAUSEFRAME
),
888 RSV_GETBIT(sts
, RSV_RXUNKNOWNOPCODE
),
889 RSV_GETBIT(sts
, RSV_RXTYPEVLAN
));
892 static void dump_packet(const char *msg
, int len
, const char *data
)
894 printk(KERN_DEBUG DRV_NAME
": %s - packet len:%d\n", msg
, len
);
895 print_hex_dump(KERN_DEBUG
, "pk data: ", DUMP_PREFIX_OFFSET
, 16, 1,
900 * Hardware receive function.
901 * Read the buffer memory, update the FIFO pointer to free the buffer,
902 * check the status vector and decrement the packet counter.
904 static void enc28j60_hw_rx(struct net_device
*ndev
)
906 struct enc28j60_net
*priv
= netdev_priv(ndev
);
907 struct sk_buff
*skb
= NULL
;
908 u16 erxrdpt
, next_packet
, rxstat
;
912 if (netif_msg_rx_status(priv
))
913 printk(KERN_DEBUG DRV_NAME
": RX pk_addr:0x%04x\n",
916 if (unlikely(priv
->next_pk_ptr
> RXEND_INIT
)) {
917 if (netif_msg_rx_err(priv
))
919 "%s() Invalid packet address!! 0x%04x\n",
920 __func__
, priv
->next_pk_ptr
);
921 /* packet address corrupted: reset RX logic */
922 mutex_lock(&priv
->lock
);
923 nolock_reg_bfclr(priv
, ECON1
, ECON1_RXEN
);
924 nolock_reg_bfset(priv
, ECON1
, ECON1_RXRST
);
925 nolock_reg_bfclr(priv
, ECON1
, ECON1_RXRST
);
926 nolock_rxfifo_init(priv
, RXSTART_INIT
, RXEND_INIT
);
927 nolock_reg_bfclr(priv
, EIR
, EIR_RXERIF
);
928 nolock_reg_bfset(priv
, ECON1
, ECON1_RXEN
);
929 mutex_unlock(&priv
->lock
);
930 ndev
->stats
.rx_errors
++;
933 /* Read next packet pointer and rx status vector */
934 enc28j60_mem_read(priv
, priv
->next_pk_ptr
, sizeof(rsv
), rsv
);
936 next_packet
= rsv
[1];
938 next_packet
|= rsv
[0];
948 if (netif_msg_rx_status(priv
))
949 enc28j60_dump_rsv(priv
, __func__
, next_packet
, len
, rxstat
);
951 if (!RSV_GETBIT(rxstat
, RSV_RXOK
) || len
> MAX_FRAMELEN
) {
952 if (netif_msg_rx_err(priv
))
953 dev_err(&ndev
->dev
, "Rx Error (%04x)\n", rxstat
);
954 ndev
->stats
.rx_errors
++;
955 if (RSV_GETBIT(rxstat
, RSV_CRCERROR
))
956 ndev
->stats
.rx_crc_errors
++;
957 if (RSV_GETBIT(rxstat
, RSV_LENCHECKERR
))
958 ndev
->stats
.rx_frame_errors
++;
959 if (len
> MAX_FRAMELEN
)
960 ndev
->stats
.rx_over_errors
++;
962 skb
= netdev_alloc_skb(ndev
, len
+ NET_IP_ALIGN
);
964 if (netif_msg_rx_err(priv
))
966 "out of memory for Rx'd frame\n");
967 ndev
->stats
.rx_dropped
++;
969 skb_reserve(skb
, NET_IP_ALIGN
);
970 /* copy the packet from the receive buffer */
971 enc28j60_mem_read(priv
,
972 rx_packet_start(priv
->next_pk_ptr
),
973 len
, skb_put(skb
, len
));
974 if (netif_msg_pktdata(priv
))
975 dump_packet(__func__
, skb
->len
, skb
->data
);
976 skb
->protocol
= eth_type_trans(skb
, ndev
);
977 /* update statistics */
978 ndev
->stats
.rx_packets
++;
979 ndev
->stats
.rx_bytes
+= len
;
984 * Move the RX read pointer to the start of the next
986 * This frees the memory we just read out
988 erxrdpt
= erxrdpt_workaround(next_packet
, RXSTART_INIT
, RXEND_INIT
);
989 if (netif_msg_hw(priv
))
990 printk(KERN_DEBUG DRV_NAME
": %s() ERXRDPT:0x%04x\n",
993 mutex_lock(&priv
->lock
);
994 nolock_regw_write(priv
, ERXRDPTL
, erxrdpt
);
995 #ifdef CONFIG_ENC28J60_WRITEVERIFY
996 if (netif_msg_drv(priv
)) {
998 reg
= nolock_regw_read(priv
, ERXRDPTL
);
1000 printk(KERN_DEBUG DRV_NAME
": %s() ERXRDPT verify "
1001 "error (0x%04x - 0x%04x)\n", __func__
,
1005 priv
->next_pk_ptr
= next_packet
;
1006 /* we are done with this packet, decrement the packet counter */
1007 nolock_reg_bfset(priv
, ECON2
, ECON2_PKTDEC
);
1008 mutex_unlock(&priv
->lock
);
1012 * Calculate free space in RxFIFO
1014 static int enc28j60_get_free_rxfifo(struct enc28j60_net
*priv
)
1016 int epkcnt
, erxst
, erxnd
, erxwr
, erxrd
;
1019 mutex_lock(&priv
->lock
);
1020 epkcnt
= nolock_regb_read(priv
, EPKTCNT
);
1024 erxst
= nolock_regw_read(priv
, ERXSTL
);
1025 erxnd
= nolock_regw_read(priv
, ERXNDL
);
1026 erxwr
= nolock_regw_read(priv
, ERXWRPTL
);
1027 erxrd
= nolock_regw_read(priv
, ERXRDPTL
);
1030 free_space
= (erxnd
- erxst
) - (erxwr
- erxrd
);
1031 else if (erxwr
== erxrd
)
1032 free_space
= (erxnd
- erxst
);
1034 free_space
= erxrd
- erxwr
- 1;
1036 mutex_unlock(&priv
->lock
);
1037 if (netif_msg_rx_status(priv
))
1038 printk(KERN_DEBUG DRV_NAME
": %s() free_space = %d\n",
1039 __func__
, free_space
);
1044 * Access the PHY to determine link status
1046 static void enc28j60_check_link_status(struct net_device
*ndev
)
1048 struct enc28j60_net
*priv
= netdev_priv(ndev
);
1052 reg
= enc28j60_phy_read(priv
, PHSTAT2
);
1053 if (netif_msg_hw(priv
))
1054 printk(KERN_DEBUG DRV_NAME
": %s() PHSTAT1: %04x, "
1055 "PHSTAT2: %04x\n", __func__
,
1056 enc28j60_phy_read(priv
, PHSTAT1
), reg
);
1057 duplex
= reg
& PHSTAT2_DPXSTAT
;
1059 if (reg
& PHSTAT2_LSTAT
) {
1060 netif_carrier_on(ndev
);
1061 if (netif_msg_ifup(priv
))
1062 dev_info(&ndev
->dev
, "link up - %s\n",
1063 duplex
? "Full duplex" : "Half duplex");
1065 if (netif_msg_ifdown(priv
))
1066 dev_info(&ndev
->dev
, "link down\n");
1067 netif_carrier_off(ndev
);
1071 static void enc28j60_tx_clear(struct net_device
*ndev
, bool err
)
1073 struct enc28j60_net
*priv
= netdev_priv(ndev
);
1076 ndev
->stats
.tx_errors
++;
1078 ndev
->stats
.tx_packets
++;
1082 ndev
->stats
.tx_bytes
+= priv
->tx_skb
->len
;
1083 dev_kfree_skb(priv
->tx_skb
);
1084 priv
->tx_skb
= NULL
;
1086 locked_reg_bfclr(priv
, ECON1
, ECON1_TXRTS
);
1087 netif_wake_queue(ndev
);
1092 * ignore PKTIF because is unreliable! (look at the errata datasheet)
1093 * check EPKTCNT is the suggested workaround.
1094 * We don't need to clear interrupt flag, automatically done when
1095 * enc28j60_hw_rx() decrements the packet counter.
1096 * Returns how many packet processed.
1098 static int enc28j60_rx_interrupt(struct net_device
*ndev
)
1100 struct enc28j60_net
*priv
= netdev_priv(ndev
);
1101 int pk_counter
, ret
;
1103 pk_counter
= locked_regb_read(priv
, EPKTCNT
);
1104 if (pk_counter
&& netif_msg_intr(priv
))
1105 printk(KERN_DEBUG DRV_NAME
": intRX, pk_cnt: %d\n", pk_counter
);
1106 if (pk_counter
> priv
->max_pk_counter
) {
1107 /* update statistics */
1108 priv
->max_pk_counter
= pk_counter
;
1109 if (netif_msg_rx_status(priv
) && priv
->max_pk_counter
> 1)
1110 printk(KERN_DEBUG DRV_NAME
": RX max_pk_cnt: %d\n",
1111 priv
->max_pk_counter
);
1114 while (pk_counter
-- > 0)
1115 enc28j60_hw_rx(ndev
);
1120 static void enc28j60_irq_work_handler(struct work_struct
*work
)
1122 struct enc28j60_net
*priv
=
1123 container_of(work
, struct enc28j60_net
, irq_work
);
1124 struct net_device
*ndev
= priv
->netdev
;
1127 if (netif_msg_intr(priv
))
1128 printk(KERN_DEBUG DRV_NAME
": %s() enter\n", __func__
);
1129 /* disable further interrupts */
1130 locked_reg_bfclr(priv
, EIE
, EIE_INTIE
);
1134 intflags
= locked_regb_read(priv
, EIR
);
1135 /* DMA interrupt handler (not currently used) */
1136 if ((intflags
& EIR_DMAIF
) != 0) {
1138 if (netif_msg_intr(priv
))
1139 printk(KERN_DEBUG DRV_NAME
1140 ": intDMA(%d)\n", loop
);
1141 locked_reg_bfclr(priv
, EIR
, EIR_DMAIF
);
1143 /* LINK changed handler */
1144 if ((intflags
& EIR_LINKIF
) != 0) {
1146 if (netif_msg_intr(priv
))
1147 printk(KERN_DEBUG DRV_NAME
1148 ": intLINK(%d)\n", loop
);
1149 enc28j60_check_link_status(ndev
);
1150 /* read PHIR to clear the flag */
1151 enc28j60_phy_read(priv
, PHIR
);
1153 /* TX complete handler */
1154 if (((intflags
& EIR_TXIF
) != 0) &&
1155 ((intflags
& EIR_TXERIF
) == 0)) {
1158 if (netif_msg_intr(priv
))
1159 printk(KERN_DEBUG DRV_NAME
1160 ": intTX(%d)\n", loop
);
1161 priv
->tx_retry_count
= 0;
1162 if (locked_regb_read(priv
, ESTAT
) & ESTAT_TXABRT
) {
1163 if (netif_msg_tx_err(priv
))
1165 "Tx Error (aborted)\n");
1168 if (netif_msg_tx_done(priv
)) {
1170 enc28j60_read_tsv(priv
, tsv
);
1171 enc28j60_dump_tsv(priv
, "Tx Done", tsv
);
1173 enc28j60_tx_clear(ndev
, err
);
1174 locked_reg_bfclr(priv
, EIR
, EIR_TXIF
);
1176 /* TX Error handler */
1177 if ((intflags
& EIR_TXERIF
) != 0) {
1181 if (netif_msg_intr(priv
))
1182 printk(KERN_DEBUG DRV_NAME
1183 ": intTXErr(%d)\n", loop
);
1184 locked_reg_bfclr(priv
, ECON1
, ECON1_TXRTS
);
1185 enc28j60_read_tsv(priv
, tsv
);
1186 if (netif_msg_tx_err(priv
))
1187 enc28j60_dump_tsv(priv
, "Tx Error", tsv
);
1188 /* Reset TX logic */
1189 mutex_lock(&priv
->lock
);
1190 nolock_reg_bfset(priv
, ECON1
, ECON1_TXRST
);
1191 nolock_reg_bfclr(priv
, ECON1
, ECON1_TXRST
);
1192 nolock_txfifo_init(priv
, TXSTART_INIT
, TXEND_INIT
);
1193 mutex_unlock(&priv
->lock
);
1194 /* Transmit Late collision check for retransmit */
1195 if (TSV_GETBIT(tsv
, TSV_TXLATECOLLISION
)) {
1196 if (netif_msg_tx_err(priv
))
1197 printk(KERN_DEBUG DRV_NAME
1198 ": LateCollision TXErr (%d)\n",
1199 priv
->tx_retry_count
);
1200 if (priv
->tx_retry_count
++ < MAX_TX_RETRYCOUNT
)
1201 locked_reg_bfset(priv
, ECON1
,
1204 enc28j60_tx_clear(ndev
, true);
1206 enc28j60_tx_clear(ndev
, true);
1207 locked_reg_bfclr(priv
, EIR
, EIR_TXERIF
| EIR_TXIF
);
1209 /* RX Error handler */
1210 if ((intflags
& EIR_RXERIF
) != 0) {
1212 if (netif_msg_intr(priv
))
1213 printk(KERN_DEBUG DRV_NAME
1214 ": intRXErr(%d)\n", loop
);
1215 /* Check free FIFO space to flag RX overrun */
1216 if (enc28j60_get_free_rxfifo(priv
) <= 0) {
1217 if (netif_msg_rx_err(priv
))
1218 printk(KERN_DEBUG DRV_NAME
1220 ndev
->stats
.rx_dropped
++;
1222 locked_reg_bfclr(priv
, EIR
, EIR_RXERIF
);
1225 if (enc28j60_rx_interrupt(ndev
))
1229 /* re-enable interrupts */
1230 locked_reg_bfset(priv
, EIE
, EIE_INTIE
);
1231 if (netif_msg_intr(priv
))
1232 printk(KERN_DEBUG DRV_NAME
": %s() exit\n", __func__
);
1236 * Hardware transmit function.
1237 * Fill the buffer memory and send the contents of the transmit buffer
1240 static void enc28j60_hw_tx(struct enc28j60_net
*priv
)
1242 BUG_ON(!priv
->tx_skb
);
1244 if (netif_msg_tx_queued(priv
))
1245 printk(KERN_DEBUG DRV_NAME
1246 ": Tx Packet Len:%d\n", priv
->tx_skb
->len
);
1248 if (netif_msg_pktdata(priv
))
1249 dump_packet(__func__
,
1250 priv
->tx_skb
->len
, priv
->tx_skb
->data
);
1251 enc28j60_packet_write(priv
, priv
->tx_skb
->len
, priv
->tx_skb
->data
);
1253 #ifdef CONFIG_ENC28J60_WRITEVERIFY
1254 /* readback and verify written data */
1255 if (netif_msg_drv(priv
)) {
1257 u8 test_buf
[64]; /* limit the test to the first 64 bytes */
1260 test_len
= priv
->tx_skb
->len
;
1261 if (test_len
> sizeof(test_buf
))
1262 test_len
= sizeof(test_buf
);
1264 /* + 1 to skip control byte */
1265 enc28j60_mem_read(priv
, TXSTART_INIT
+ 1, test_len
, test_buf
);
1267 for (k
= 0; k
< test_len
; k
++) {
1268 if (priv
->tx_skb
->data
[k
] != test_buf
[k
]) {
1269 printk(KERN_DEBUG DRV_NAME
1270 ": Error, %d location differ: "
1271 "0x%02x-0x%02x\n", k
,
1272 priv
->tx_skb
->data
[k
], test_buf
[k
]);
1277 printk(KERN_DEBUG DRV_NAME
": Tx write buffer, "
1281 /* set TX request flag */
1282 locked_reg_bfset(priv
, ECON1
, ECON1_TXRTS
);
1285 static netdev_tx_t
enc28j60_send_packet(struct sk_buff
*skb
,
1286 struct net_device
*dev
)
1288 struct enc28j60_net
*priv
= netdev_priv(dev
);
1290 if (netif_msg_tx_queued(priv
))
1291 printk(KERN_DEBUG DRV_NAME
": %s() enter\n", __func__
);
1293 /* If some error occurs while trying to transmit this
1294 * packet, you should return '1' from this function.
1295 * In such a case you _may not_ do anything to the
1296 * SKB, it is still owned by the network queueing
1297 * layer when an error is returned. This means you
1298 * may not modify any SKB fields, you may not free
1301 netif_stop_queue(dev
);
1303 /* Remember the skb for deferred processing */
1305 schedule_work(&priv
->tx_work
);
1307 return NETDEV_TX_OK
;
1310 static void enc28j60_tx_work_handler(struct work_struct
*work
)
1312 struct enc28j60_net
*priv
=
1313 container_of(work
, struct enc28j60_net
, tx_work
);
1315 /* actual delivery of data */
1316 enc28j60_hw_tx(priv
);
1319 static irqreturn_t
enc28j60_irq(int irq
, void *dev_id
)
1321 struct enc28j60_net
*priv
= dev_id
;
1324 * Can't do anything in interrupt context because we need to
1325 * block (spi_sync() is blocking) so fire of the interrupt
1326 * handling workqueue.
1327 * Remember that we access enc28j60 registers through SPI bus
1328 * via spi_sync() call.
1330 schedule_work(&priv
->irq_work
);
1335 static void enc28j60_tx_timeout(struct net_device
*ndev
)
1337 struct enc28j60_net
*priv
= netdev_priv(ndev
);
1339 if (netif_msg_timer(priv
))
1340 dev_err(&ndev
->dev
, DRV_NAME
" tx timeout\n");
1342 ndev
->stats
.tx_errors
++;
1343 /* can't restart safely under softirq */
1344 schedule_work(&priv
->restart_work
);
1348 * Open/initialize the board. This is called (in the current kernel)
1349 * sometime after booting when the 'ifconfig' program is run.
1351 * This routine should set everything up anew at each open, even
1352 * registers that "should" only need to be set once at boot, so that
1353 * there is non-reboot way to recover if something goes wrong.
1355 static int enc28j60_net_open(struct net_device
*dev
)
1357 struct enc28j60_net
*priv
= netdev_priv(dev
);
1359 if (netif_msg_drv(priv
))
1360 printk(KERN_DEBUG DRV_NAME
": %s() enter\n", __func__
);
1362 if (!is_valid_ether_addr(dev
->dev_addr
)) {
1363 if (netif_msg_ifup(priv
))
1364 dev_err(&dev
->dev
, "invalid MAC address %pM\n",
1366 return -EADDRNOTAVAIL
;
1368 /* Reset the hardware here (and take it out of low power mode) */
1369 enc28j60_lowpower(priv
, false);
1370 enc28j60_hw_disable(priv
);
1371 if (!enc28j60_hw_init(priv
)) {
1372 if (netif_msg_ifup(priv
))
1373 dev_err(&dev
->dev
, "hw_reset() failed\n");
1376 /* Update the MAC address (in case user has changed it) */
1377 enc28j60_set_hw_macaddr(dev
);
1378 /* Enable interrupts */
1379 enc28j60_hw_enable(priv
);
1380 /* check link status */
1381 enc28j60_check_link_status(dev
);
1382 /* We are now ready to accept transmit requests from
1383 * the queueing layer of the networking.
1385 netif_start_queue(dev
);
1390 /* The inverse routine to net_open(). */
1391 static int enc28j60_net_close(struct net_device
*dev
)
1393 struct enc28j60_net
*priv
= netdev_priv(dev
);
1395 if (netif_msg_drv(priv
))
1396 printk(KERN_DEBUG DRV_NAME
": %s() enter\n", __func__
);
1398 enc28j60_hw_disable(priv
);
1399 enc28j60_lowpower(priv
, true);
1400 netif_stop_queue(dev
);
1406 * Set or clear the multicast filter for this adapter
1407 * num_addrs == -1 Promiscuous mode, receive all packets
1408 * num_addrs == 0 Normal mode, filter out multicast packets
1409 * num_addrs > 0 Multicast mode, receive normal and MC packets
1411 static void enc28j60_set_multicast_list(struct net_device
*dev
)
1413 struct enc28j60_net
*priv
= netdev_priv(dev
);
1414 int oldfilter
= priv
->rxfilter
;
1416 if (dev
->flags
& IFF_PROMISC
) {
1417 if (netif_msg_link(priv
))
1418 dev_info(&dev
->dev
, "promiscuous mode\n");
1419 priv
->rxfilter
= RXFILTER_PROMISC
;
1420 } else if ((dev
->flags
& IFF_ALLMULTI
) || !netdev_mc_empty(dev
)) {
1421 if (netif_msg_link(priv
))
1422 dev_info(&dev
->dev
, "%smulticast mode\n",
1423 (dev
->flags
& IFF_ALLMULTI
) ? "all-" : "");
1424 priv
->rxfilter
= RXFILTER_MULTI
;
1426 if (netif_msg_link(priv
))
1427 dev_info(&dev
->dev
, "normal mode\n");
1428 priv
->rxfilter
= RXFILTER_NORMAL
;
1431 if (oldfilter
!= priv
->rxfilter
)
1432 schedule_work(&priv
->setrx_work
);
1435 static void enc28j60_setrx_work_handler(struct work_struct
*work
)
1437 struct enc28j60_net
*priv
=
1438 container_of(work
, struct enc28j60_net
, setrx_work
);
1440 if (priv
->rxfilter
== RXFILTER_PROMISC
) {
1441 if (netif_msg_drv(priv
))
1442 printk(KERN_DEBUG DRV_NAME
": promiscuous mode\n");
1443 locked_regb_write(priv
, ERXFCON
, 0x00);
1444 } else if (priv
->rxfilter
== RXFILTER_MULTI
) {
1445 if (netif_msg_drv(priv
))
1446 printk(KERN_DEBUG DRV_NAME
": multicast mode\n");
1447 locked_regb_write(priv
, ERXFCON
,
1448 ERXFCON_UCEN
| ERXFCON_CRCEN
|
1449 ERXFCON_BCEN
| ERXFCON_MCEN
);
1451 if (netif_msg_drv(priv
))
1452 printk(KERN_DEBUG DRV_NAME
": normal mode\n");
1453 locked_regb_write(priv
, ERXFCON
,
1454 ERXFCON_UCEN
| ERXFCON_CRCEN
|
1459 static void enc28j60_restart_work_handler(struct work_struct
*work
)
1461 struct enc28j60_net
*priv
=
1462 container_of(work
, struct enc28j60_net
, restart_work
);
1463 struct net_device
*ndev
= priv
->netdev
;
1467 if (netif_running(ndev
)) {
1468 enc28j60_net_close(ndev
);
1469 ret
= enc28j60_net_open(ndev
);
1470 if (unlikely(ret
)) {
1471 dev_info(&ndev
->dev
, " could not restart %d\n", ret
);
1478 /* ......................... ETHTOOL SUPPORT ........................... */
1481 enc28j60_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1483 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1484 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1485 strlcpy(info
->bus_info
,
1486 dev_name(dev
->dev
.parent
), sizeof(info
->bus_info
));
1490 enc28j60_get_link_ksettings(struct net_device
*dev
,
1491 struct ethtool_link_ksettings
*cmd
)
1493 struct enc28j60_net
*priv
= netdev_priv(dev
);
1495 ethtool_link_ksettings_zero_link_mode(cmd
, supported
);
1496 ethtool_link_ksettings_add_link_mode(cmd
, supported
, 10baseT_Half
);
1497 ethtool_link_ksettings_add_link_mode(cmd
, supported
, 10baseT_Full
);
1498 ethtool_link_ksettings_add_link_mode(cmd
, supported
, TP
);
1500 cmd
->base
.speed
= SPEED_10
;
1501 cmd
->base
.duplex
= priv
->full_duplex
? DUPLEX_FULL
: DUPLEX_HALF
;
1502 cmd
->base
.port
= PORT_TP
;
1503 cmd
->base
.autoneg
= AUTONEG_DISABLE
;
1509 enc28j60_set_link_ksettings(struct net_device
*dev
,
1510 const struct ethtool_link_ksettings
*cmd
)
1512 return enc28j60_setlink(dev
, cmd
->base
.autoneg
,
1513 cmd
->base
.speed
, cmd
->base
.duplex
);
1516 static u32
enc28j60_get_msglevel(struct net_device
*dev
)
1518 struct enc28j60_net
*priv
= netdev_priv(dev
);
1519 return priv
->msg_enable
;
1522 static void enc28j60_set_msglevel(struct net_device
*dev
, u32 val
)
1524 struct enc28j60_net
*priv
= netdev_priv(dev
);
1525 priv
->msg_enable
= val
;
1528 static const struct ethtool_ops enc28j60_ethtool_ops
= {
1529 .get_drvinfo
= enc28j60_get_drvinfo
,
1530 .get_msglevel
= enc28j60_get_msglevel
,
1531 .set_msglevel
= enc28j60_set_msglevel
,
1532 .get_link_ksettings
= enc28j60_get_link_ksettings
,
1533 .set_link_ksettings
= enc28j60_set_link_ksettings
,
1536 static int enc28j60_chipset_init(struct net_device
*dev
)
1538 struct enc28j60_net
*priv
= netdev_priv(dev
);
1540 return enc28j60_hw_init(priv
);
1543 static const struct net_device_ops enc28j60_netdev_ops
= {
1544 .ndo_open
= enc28j60_net_open
,
1545 .ndo_stop
= enc28j60_net_close
,
1546 .ndo_start_xmit
= enc28j60_send_packet
,
1547 .ndo_set_rx_mode
= enc28j60_set_multicast_list
,
1548 .ndo_set_mac_address
= enc28j60_set_mac_address
,
1549 .ndo_tx_timeout
= enc28j60_tx_timeout
,
1550 .ndo_validate_addr
= eth_validate_addr
,
1553 static int enc28j60_probe(struct spi_device
*spi
)
1555 struct net_device
*dev
;
1556 struct enc28j60_net
*priv
;
1557 const void *macaddr
;
1560 if (netif_msg_drv(&debug
))
1561 dev_info(&spi
->dev
, DRV_NAME
" Ethernet driver %s loaded\n",
1564 dev
= alloc_etherdev(sizeof(struct enc28j60_net
));
1569 priv
= netdev_priv(dev
);
1571 priv
->netdev
= dev
; /* priv to netdev reference */
1572 priv
->spi
= spi
; /* priv to spi reference */
1573 priv
->msg_enable
= netif_msg_init(debug
.msg_enable
,
1574 ENC28J60_MSG_DEFAULT
);
1575 mutex_init(&priv
->lock
);
1576 INIT_WORK(&priv
->tx_work
, enc28j60_tx_work_handler
);
1577 INIT_WORK(&priv
->setrx_work
, enc28j60_setrx_work_handler
);
1578 INIT_WORK(&priv
->irq_work
, enc28j60_irq_work_handler
);
1579 INIT_WORK(&priv
->restart_work
, enc28j60_restart_work_handler
);
1580 spi_set_drvdata(spi
, priv
); /* spi to priv reference */
1581 SET_NETDEV_DEV(dev
, &spi
->dev
);
1583 if (!enc28j60_chipset_init(dev
)) {
1584 if (netif_msg_probe(priv
))
1585 dev_info(&spi
->dev
, DRV_NAME
" chip not found\n");
1590 macaddr
= of_get_mac_address(spi
->dev
.of_node
);
1592 ether_addr_copy(dev
->dev_addr
, macaddr
);
1594 eth_hw_addr_random(dev
);
1595 enc28j60_set_hw_macaddr(dev
);
1597 /* Board setup must set the relevant edge trigger type;
1598 * level triggers won't currently work.
1600 ret
= request_irq(spi
->irq
, enc28j60_irq
, 0, DRV_NAME
, priv
);
1602 if (netif_msg_probe(priv
))
1603 dev_err(&spi
->dev
, DRV_NAME
": request irq %d failed "
1604 "(ret = %d)\n", spi
->irq
, ret
);
1608 dev
->if_port
= IF_PORT_10BASET
;
1609 dev
->irq
= spi
->irq
;
1610 dev
->netdev_ops
= &enc28j60_netdev_ops
;
1611 dev
->watchdog_timeo
= TX_TIMEOUT
;
1612 dev
->ethtool_ops
= &enc28j60_ethtool_ops
;
1614 enc28j60_lowpower(priv
, true);
1616 ret
= register_netdev(dev
);
1618 if (netif_msg_probe(priv
))
1619 dev_err(&spi
->dev
, "register netdev " DRV_NAME
1620 " failed (ret = %d)\n", ret
);
1621 goto error_register
;
1623 dev_info(&dev
->dev
, DRV_NAME
" driver registered\n");
1628 free_irq(spi
->irq
, priv
);
1635 static int enc28j60_remove(struct spi_device
*spi
)
1637 struct enc28j60_net
*priv
= spi_get_drvdata(spi
);
1639 if (netif_msg_drv(priv
))
1640 printk(KERN_DEBUG DRV_NAME
": remove\n");
1642 unregister_netdev(priv
->netdev
);
1643 free_irq(spi
->irq
, priv
);
1644 free_netdev(priv
->netdev
);
1649 static const struct of_device_id enc28j60_dt_ids
[] = {
1650 { .compatible
= "microchip,enc28j60" },
1653 MODULE_DEVICE_TABLE(of
, enc28j60_dt_ids
);
1655 static struct spi_driver enc28j60_driver
= {
1658 .of_match_table
= enc28j60_dt_ids
,
1660 .probe
= enc28j60_probe
,
1661 .remove
= enc28j60_remove
,
1664 static int __init
enc28j60_init(void)
1666 msec20_to_jiffies
= msecs_to_jiffies(20);
1668 return spi_register_driver(&enc28j60_driver
);
1671 module_init(enc28j60_init
);
1673 static void __exit
enc28j60_exit(void)
1675 spi_unregister_driver(&enc28j60_driver
);
1678 module_exit(enc28j60_exit
);
1680 MODULE_DESCRIPTION(DRV_NAME
" ethernet driver");
1681 MODULE_AUTHOR("Claudio Lanconelli <lanconelli.claudio@eptar.com>");
1682 MODULE_LICENSE("GPL");
1683 module_param_named(debug
, debug
.msg_enable
, int, 0);
1684 MODULE_PARM_DESC(debug
, "Debug verbosity level (0=none, ..., ffff=all)");
1685 MODULE_ALIAS("spi:" DRV_NAME
);