Skip to content

Commit 7dac6f8

Browse files
David BrownellJeff Garzik
authored andcommitted
net/enc28j60: low power mode
Keep enc28j60 chips in low-power mode when they're not in use. At typically 120 mA, these chips run hot even when idle; this low power mode cuts that power usage by a factor of around 100. This version provides a generic routine to poll a register until its masked value equals some value ... e.g. bit set or cleared. It's basically what the previous wait_phy_ready() did, but this version is generalized to support the handshaking needed to enter and exit low power mode. Signed-off-by: David Brownell <[email protected]> Signed-off-by: Claudio Lanconelli <[email protected]> Signed-off-by: Jeff Garzik <[email protected]>
1 parent 6fd6588 commit 7dac6f8

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

drivers/net/enc28j60.c

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -400,26 +400,31 @@ enc28j60_packet_write(struct enc28j60_net *priv, int len, const u8 *data)
400400
mutex_unlock(&priv->lock);
401401
}
402402

403-
/*
404-
* Wait until the PHY operation is complete.
405-
*/
406-
static int wait_phy_ready(struct enc28j60_net *priv)
403+
static unsigned long msec20_to_jiffies;
404+
405+
static int poll_ready(struct enc28j60_net *priv, u8 reg, u8 mask, u8 val)
407406
{
408-
unsigned long timeout = jiffies + 20 * HZ / 1000;
409-
int ret = 1;
407+
unsigned long timeout = jiffies + msec20_to_jiffies;
410408

411409
/* 20 msec timeout read */
412-
while (nolock_regb_read(priv, MISTAT) & MISTAT_BUSY) {
410+
while ((nolock_regb_read(priv, reg) & mask) != val) {
413411
if (time_after(jiffies, timeout)) {
414412
if (netif_msg_drv(priv))
415-
printk(KERN_DEBUG DRV_NAME
416-
": PHY ready timeout!\n");
417-
ret = 0;
418-
break;
413+
dev_dbg(&priv->spi->dev,
414+
"reg %02x ready timeout!\n", reg);
415+
return -ETIMEDOUT;
419416
}
420417
cpu_relax();
421418
}
422-
return ret;
419+
return 0;
420+
}
421+
422+
/*
423+
* Wait until the PHY operation is complete.
424+
*/
425+
static int wait_phy_ready(struct enc28j60_net *priv)
426+
{
427+
return poll_ready(priv, MISTAT, MISTAT_BUSY, 0) ? 0 : 1;
423428
}
424429

425430
/*
@@ -594,6 +599,32 @@ static void nolock_txfifo_init(struct enc28j60_net *priv, u16 start, u16 end)
594599
nolock_regw_write(priv, ETXNDL, end);
595600
}
596601

602+
/*
603+
* Low power mode shrinks power consumption about 100x, so we'd like
604+
* the chip to be in that mode whenever it's inactive. (However, we
605+
* can't stay in lowpower mode during suspend with WOL active.)
606+
*/
607+
static void enc28j60_lowpower(struct enc28j60_net *priv, bool is_low)
608+
{
609+
if (netif_msg_drv(priv))
610+
dev_dbg(&priv->spi->dev, "%s power...\n",
611+
is_low ? "low" : "high");
612+
613+
mutex_lock(&priv->lock);
614+
if (is_low) {
615+
nolock_reg_bfclr(priv, ECON1, ECON1_RXEN);
616+
poll_ready(priv, ESTAT, ESTAT_RXBUSY, 0);
617+
poll_ready(priv, ECON1, ECON1_TXRTS, 0);
618+
/* ECON2_VRPS was set during initialization */
619+
nolock_reg_bfset(priv, ECON2, ECON2_PWRSV);
620+
} else {
621+
nolock_reg_bfclr(priv, ECON2, ECON2_PWRSV);
622+
poll_ready(priv, ESTAT, ESTAT_CLKRDY, ESTAT_CLKRDY);
623+
/* caller sets ECON1_RXEN */
624+
}
625+
mutex_unlock(&priv->lock);
626+
}
627+
597628
static int enc28j60_hw_init(struct enc28j60_net *priv)
598629
{
599630
u8 reg;
@@ -612,8 +643,8 @@ static int enc28j60_hw_init(struct enc28j60_net *priv)
612643
priv->tx_retry_count = 0;
613644
priv->max_pk_counter = 0;
614645
priv->rxfilter = RXFILTER_NORMAL;
615-
/* enable address auto increment */
616-
nolock_regb_write(priv, ECON2, ECON2_AUTOINC);
646+
/* enable address auto increment and voltage regulator powersave */
647+
nolock_regb_write(priv, ECON2, ECON2_AUTOINC | ECON2_VRPS);
617648

618649
nolock_rxfifo_init(priv, RXSTART_INIT, RXEND_INIT);
619650
nolock_txfifo_init(priv, TXSTART_INIT, TXEND_INIT);
@@ -690,7 +721,7 @@ static int enc28j60_hw_init(struct enc28j60_net *priv)
690721

691722
static void enc28j60_hw_enable(struct enc28j60_net *priv)
692723
{
693-
/* enable interrutps */
724+
/* enable interrupts */
694725
if (netif_msg_hw(priv))
695726
printk(KERN_DEBUG DRV_NAME ": %s() enabling interrupts.\n",
696727
__FUNCTION__);
@@ -726,15 +757,12 @@ enc28j60_setlink(struct net_device *ndev, u8 autoneg, u16 speed, u8 duplex)
726757
int ret = 0;
727758

728759
if (!priv->hw_enable) {
729-
if (autoneg == AUTONEG_DISABLE && speed == SPEED_10) {
760+
/* link is in low power mode now; duplex setting
761+
* will take effect on next enc28j60_hw_init().
762+
*/
763+
if (autoneg == AUTONEG_DISABLE && speed == SPEED_10)
730764
priv->full_duplex = (duplex == DUPLEX_FULL);
731-
if (!enc28j60_hw_init(priv)) {
732-
if (netif_msg_drv(priv))
733-
dev_err(&ndev->dev,
734-
"hw_reset() failed\n");
735-
ret = -EINVAL;
736-
}
737-
} else {
765+
else {
738766
if (netif_msg_link(priv))
739767
dev_warn(&ndev->dev,
740768
"unsupported link setting\n");
@@ -1307,7 +1335,8 @@ static int enc28j60_net_open(struct net_device *dev)
13071335
}
13081336
return -EADDRNOTAVAIL;
13091337
}
1310-
/* Reset the hardware here */
1338+
/* Reset the hardware here (and take it out of low power mode) */
1339+
enc28j60_lowpower(priv, false);
13111340
enc28j60_hw_disable(priv);
13121341
if (!enc28j60_hw_init(priv)) {
13131342
if (netif_msg_ifup(priv))
@@ -1337,6 +1366,7 @@ static int enc28j60_net_close(struct net_device *dev)
13371366
printk(KERN_DEBUG DRV_NAME ": %s() enter\n", __FUNCTION__);
13381367

13391368
enc28j60_hw_disable(priv);
1369+
enc28j60_lowpower(priv, true);
13401370
netif_stop_queue(dev);
13411371

13421372
return 0;
@@ -1537,6 +1567,8 @@ static int __devinit enc28j60_probe(struct spi_device *spi)
15371567
dev->watchdog_timeo = TX_TIMEOUT;
15381568
SET_ETHTOOL_OPS(dev, &enc28j60_ethtool_ops);
15391569

1570+
enc28j60_lowpower(priv, true);
1571+
15401572
ret = register_netdev(dev);
15411573
if (ret) {
15421574
if (netif_msg_probe(priv))
@@ -1581,6 +1613,8 @@ static struct spi_driver enc28j60_driver = {
15811613

15821614
static int __init enc28j60_init(void)
15831615
{
1616+
msec20_to_jiffies = msecs_to_jiffies(20);
1617+
15841618
return spi_register_driver(&enc28j60_driver);
15851619
}
15861620

0 commit comments

Comments
 (0)