treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / ethernet / microchip / encx24j600.c
blob39925e4bf2ecb4a8d8304c2b36b90a3fd80a2b60
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3 * Microchip ENCX24J600 ethernet driver
5 * Copyright (C) 2015 Gridpoint
6 * Author: Jon Ringle <jringle@gridpoint.com>
7 */
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/regmap.h>
18 #include <linux/skbuff.h>
19 #include <linux/spi/spi.h>
21 #include "encx24j600_hw.h"
23 #define DRV_NAME "encx24j600"
24 #define DRV_VERSION "1.0"
26 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
27 static int debug = -1;
28 module_param(debug, int, 0000);
29 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
31 /* SRAM memory layout:
33 * 0x0000-0x05ff TX buffers 1.5KB (1*1536) reside in the GP area in SRAM
34 * 0x0600-0x5fff RX buffers 22.5KB (15*1536) reside in the RX area in SRAM
36 #define ENC_TX_BUF_START 0x0000U
37 #define ENC_RX_BUF_START 0x0600U
38 #define ENC_RX_BUF_END 0x5fffU
39 #define ENC_SRAM_SIZE 0x6000U
41 enum {
42 RXFILTER_NORMAL,
43 RXFILTER_MULTI,
44 RXFILTER_PROMISC
47 struct encx24j600_priv {
48 struct net_device *ndev;
49 struct mutex lock; /* device access lock */
50 struct encx24j600_context ctx;
51 struct sk_buff *tx_skb;
52 struct task_struct *kworker_task;
53 struct kthread_worker kworker;
54 struct kthread_work tx_work;
55 struct kthread_work setrx_work;
56 u16 next_packet;
57 bool hw_enabled;
58 bool full_duplex;
59 bool autoneg;
60 u16 speed;
61 int rxfilter;
62 u32 msg_enable;
65 static void dump_packet(const char *msg, int len, const char *data)
67 pr_debug(DRV_NAME ": %s - packet len:%d\n", msg, len);
68 print_hex_dump_bytes("pk data: ", DUMP_PREFIX_OFFSET, data, len);
71 static void encx24j600_dump_rsv(struct encx24j600_priv *priv, const char *msg,
72 struct rsv *rsv)
74 struct net_device *dev = priv->ndev;
76 netdev_info(dev, "RX packet Len:%d\n", rsv->len);
77 netdev_dbg(dev, "%s - NextPk: 0x%04x\n", msg,
78 rsv->next_packet);
79 netdev_dbg(dev, "RxOK: %d, DribbleNibble: %d\n",
80 RSV_GETBIT(rsv->rxstat, RSV_RXOK),
81 RSV_GETBIT(rsv->rxstat, RSV_DRIBBLENIBBLE));
82 netdev_dbg(dev, "CRCErr:%d, LenChkErr: %d, LenOutOfRange: %d\n",
83 RSV_GETBIT(rsv->rxstat, RSV_CRCERROR),
84 RSV_GETBIT(rsv->rxstat, RSV_LENCHECKERR),
85 RSV_GETBIT(rsv->rxstat, RSV_LENOUTOFRANGE));
86 netdev_dbg(dev, "Multicast: %d, Broadcast: %d, LongDropEvent: %d, CarrierEvent: %d\n",
87 RSV_GETBIT(rsv->rxstat, RSV_RXMULTICAST),
88 RSV_GETBIT(rsv->rxstat, RSV_RXBROADCAST),
89 RSV_GETBIT(rsv->rxstat, RSV_RXLONGEVDROPEV),
90 RSV_GETBIT(rsv->rxstat, RSV_CARRIEREV));
91 netdev_dbg(dev, "ControlFrame: %d, PauseFrame: %d, UnknownOp: %d, VLanTagFrame: %d\n",
92 RSV_GETBIT(rsv->rxstat, RSV_RXCONTROLFRAME),
93 RSV_GETBIT(rsv->rxstat, RSV_RXPAUSEFRAME),
94 RSV_GETBIT(rsv->rxstat, RSV_RXUNKNOWNOPCODE),
95 RSV_GETBIT(rsv->rxstat, RSV_RXTYPEVLAN));
98 static u16 encx24j600_read_reg(struct encx24j600_priv *priv, u8 reg)
100 struct net_device *dev = priv->ndev;
101 unsigned int val = 0;
102 int ret = regmap_read(priv->ctx.regmap, reg, &val);
104 if (unlikely(ret))
105 netif_err(priv, drv, dev, "%s: error %d reading reg %02x\n",
106 __func__, ret, reg);
107 return val;
110 static void encx24j600_write_reg(struct encx24j600_priv *priv, u8 reg, u16 val)
112 struct net_device *dev = priv->ndev;
113 int ret = regmap_write(priv->ctx.regmap, reg, val);
115 if (unlikely(ret))
116 netif_err(priv, drv, dev, "%s: error %d writing reg %02x=%04x\n",
117 __func__, ret, reg, val);
120 static void encx24j600_update_reg(struct encx24j600_priv *priv, u8 reg,
121 u16 mask, u16 val)
123 struct net_device *dev = priv->ndev;
124 int ret = regmap_update_bits(priv->ctx.regmap, reg, mask, val);
126 if (unlikely(ret))
127 netif_err(priv, drv, dev, "%s: error %d updating reg %02x=%04x~%04x\n",
128 __func__, ret, reg, val, mask);
131 static u16 encx24j600_read_phy(struct encx24j600_priv *priv, u8 reg)
133 struct net_device *dev = priv->ndev;
134 unsigned int val = 0;
135 int ret = regmap_read(priv->ctx.phymap, reg, &val);
137 if (unlikely(ret))
138 netif_err(priv, drv, dev, "%s: error %d reading %02x\n",
139 __func__, ret, reg);
140 return val;
143 static void encx24j600_write_phy(struct encx24j600_priv *priv, u8 reg, u16 val)
145 struct net_device *dev = priv->ndev;
146 int ret = regmap_write(priv->ctx.phymap, reg, val);
148 if (unlikely(ret))
149 netif_err(priv, drv, dev, "%s: error %d writing reg %02x=%04x\n",
150 __func__, ret, reg, val);
153 static void encx24j600_clr_bits(struct encx24j600_priv *priv, u8 reg, u16 mask)
155 encx24j600_update_reg(priv, reg, mask, 0);
158 static void encx24j600_set_bits(struct encx24j600_priv *priv, u8 reg, u16 mask)
160 encx24j600_update_reg(priv, reg, mask, mask);
163 static void encx24j600_cmd(struct encx24j600_priv *priv, u8 cmd)
165 struct net_device *dev = priv->ndev;
166 int ret = regmap_write(priv->ctx.regmap, cmd, 0);
168 if (unlikely(ret))
169 netif_err(priv, drv, dev, "%s: error %d with cmd %02x\n",
170 __func__, ret, cmd);
173 static int encx24j600_raw_read(struct encx24j600_priv *priv, u8 reg, u8 *data,
174 size_t count)
176 int ret;
178 mutex_lock(&priv->ctx.mutex);
179 ret = regmap_encx24j600_spi_read(&priv->ctx, reg, data, count);
180 mutex_unlock(&priv->ctx.mutex);
182 return ret;
185 static int encx24j600_raw_write(struct encx24j600_priv *priv, u8 reg,
186 const u8 *data, size_t count)
188 int ret;
190 mutex_lock(&priv->ctx.mutex);
191 ret = regmap_encx24j600_spi_write(&priv->ctx, reg, data, count);
192 mutex_unlock(&priv->ctx.mutex);
194 return ret;
197 static void encx24j600_update_phcon1(struct encx24j600_priv *priv)
199 u16 phcon1 = encx24j600_read_phy(priv, PHCON1);
201 if (priv->autoneg == AUTONEG_ENABLE) {
202 phcon1 |= ANEN | RENEG;
203 } else {
204 phcon1 &= ~ANEN;
205 if (priv->speed == SPEED_100)
206 phcon1 |= SPD100;
207 else
208 phcon1 &= ~SPD100;
210 if (priv->full_duplex)
211 phcon1 |= PFULDPX;
212 else
213 phcon1 &= ~PFULDPX;
215 encx24j600_write_phy(priv, PHCON1, phcon1);
218 /* Waits for autonegotiation to complete. */
219 static int encx24j600_wait_for_autoneg(struct encx24j600_priv *priv)
221 struct net_device *dev = priv->ndev;
222 unsigned long timeout = jiffies + msecs_to_jiffies(2000);
223 u16 phstat1;
224 u16 estat;
225 int ret = 0;
227 phstat1 = encx24j600_read_phy(priv, PHSTAT1);
228 while ((phstat1 & ANDONE) == 0) {
229 if (time_after(jiffies, timeout)) {
230 u16 phstat3;
232 netif_notice(priv, drv, dev, "timeout waiting for autoneg done\n");
234 priv->autoneg = AUTONEG_DISABLE;
235 phstat3 = encx24j600_read_phy(priv, PHSTAT3);
236 priv->speed = (phstat3 & PHY3SPD100)
237 ? SPEED_100 : SPEED_10;
238 priv->full_duplex = (phstat3 & PHY3DPX) ? 1 : 0;
239 encx24j600_update_phcon1(priv);
240 netif_notice(priv, drv, dev, "Using parallel detection: %s/%s",
241 priv->speed == SPEED_100 ? "100" : "10",
242 priv->full_duplex ? "Full" : "Half");
244 return -ETIMEDOUT;
246 cpu_relax();
247 phstat1 = encx24j600_read_phy(priv, PHSTAT1);
250 estat = encx24j600_read_reg(priv, ESTAT);
251 if (estat & PHYDPX) {
252 encx24j600_set_bits(priv, MACON2, FULDPX);
253 encx24j600_write_reg(priv, MABBIPG, 0x15);
254 } else {
255 encx24j600_clr_bits(priv, MACON2, FULDPX);
256 encx24j600_write_reg(priv, MABBIPG, 0x12);
257 /* Max retransmittions attempt */
258 encx24j600_write_reg(priv, MACLCON, 0x370f);
261 return ret;
264 /* Access the PHY to determine link status */
265 static void encx24j600_check_link_status(struct encx24j600_priv *priv)
267 struct net_device *dev = priv->ndev;
268 u16 estat;
270 estat = encx24j600_read_reg(priv, ESTAT);
272 if (estat & PHYLNK) {
273 if (priv->autoneg == AUTONEG_ENABLE)
274 encx24j600_wait_for_autoneg(priv);
276 netif_carrier_on(dev);
277 netif_info(priv, ifup, dev, "link up\n");
278 } else {
279 netif_info(priv, ifdown, dev, "link down\n");
281 /* Re-enable autoneg since we won't know what we might be
282 * connected to when the link is brought back up again.
284 priv->autoneg = AUTONEG_ENABLE;
285 priv->full_duplex = true;
286 priv->speed = SPEED_100;
287 netif_carrier_off(dev);
291 static void encx24j600_int_link_handler(struct encx24j600_priv *priv)
293 struct net_device *dev = priv->ndev;
295 netif_dbg(priv, intr, dev, "%s", __func__);
296 encx24j600_check_link_status(priv);
297 encx24j600_clr_bits(priv, EIR, LINKIF);
300 static void encx24j600_tx_complete(struct encx24j600_priv *priv, bool err)
302 struct net_device *dev = priv->ndev;
304 if (!priv->tx_skb) {
305 BUG();
306 return;
309 mutex_lock(&priv->lock);
311 if (err)
312 dev->stats.tx_errors++;
313 else
314 dev->stats.tx_packets++;
316 dev->stats.tx_bytes += priv->tx_skb->len;
318 encx24j600_clr_bits(priv, EIR, TXIF | TXABTIF);
320 netif_dbg(priv, tx_done, dev, "TX Done%s\n", err ? ": Err" : "");
322 dev_kfree_skb(priv->tx_skb);
323 priv->tx_skb = NULL;
325 netif_wake_queue(dev);
327 mutex_unlock(&priv->lock);
330 static int encx24j600_receive_packet(struct encx24j600_priv *priv,
331 struct rsv *rsv)
333 struct net_device *dev = priv->ndev;
334 struct sk_buff *skb = netdev_alloc_skb(dev, rsv->len + NET_IP_ALIGN);
336 if (!skb) {
337 pr_err_ratelimited("RX: OOM: packet dropped\n");
338 dev->stats.rx_dropped++;
339 return -ENOMEM;
341 skb_reserve(skb, NET_IP_ALIGN);
342 encx24j600_raw_read(priv, RRXDATA, skb_put(skb, rsv->len), rsv->len);
344 if (netif_msg_pktdata(priv))
345 dump_packet("RX", skb->len, skb->data);
347 skb->dev = dev;
348 skb->protocol = eth_type_trans(skb, dev);
349 skb->ip_summed = CHECKSUM_COMPLETE;
351 /* Maintain stats */
352 dev->stats.rx_packets++;
353 dev->stats.rx_bytes += rsv->len;
355 netif_rx(skb);
357 return 0;
360 static void encx24j600_rx_packets(struct encx24j600_priv *priv, u8 packet_count)
362 struct net_device *dev = priv->ndev;
364 while (packet_count--) {
365 struct rsv rsv;
366 u16 newrxtail;
368 encx24j600_write_reg(priv, ERXRDPT, priv->next_packet);
369 encx24j600_raw_read(priv, RRXDATA, (u8 *)&rsv, sizeof(rsv));
371 if (netif_msg_rx_status(priv))
372 encx24j600_dump_rsv(priv, __func__, &rsv);
374 if (!RSV_GETBIT(rsv.rxstat, RSV_RXOK) ||
375 (rsv.len > MAX_FRAMELEN)) {
376 netif_err(priv, rx_err, dev, "RX Error %04x\n",
377 rsv.rxstat);
378 dev->stats.rx_errors++;
380 if (RSV_GETBIT(rsv.rxstat, RSV_CRCERROR))
381 dev->stats.rx_crc_errors++;
382 if (RSV_GETBIT(rsv.rxstat, RSV_LENCHECKERR))
383 dev->stats.rx_frame_errors++;
384 if (rsv.len > MAX_FRAMELEN)
385 dev->stats.rx_over_errors++;
386 } else {
387 encx24j600_receive_packet(priv, &rsv);
390 priv->next_packet = rsv.next_packet;
392 newrxtail = priv->next_packet - 2;
393 if (newrxtail == ENC_RX_BUF_START)
394 newrxtail = SRAM_SIZE - 2;
396 encx24j600_cmd(priv, SETPKTDEC);
397 encx24j600_write_reg(priv, ERXTAIL, newrxtail);
401 static irqreturn_t encx24j600_isr(int irq, void *dev_id)
403 struct encx24j600_priv *priv = dev_id;
404 struct net_device *dev = priv->ndev;
405 int eir;
407 /* Clear interrupts */
408 encx24j600_cmd(priv, CLREIE);
410 eir = encx24j600_read_reg(priv, EIR);
412 if (eir & LINKIF)
413 encx24j600_int_link_handler(priv);
415 if (eir & TXIF)
416 encx24j600_tx_complete(priv, false);
418 if (eir & TXABTIF)
419 encx24j600_tx_complete(priv, true);
421 if (eir & RXABTIF) {
422 if (eir & PCFULIF) {
423 /* Packet counter is full */
424 netif_err(priv, rx_err, dev, "Packet counter full\n");
426 dev->stats.rx_dropped++;
427 encx24j600_clr_bits(priv, EIR, RXABTIF);
430 if (eir & PKTIF) {
431 u8 packet_count;
433 mutex_lock(&priv->lock);
435 packet_count = encx24j600_read_reg(priv, ESTAT) & 0xff;
436 while (packet_count) {
437 encx24j600_rx_packets(priv, packet_count);
438 packet_count = encx24j600_read_reg(priv, ESTAT) & 0xff;
441 mutex_unlock(&priv->lock);
444 /* Enable interrupts */
445 encx24j600_cmd(priv, SETEIE);
447 return IRQ_HANDLED;
450 static int encx24j600_soft_reset(struct encx24j600_priv *priv)
452 int ret = 0;
453 int timeout;
454 u16 eudast;
456 /* Write and verify a test value to EUDAST */
457 regcache_cache_bypass(priv->ctx.regmap, true);
458 timeout = 10;
459 do {
460 encx24j600_write_reg(priv, EUDAST, EUDAST_TEST_VAL);
461 eudast = encx24j600_read_reg(priv, EUDAST);
462 usleep_range(25, 100);
463 } while ((eudast != EUDAST_TEST_VAL) && --timeout);
464 regcache_cache_bypass(priv->ctx.regmap, false);
466 if (timeout == 0) {
467 ret = -ETIMEDOUT;
468 goto err_out;
471 /* Wait for CLKRDY to become set */
472 timeout = 10;
473 while (!(encx24j600_read_reg(priv, ESTAT) & CLKRDY) && --timeout)
474 usleep_range(25, 100);
476 if (timeout == 0) {
477 ret = -ETIMEDOUT;
478 goto err_out;
481 /* Issue a System Reset command */
482 encx24j600_cmd(priv, SETETHRST);
483 usleep_range(25, 100);
485 /* Confirm that EUDAST has 0000h after system reset */
486 if (encx24j600_read_reg(priv, EUDAST) != 0) {
487 ret = -EINVAL;
488 goto err_out;
491 /* Wait for PHY register and status bits to become available */
492 usleep_range(256, 1000);
494 err_out:
495 return ret;
498 static int encx24j600_hw_reset(struct encx24j600_priv *priv)
500 int ret;
502 mutex_lock(&priv->lock);
503 ret = encx24j600_soft_reset(priv);
504 mutex_unlock(&priv->lock);
506 return ret;
509 static void encx24j600_reset_hw_tx(struct encx24j600_priv *priv)
511 encx24j600_set_bits(priv, ECON2, TXRST);
512 encx24j600_clr_bits(priv, ECON2, TXRST);
515 static void encx24j600_hw_init_tx(struct encx24j600_priv *priv)
517 /* Reset TX */
518 encx24j600_reset_hw_tx(priv);
520 /* Clear the TXIF flag if were previously set */
521 encx24j600_clr_bits(priv, EIR, TXIF | TXABTIF);
523 /* Write the Tx Buffer pointer */
524 encx24j600_write_reg(priv, EGPWRPT, ENC_TX_BUF_START);
527 static void encx24j600_hw_init_rx(struct encx24j600_priv *priv)
529 encx24j600_cmd(priv, DISABLERX);
531 /* Set up RX packet start address in the SRAM */
532 encx24j600_write_reg(priv, ERXST, ENC_RX_BUF_START);
534 /* Preload the RX Data pointer to the beginning of the RX area */
535 encx24j600_write_reg(priv, ERXRDPT, ENC_RX_BUF_START);
537 priv->next_packet = ENC_RX_BUF_START;
539 /* Set up RX end address in the SRAM */
540 encx24j600_write_reg(priv, ERXTAIL, ENC_SRAM_SIZE - 2);
542 /* Reset the user data pointers */
543 encx24j600_write_reg(priv, EUDAST, ENC_SRAM_SIZE);
544 encx24j600_write_reg(priv, EUDAND, ENC_SRAM_SIZE + 1);
546 /* Set Max Frame length */
547 encx24j600_write_reg(priv, MAMXFL, MAX_FRAMELEN);
550 static void encx24j600_dump_config(struct encx24j600_priv *priv,
551 const char *msg)
553 pr_info(DRV_NAME ": %s\n", msg);
555 /* CHIP configuration */
556 pr_info(DRV_NAME " ECON1: %04X\n", encx24j600_read_reg(priv, ECON1));
557 pr_info(DRV_NAME " ECON2: %04X\n", encx24j600_read_reg(priv, ECON2));
558 pr_info(DRV_NAME " ERXFCON: %04X\n", encx24j600_read_reg(priv,
559 ERXFCON));
560 pr_info(DRV_NAME " ESTAT: %04X\n", encx24j600_read_reg(priv, ESTAT));
561 pr_info(DRV_NAME " EIR: %04X\n", encx24j600_read_reg(priv, EIR));
562 pr_info(DRV_NAME " EIDLED: %04X\n", encx24j600_read_reg(priv, EIDLED));
564 /* MAC layer configuration */
565 pr_info(DRV_NAME " MACON1: %04X\n", encx24j600_read_reg(priv, MACON1));
566 pr_info(DRV_NAME " MACON2: %04X\n", encx24j600_read_reg(priv, MACON2));
567 pr_info(DRV_NAME " MAIPG: %04X\n", encx24j600_read_reg(priv, MAIPG));
568 pr_info(DRV_NAME " MACLCON: %04X\n", encx24j600_read_reg(priv,
569 MACLCON));
570 pr_info(DRV_NAME " MABBIPG: %04X\n", encx24j600_read_reg(priv,
571 MABBIPG));
573 /* PHY configuation */
574 pr_info(DRV_NAME " PHCON1: %04X\n", encx24j600_read_phy(priv, PHCON1));
575 pr_info(DRV_NAME " PHCON2: %04X\n", encx24j600_read_phy(priv, PHCON2));
576 pr_info(DRV_NAME " PHANA: %04X\n", encx24j600_read_phy(priv, PHANA));
577 pr_info(DRV_NAME " PHANLPA: %04X\n", encx24j600_read_phy(priv,
578 PHANLPA));
579 pr_info(DRV_NAME " PHANE: %04X\n", encx24j600_read_phy(priv, PHANE));
580 pr_info(DRV_NAME " PHSTAT1: %04X\n", encx24j600_read_phy(priv,
581 PHSTAT1));
582 pr_info(DRV_NAME " PHSTAT2: %04X\n", encx24j600_read_phy(priv,
583 PHSTAT2));
584 pr_info(DRV_NAME " PHSTAT3: %04X\n", encx24j600_read_phy(priv,
585 PHSTAT3));
588 static void encx24j600_set_rxfilter_mode(struct encx24j600_priv *priv)
590 switch (priv->rxfilter) {
591 case RXFILTER_PROMISC:
592 encx24j600_set_bits(priv, MACON1, PASSALL);
593 encx24j600_write_reg(priv, ERXFCON, UCEN | MCEN | NOTMEEN);
594 break;
595 case RXFILTER_MULTI:
596 encx24j600_clr_bits(priv, MACON1, PASSALL);
597 encx24j600_write_reg(priv, ERXFCON, UCEN | CRCEN | BCEN | MCEN);
598 break;
599 case RXFILTER_NORMAL:
600 default:
601 encx24j600_clr_bits(priv, MACON1, PASSALL);
602 encx24j600_write_reg(priv, ERXFCON, UCEN | CRCEN | BCEN);
603 break;
607 static int encx24j600_hw_init(struct encx24j600_priv *priv)
609 int ret = 0;
610 u16 macon2;
612 priv->hw_enabled = false;
614 /* PHY Leds: link status,
615 * LEDA: Link State + collision events
616 * LEDB: Link State + transmit/receive events
618 encx24j600_update_reg(priv, EIDLED, 0xff00, 0xcb00);
620 /* Loopback disabled */
621 encx24j600_write_reg(priv, MACON1, 0x9);
623 /* interpacket gap value */
624 encx24j600_write_reg(priv, MAIPG, 0x0c12);
626 /* Write the auto negotiation pattern */
627 encx24j600_write_phy(priv, PHANA, PHANA_DEFAULT);
629 encx24j600_update_phcon1(priv);
630 encx24j600_check_link_status(priv);
632 macon2 = MACON2_RSV1 | TXCRCEN | PADCFG0 | PADCFG2 | MACON2_DEFER;
633 if ((priv->autoneg == AUTONEG_DISABLE) && priv->full_duplex)
634 macon2 |= FULDPX;
636 encx24j600_set_bits(priv, MACON2, macon2);
638 priv->rxfilter = RXFILTER_NORMAL;
639 encx24j600_set_rxfilter_mode(priv);
641 /* Program the Maximum frame length */
642 encx24j600_write_reg(priv, MAMXFL, MAX_FRAMELEN);
644 /* Init Tx pointers */
645 encx24j600_hw_init_tx(priv);
647 /* Init Rx pointers */
648 encx24j600_hw_init_rx(priv);
650 if (netif_msg_hw(priv))
651 encx24j600_dump_config(priv, "Hw is initialized");
653 return ret;
656 static void encx24j600_hw_enable(struct encx24j600_priv *priv)
658 /* Clear the interrupt flags in case was set */
659 encx24j600_clr_bits(priv, EIR, (PCFULIF | RXABTIF | TXABTIF | TXIF |
660 PKTIF | LINKIF));
662 /* Enable the interrupts */
663 encx24j600_write_reg(priv, EIE, (PCFULIE | RXABTIE | TXABTIE | TXIE |
664 PKTIE | LINKIE | INTIE));
666 /* Enable RX */
667 encx24j600_cmd(priv, ENABLERX);
669 priv->hw_enabled = true;
672 static void encx24j600_hw_disable(struct encx24j600_priv *priv)
674 /* Disable all interrupts */
675 encx24j600_write_reg(priv, EIE, 0);
677 /* Disable RX */
678 encx24j600_cmd(priv, DISABLERX);
680 priv->hw_enabled = false;
683 static int encx24j600_setlink(struct net_device *dev, u8 autoneg, u16 speed,
684 u8 duplex)
686 struct encx24j600_priv *priv = netdev_priv(dev);
687 int ret = 0;
689 if (!priv->hw_enabled) {
690 /* link is in low power mode now; duplex setting
691 * will take effect on next encx24j600_hw_init()
693 if (speed == SPEED_10 || speed == SPEED_100) {
694 priv->autoneg = (autoneg == AUTONEG_ENABLE);
695 priv->full_duplex = (duplex == DUPLEX_FULL);
696 priv->speed = (speed == SPEED_100);
697 } else {
698 netif_warn(priv, link, dev, "unsupported link speed setting\n");
699 /*speeds other than SPEED_10 and SPEED_100 */
700 /*are not supported by chip */
701 ret = -EOPNOTSUPP;
703 } else {
704 netif_warn(priv, link, dev, "Warning: hw must be disabled to set link mode\n");
705 ret = -EBUSY;
707 return ret;
710 static void encx24j600_hw_get_macaddr(struct encx24j600_priv *priv,
711 unsigned char *ethaddr)
713 unsigned short val;
715 val = encx24j600_read_reg(priv, MAADR1);
717 ethaddr[0] = val & 0x00ff;
718 ethaddr[1] = (val & 0xff00) >> 8;
720 val = encx24j600_read_reg(priv, MAADR2);
722 ethaddr[2] = val & 0x00ffU;
723 ethaddr[3] = (val & 0xff00U) >> 8;
725 val = encx24j600_read_reg(priv, MAADR3);
727 ethaddr[4] = val & 0x00ffU;
728 ethaddr[5] = (val & 0xff00U) >> 8;
731 /* Program the hardware MAC address from dev->dev_addr.*/
732 static int encx24j600_set_hw_macaddr(struct net_device *dev)
734 struct encx24j600_priv *priv = netdev_priv(dev);
736 if (priv->hw_enabled) {
737 netif_info(priv, drv, dev, "Hardware must be disabled to set Mac address\n");
738 return -EBUSY;
741 mutex_lock(&priv->lock);
743 netif_info(priv, drv, dev, "%s: Setting MAC address to %pM\n",
744 dev->name, dev->dev_addr);
746 encx24j600_write_reg(priv, MAADR3, (dev->dev_addr[4] |
747 dev->dev_addr[5] << 8));
748 encx24j600_write_reg(priv, MAADR2, (dev->dev_addr[2] |
749 dev->dev_addr[3] << 8));
750 encx24j600_write_reg(priv, MAADR1, (dev->dev_addr[0] |
751 dev->dev_addr[1] << 8));
753 mutex_unlock(&priv->lock);
755 return 0;
758 /* Store the new hardware address in dev->dev_addr, and update the MAC.*/
759 static int encx24j600_set_mac_address(struct net_device *dev, void *addr)
761 struct sockaddr *address = addr;
763 if (netif_running(dev))
764 return -EBUSY;
765 if (!is_valid_ether_addr(address->sa_data))
766 return -EADDRNOTAVAIL;
768 memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
769 return encx24j600_set_hw_macaddr(dev);
772 static int encx24j600_open(struct net_device *dev)
774 struct encx24j600_priv *priv = netdev_priv(dev);
776 int ret = request_threaded_irq(priv->ctx.spi->irq, NULL, encx24j600_isr,
777 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
778 DRV_NAME, priv);
779 if (unlikely(ret < 0)) {
780 netdev_err(dev, "request irq %d failed (ret = %d)\n",
781 priv->ctx.spi->irq, ret);
782 return ret;
785 encx24j600_hw_disable(priv);
786 encx24j600_hw_init(priv);
787 encx24j600_hw_enable(priv);
788 netif_start_queue(dev);
790 return 0;
793 static int encx24j600_stop(struct net_device *dev)
795 struct encx24j600_priv *priv = netdev_priv(dev);
797 netif_stop_queue(dev);
798 free_irq(priv->ctx.spi->irq, priv);
799 return 0;
802 static void encx24j600_setrx_proc(struct kthread_work *ws)
804 struct encx24j600_priv *priv =
805 container_of(ws, struct encx24j600_priv, setrx_work);
807 mutex_lock(&priv->lock);
808 encx24j600_set_rxfilter_mode(priv);
809 mutex_unlock(&priv->lock);
812 static void encx24j600_set_multicast_list(struct net_device *dev)
814 struct encx24j600_priv *priv = netdev_priv(dev);
815 int oldfilter = priv->rxfilter;
817 if (dev->flags & IFF_PROMISC) {
818 netif_dbg(priv, link, dev, "promiscuous mode\n");
819 priv->rxfilter = RXFILTER_PROMISC;
820 } else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev)) {
821 netif_dbg(priv, link, dev, "%smulticast mode\n",
822 (dev->flags & IFF_ALLMULTI) ? "all-" : "");
823 priv->rxfilter = RXFILTER_MULTI;
824 } else {
825 netif_dbg(priv, link, dev, "normal mode\n");
826 priv->rxfilter = RXFILTER_NORMAL;
829 if (oldfilter != priv->rxfilter)
830 kthread_queue_work(&priv->kworker, &priv->setrx_work);
833 static void encx24j600_hw_tx(struct encx24j600_priv *priv)
835 struct net_device *dev = priv->ndev;
837 netif_info(priv, tx_queued, dev, "TX Packet Len:%d\n",
838 priv->tx_skb->len);
840 if (netif_msg_pktdata(priv))
841 dump_packet("TX", priv->tx_skb->len, priv->tx_skb->data);
843 if (encx24j600_read_reg(priv, EIR) & TXABTIF)
844 /* Last transmition aborted due to error. Reset TX interface */
845 encx24j600_reset_hw_tx(priv);
847 /* Clear the TXIF flag if were previously set */
848 encx24j600_clr_bits(priv, EIR, TXIF);
850 /* Set the data pointer to the TX buffer address in the SRAM */
851 encx24j600_write_reg(priv, EGPWRPT, ENC_TX_BUF_START);
853 /* Copy the packet into the SRAM */
854 encx24j600_raw_write(priv, WGPDATA, (u8 *)priv->tx_skb->data,
855 priv->tx_skb->len);
857 /* Program the Tx buffer start pointer */
858 encx24j600_write_reg(priv, ETXST, ENC_TX_BUF_START);
860 /* Program the packet length */
861 encx24j600_write_reg(priv, ETXLEN, priv->tx_skb->len);
863 /* Start the transmission */
864 encx24j600_cmd(priv, SETTXRTS);
867 static void encx24j600_tx_proc(struct kthread_work *ws)
869 struct encx24j600_priv *priv =
870 container_of(ws, struct encx24j600_priv, tx_work);
872 mutex_lock(&priv->lock);
873 encx24j600_hw_tx(priv);
874 mutex_unlock(&priv->lock);
877 static netdev_tx_t encx24j600_tx(struct sk_buff *skb, struct net_device *dev)
879 struct encx24j600_priv *priv = netdev_priv(dev);
881 netif_stop_queue(dev);
883 /* save the timestamp */
884 netif_trans_update(dev);
886 /* Remember the skb for deferred processing */
887 priv->tx_skb = skb;
889 kthread_queue_work(&priv->kworker, &priv->tx_work);
891 return NETDEV_TX_OK;
894 /* Deal with a transmit timeout */
895 static void encx24j600_tx_timeout(struct net_device *dev, unsigned int txqueue)
897 struct encx24j600_priv *priv = netdev_priv(dev);
899 netif_err(priv, tx_err, dev, "TX timeout at %ld, latency %ld\n",
900 jiffies, jiffies - dev_trans_start(dev));
902 dev->stats.tx_errors++;
903 netif_wake_queue(dev);
906 static int encx24j600_get_regs_len(struct net_device *dev)
908 return SFR_REG_COUNT;
911 static void encx24j600_get_regs(struct net_device *dev,
912 struct ethtool_regs *regs, void *p)
914 struct encx24j600_priv *priv = netdev_priv(dev);
915 u16 *buff = p;
916 u8 reg;
918 regs->version = 1;
919 mutex_lock(&priv->lock);
920 for (reg = 0; reg < SFR_REG_COUNT; reg += 2) {
921 unsigned int val = 0;
922 /* ignore errors for unreadable registers */
923 regmap_read(priv->ctx.regmap, reg, &val);
924 buff[reg] = val & 0xffff;
926 mutex_unlock(&priv->lock);
929 static void encx24j600_get_drvinfo(struct net_device *dev,
930 struct ethtool_drvinfo *info)
932 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
933 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
934 strlcpy(info->bus_info, dev_name(dev->dev.parent),
935 sizeof(info->bus_info));
938 static int encx24j600_get_link_ksettings(struct net_device *dev,
939 struct ethtool_link_ksettings *cmd)
941 struct encx24j600_priv *priv = netdev_priv(dev);
942 u32 supported;
944 supported = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
945 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
946 SUPPORTED_Autoneg | SUPPORTED_TP;
948 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
949 supported);
951 cmd->base.speed = priv->speed;
952 cmd->base.duplex = priv->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
953 cmd->base.port = PORT_TP;
954 cmd->base.autoneg = priv->autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
956 return 0;
959 static int
960 encx24j600_set_link_ksettings(struct net_device *dev,
961 const struct ethtool_link_ksettings *cmd)
963 return encx24j600_setlink(dev, cmd->base.autoneg,
964 cmd->base.speed, cmd->base.duplex);
967 static u32 encx24j600_get_msglevel(struct net_device *dev)
969 struct encx24j600_priv *priv = netdev_priv(dev);
971 return priv->msg_enable;
974 static void encx24j600_set_msglevel(struct net_device *dev, u32 val)
976 struct encx24j600_priv *priv = netdev_priv(dev);
978 priv->msg_enable = val;
981 static const struct ethtool_ops encx24j600_ethtool_ops = {
982 .get_drvinfo = encx24j600_get_drvinfo,
983 .get_msglevel = encx24j600_get_msglevel,
984 .set_msglevel = encx24j600_set_msglevel,
985 .get_regs_len = encx24j600_get_regs_len,
986 .get_regs = encx24j600_get_regs,
987 .get_link_ksettings = encx24j600_get_link_ksettings,
988 .set_link_ksettings = encx24j600_set_link_ksettings,
991 static const struct net_device_ops encx24j600_netdev_ops = {
992 .ndo_open = encx24j600_open,
993 .ndo_stop = encx24j600_stop,
994 .ndo_start_xmit = encx24j600_tx,
995 .ndo_set_rx_mode = encx24j600_set_multicast_list,
996 .ndo_set_mac_address = encx24j600_set_mac_address,
997 .ndo_tx_timeout = encx24j600_tx_timeout,
998 .ndo_validate_addr = eth_validate_addr,
1001 static int encx24j600_spi_probe(struct spi_device *spi)
1003 int ret;
1005 struct net_device *ndev;
1006 struct encx24j600_priv *priv;
1007 u16 eidled;
1009 ndev = alloc_etherdev(sizeof(struct encx24j600_priv));
1011 if (!ndev) {
1012 ret = -ENOMEM;
1013 goto error_out;
1016 priv = netdev_priv(ndev);
1017 spi_set_drvdata(spi, priv);
1018 dev_set_drvdata(&spi->dev, priv);
1019 SET_NETDEV_DEV(ndev, &spi->dev);
1021 priv->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1022 priv->ndev = ndev;
1024 /* Default configuration PHY configuration */
1025 priv->full_duplex = true;
1026 priv->autoneg = AUTONEG_ENABLE;
1027 priv->speed = SPEED_100;
1029 priv->ctx.spi = spi;
1030 devm_regmap_init_encx24j600(&spi->dev, &priv->ctx);
1031 ndev->irq = spi->irq;
1032 ndev->netdev_ops = &encx24j600_netdev_ops;
1034 mutex_init(&priv->lock);
1036 /* Reset device and check if it is connected */
1037 if (encx24j600_hw_reset(priv)) {
1038 netif_err(priv, probe, ndev,
1039 DRV_NAME ": Chip is not detected\n");
1040 ret = -EIO;
1041 goto out_free;
1044 /* Initialize the device HW to the consistent state */
1045 if (encx24j600_hw_init(priv)) {
1046 netif_err(priv, probe, ndev,
1047 DRV_NAME ": HW initialization error\n");
1048 ret = -EIO;
1049 goto out_free;
1052 kthread_init_worker(&priv->kworker);
1053 kthread_init_work(&priv->tx_work, encx24j600_tx_proc);
1054 kthread_init_work(&priv->setrx_work, encx24j600_setrx_proc);
1056 priv->kworker_task = kthread_run(kthread_worker_fn, &priv->kworker,
1057 "encx24j600");
1059 if (IS_ERR(priv->kworker_task)) {
1060 ret = PTR_ERR(priv->kworker_task);
1061 goto out_free;
1064 /* Get the MAC address from the chip */
1065 encx24j600_hw_get_macaddr(priv, ndev->dev_addr);
1067 ndev->ethtool_ops = &encx24j600_ethtool_ops;
1069 ret = register_netdev(ndev);
1070 if (unlikely(ret)) {
1071 netif_err(priv, probe, ndev, "Error %d initializing card encx24j600 card\n",
1072 ret);
1073 goto out_free;
1076 eidled = encx24j600_read_reg(priv, EIDLED);
1077 if (((eidled & DEVID_MASK) >> DEVID_SHIFT) != ENCX24J600_DEV_ID) {
1078 ret = -EINVAL;
1079 goto out_unregister;
1082 netif_info(priv, probe, ndev, "Silicon rev ID: 0x%02x\n",
1083 (eidled & REVID_MASK) >> REVID_SHIFT);
1085 netif_info(priv, drv, priv->ndev, "MAC address %pM\n", ndev->dev_addr);
1087 return ret;
1089 out_unregister:
1090 unregister_netdev(priv->ndev);
1091 out_free:
1092 free_netdev(ndev);
1094 error_out:
1095 return ret;
1098 static int encx24j600_spi_remove(struct spi_device *spi)
1100 struct encx24j600_priv *priv = dev_get_drvdata(&spi->dev);
1102 unregister_netdev(priv->ndev);
1104 free_netdev(priv->ndev);
1106 return 0;
1109 static const struct spi_device_id encx24j600_spi_id_table[] = {
1110 { .name = "encx24j600" },
1111 { /* sentinel */ }
1113 MODULE_DEVICE_TABLE(spi, encx24j600_spi_id_table);
1115 static struct spi_driver encx24j600_spi_net_driver = {
1116 .driver = {
1117 .name = DRV_NAME,
1118 .owner = THIS_MODULE,
1119 .bus = &spi_bus_type,
1121 .probe = encx24j600_spi_probe,
1122 .remove = encx24j600_spi_remove,
1123 .id_table = encx24j600_spi_id_table,
1126 static int __init encx24j600_init(void)
1128 return spi_register_driver(&encx24j600_spi_net_driver);
1130 module_init(encx24j600_init);
1132 static void encx24j600_exit(void)
1134 spi_unregister_driver(&encx24j600_spi_net_driver);
1136 module_exit(encx24j600_exit);
1138 MODULE_DESCRIPTION(DRV_NAME " ethernet driver");
1139 MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>");
1140 MODULE_LICENSE("GPL");
1141 MODULE_ALIAS("spi:" DRV_NAME);