2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-04-15 04:08:20 +00:00
|
|
|
/*
|
|
|
|
* SMSC LAN9[12]1[567] Network driver
|
|
|
|
*
|
2008-05-08 20:52:09 +00:00
|
|
|
* (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
|
2008-04-15 04:08:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-08-18 13:19:02 +00:00
|
|
|
#include <env.h>
|
2008-04-15 04:08:20 +00:00
|
|
|
#include <command.h>
|
2009-07-21 05:01:11 +00:00
|
|
|
#include <malloc.h>
|
2008-04-15 04:08:20 +00:00
|
|
|
#include <net.h>
|
|
|
|
#include <miiphy.h>
|
2020-03-15 13:42:23 +00:00
|
|
|
#include <linux/io.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2020-03-15 14:03:07 +00:00
|
|
|
#include <linux/types.h>
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2009-02-23 15:29:47 +00:00
|
|
|
#include "smc911x.h"
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:03:07 +00:00
|
|
|
struct chip_id {
|
|
|
|
u16 id;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
struct smc911x_priv {
|
2020-03-15 16:39:01 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
2020-03-15 14:36:09 +00:00
|
|
|
struct eth_device dev;
|
2020-03-15 16:39:01 +00:00
|
|
|
#endif
|
2020-03-15 14:36:09 +00:00
|
|
|
phys_addr_t iobase;
|
|
|
|
const struct chip_id *chipid;
|
|
|
|
unsigned char enetaddr[6];
|
|
|
|
};
|
|
|
|
|
2020-03-15 14:03:07 +00:00
|
|
|
static const struct chip_id chip_ids[] = {
|
|
|
|
{ CHIP_89218, "LAN89218" },
|
|
|
|
{ CHIP_9115, "LAN9115" },
|
|
|
|
{ CHIP_9116, "LAN9116" },
|
|
|
|
{ CHIP_9117, "LAN9117" },
|
|
|
|
{ CHIP_9118, "LAN9118" },
|
|
|
|
{ CHIP_9211, "LAN9211" },
|
|
|
|
{ CHIP_9215, "LAN9215" },
|
|
|
|
{ CHIP_9216, "LAN9216" },
|
|
|
|
{ CHIP_9217, "LAN9217" },
|
|
|
|
{ CHIP_9218, "LAN9218" },
|
|
|
|
{ CHIP_9220, "LAN9220" },
|
|
|
|
{ CHIP_9221, "LAN9221" },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DRIVERNAME "smc911x"
|
|
|
|
|
|
|
|
#if defined (CONFIG_SMC911X_32_BIT) && \
|
|
|
|
defined (CONFIG_SMC911X_16_BIT)
|
|
|
|
#error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
|
|
|
|
CONFIG_SMC911X_16_BIT shall be set"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined (CONFIG_SMC911X_32_BIT)
|
2020-03-15 14:36:09 +00:00
|
|
|
static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
return readl(priv->iobase + offset);
|
2020-03-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
writel(val, priv->iobase + offset);
|
2020-03-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
#elif defined (CONFIG_SMC911X_16_BIT)
|
2020-03-15 14:36:09 +00:00
|
|
|
static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
return (readw(priv->iobase + offset) & 0xffff) |
|
|
|
|
(readw(priv->iobase + offset + 2) << 16);
|
2020-03-15 14:03:07 +00:00
|
|
|
}
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
writew(val & 0xffff, priv->iobase + offset);
|
|
|
|
writew(val >> 16, priv->iobase + offset + 2);
|
2020-03-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#error "SMC911X: undefined bus width"
|
|
|
|
#endif /* CONFIG_SMC911X_16_BIT */
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
|
2020-03-15 14:03:07 +00:00
|
|
|
;
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, MAC_CSR_CMD,
|
2020-03-15 14:03:07 +00:00
|
|
|
MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
|
2020-03-15 14:03:07 +00:00
|
|
|
;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
return smc911x_reg_read(priv, MAC_CSR_DATA);
|
2020-03-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
|
2020-03-15 14:03:07 +00:00
|
|
|
;
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, MAC_CSR_DATA, data);
|
|
|
|
smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
|
|
|
|
while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
|
2020-03-15 14:03:07 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static int smc911x_detect_chip(struct smc911x_priv *priv)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
unsigned long val, i;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
val = smc911x_reg_read(priv, BYTE_TEST);
|
2020-03-15 14:03:07 +00:00
|
|
|
if (val == 0xffffffff) {
|
|
|
|
/* Special case -- no chip present */
|
|
|
|
return -1;
|
|
|
|
} else if (val != 0x87654321) {
|
|
|
|
printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
val = smc911x_reg_read(priv, ID_REV) >> 16;
|
2020-03-15 14:03:07 +00:00
|
|
|
for (i = 0; chip_ids[i].id != 0; i++) {
|
|
|
|
if (chip_ids[i].id == val) break;
|
|
|
|
}
|
|
|
|
if (!chip_ids[i].id) {
|
|
|
|
printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
priv->chipid = &chip_ids[i];
|
2020-03-15 14:03:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_reset(struct smc911x_priv *priv)
|
2020-03-15 14:03:07 +00:00
|
|
|
{
|
|
|
|
int timeout;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take out of PM setting first
|
|
|
|
* Device is already wake up if PMT_CTRL_READY bit is set
|
|
|
|
*/
|
2020-03-15 14:36:09 +00:00
|
|
|
if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) {
|
2020-03-15 14:03:07 +00:00
|
|
|
/* Write to the bytetest will take out of powerdown */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, BYTE_TEST, 0x0);
|
2020-03-15 14:03:07 +00:00
|
|
|
|
|
|
|
timeout = 10;
|
|
|
|
|
|
|
|
while (timeout-- &&
|
2020-03-15 14:36:09 +00:00
|
|
|
!(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY))
|
2020-03-15 14:03:07 +00:00
|
|
|
udelay(10);
|
|
|
|
if (timeout < 0) {
|
|
|
|
printf(DRIVERNAME
|
|
|
|
": timeout waiting for PM restore\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable interrupts */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, INT_EN, 0);
|
2020-03-15 14:03:07 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST);
|
2020-03-15 14:03:07 +00:00
|
|
|
|
|
|
|
timeout = 1000;
|
2020-03-15 14:36:09 +00:00
|
|
|
while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY)
|
2020-03-15 14:03:07 +00:00
|
|
|
udelay(10);
|
|
|
|
|
|
|
|
if (timeout < 0) {
|
|
|
|
printf(DRIVERNAME ": reset timeout\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the FIFO level and flow control settings */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN);
|
|
|
|
smc911x_reg_write(priv, AFC_CFG, 0x0050287F);
|
2020-03-15 14:03:07 +00:00
|
|
|
|
|
|
|
/* Set to LED outputs */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, GPIO_CFG, 0x70070000);
|
2020-03-15 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_handle_mac_address(struct smc911x_priv *priv)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
|
|
|
unsigned long addrh, addrl;
|
2020-03-15 14:36:09 +00:00
|
|
|
unsigned char *m = priv->enetaddr;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2009-07-21 05:01:11 +00:00
|
|
|
addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
|
|
|
|
addrh = m[4] | (m[5] << 8);
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_set_mac_csr(priv, ADDRL, addrl);
|
|
|
|
smc911x_set_mac_csr(priv, ADDRH, addrh);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2009-07-21 05:01:11 +00:00
|
|
|
printf(DRIVERNAME ": MAC %pM\n", m);
|
2020-08-18 13:19:02 +00:00
|
|
|
if (!env_get("ethaddr"))
|
|
|
|
env_set("ethaddr", (const char *)m);
|
2008-04-15 04:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 11:03:17 +00:00
|
|
|
static bool smc911x_read_mac_address(struct smc911x_priv *priv)
|
|
|
|
{
|
|
|
|
u32 addrh, addrl;
|
|
|
|
|
|
|
|
/* address is obtained from optional eeprom */
|
|
|
|
addrh = smc911x_get_mac_csr(priv, ADDRH);
|
|
|
|
addrl = smc911x_get_mac_csr(priv, ADDRL);
|
|
|
|
if (addrl == 0xffffffff && addrh == 0x0000ffff)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
priv->enetaddr[0] = addrl;
|
|
|
|
priv->enetaddr[1] = addrl >> 8;
|
|
|
|
priv->enetaddr[2] = addrl >> 16;
|
|
|
|
priv->enetaddr[3] = addrl >> 24;
|
|
|
|
priv->enetaddr[4] = addrh;
|
|
|
|
priv->enetaddr[5] = addrh >> 8;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static int smc911x_eth_phy_read(struct smc911x_priv *priv,
|
2009-07-21 05:01:11 +00:00
|
|
|
u8 phy, u8 reg, u16 *val)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
|
2008-04-29 12:35:08 +00:00
|
|
|
;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 |
|
2009-07-21 05:01:11 +00:00
|
|
|
MII_ACC_MII_BUSY);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
|
2008-04-29 12:35:08 +00:00
|
|
|
;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
*val = smc911x_get_mac_csr(priv, MII_DATA);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static int smc911x_eth_phy_write(struct smc911x_priv *priv,
|
2009-07-21 05:01:11 +00:00
|
|
|
u8 phy, u8 reg, u16 val)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
|
2008-04-29 12:35:08 +00:00
|
|
|
;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_set_mac_csr(priv, MII_DATA, val);
|
|
|
|
smc911x_set_mac_csr(priv, MII_ACC,
|
2008-04-15 04:08:20 +00:00
|
|
|
phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY)
|
2008-04-29 12:35:08 +00:00
|
|
|
;
|
2008-04-15 04:08:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static int smc911x_phy_reset(struct smc911x_priv *priv)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
|
|
|
u32 reg;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
reg = smc911x_reg_read(priv, PMT_CTRL);
|
2008-04-15 04:08:20 +00:00
|
|
|
reg &= ~0xfffff030;
|
|
|
|
reg |= PMT_CTRL_PHY_RST;
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, PMT_CTRL, reg);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
mdelay(100);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_phy_configure(struct smc911x_priv *priv)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
|
|
|
int timeout;
|
|
|
|
u16 status;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_phy_reset(priv);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET);
|
2008-04-15 04:08:20 +00:00
|
|
|
mdelay(1);
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1);
|
|
|
|
smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE |
|
2010-12-23 20:40:12 +00:00
|
|
|
BMCR_ANRESTART);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
timeout = 5000;
|
|
|
|
do {
|
|
|
|
mdelay(1);
|
|
|
|
if ((timeout--) == 0)
|
|
|
|
goto err_out;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0)
|
2008-04-15 04:08:20 +00:00
|
|
|
goto err_out;
|
2010-12-23 20:40:12 +00:00
|
|
|
} while (!(status & BMSR_LSTATUS));
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
printf(DRIVERNAME ": phy initialized\n");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
printf(DRIVERNAME ": autonegotiation timed out\n");
|
|
|
|
}
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static void smc911x_enable(struct smc911x_priv *priv)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
|
|
|
/* Enable TX */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
/* no padding to start of packets */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, RX_CFG, 0);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
|
2009-07-21 05:01:11 +00:00
|
|
|
MAC_CR_HBDIS);
|
2008-04-15 04:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 16:25:27 +00:00
|
|
|
static int smc911x_init_common(struct smc911x_priv *priv)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
const struct chip_id *id = priv->chipid;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2009-10-27 23:49:47 +00:00
|
|
|
printf(DRIVERNAME ": detected %s controller\n", id->name);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reset(priv);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
/* Configure the PHY, initialize the link state */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_phy_configure(priv);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_handle_mac_address(priv);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
/* Turn on Tx + Rx */
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_enable(priv);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:25:27 +00:00
|
|
|
static int smc911x_send_common(struct smc911x_priv *priv,
|
|
|
|
void *packet, int length)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
|
|
|
u32 *data = (u32*)packet;
|
|
|
|
u32 tmplen;
|
|
|
|
u32 status;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
|
2009-07-21 05:01:11 +00:00
|
|
|
TX_CMD_A_INT_LAST_SEG | length);
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, TX_DATA_FIFO, length);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
tmplen = (length + 3) / 4;
|
|
|
|
|
2008-04-29 12:35:08 +00:00
|
|
|
while (tmplen--)
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reg_write(priv, TX_DATA_FIFO, *data++);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
/* wait for transmission */
|
2020-03-15 14:36:09 +00:00
|
|
|
while (!((smc911x_reg_read(priv, TX_FIFO_INF) &
|
2009-07-21 05:01:11 +00:00
|
|
|
TX_FIFO_INF_TSUSED) >> 16));
|
2008-04-15 04:08:20 +00:00
|
|
|
|
|
|
|
/* get status. Ignore 'no carrier' error, it has no meaning for
|
|
|
|
* full duplex operation
|
|
|
|
*/
|
2020-03-15 14:36:09 +00:00
|
|
|
status = smc911x_reg_read(priv, TX_STATUS_FIFO) &
|
2009-07-21 05:01:11 +00:00
|
|
|
(TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
|
|
|
|
TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2008-04-29 12:35:08 +00:00
|
|
|
if (!status)
|
2008-04-15 04:08:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
|
|
|
|
status & TX_STS_LOC ? "TX_STS_LOC " : "",
|
|
|
|
status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
|
|
|
|
status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
|
|
|
|
status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
|
|
|
|
status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:25:27 +00:00
|
|
|
static void smc911x_halt_common(struct smc911x_priv *priv)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
smc911x_reset(priv);
|
|
|
|
smc911x_handle_mac_address(priv);
|
2008-04-15 04:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 16:25:27 +00:00
|
|
|
static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data)
|
2008-04-15 04:08:20 +00:00
|
|
|
{
|
|
|
|
u32 pktlen, tmplen;
|
|
|
|
u32 status;
|
|
|
|
|
2020-03-15 16:02:05 +00:00
|
|
|
status = smc911x_reg_read(priv, RX_FIFO_INF);
|
|
|
|
if (!(status & RX_FIFO_INF_RXSUSED))
|
|
|
|
return 0;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 16:02:05 +00:00
|
|
|
status = smc911x_reg_read(priv, RX_STATUS_FIFO);
|
|
|
|
pktlen = (status & RX_STS_PKT_LEN) >> 16;
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 16:02:05 +00:00
|
|
|
smc911x_reg_write(priv, RX_CFG, 0);
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 16:02:05 +00:00
|
|
|
tmplen = (pktlen + 3) / 4;
|
|
|
|
while (tmplen--)
|
|
|
|
*data++ = smc911x_reg_read(priv, RX_DATA_FIFO);
|
|
|
|
|
2020-03-15 16:25:27 +00:00
|
|
|
if (status & RX_STS_ES) {
|
2020-03-15 16:02:05 +00:00
|
|
|
printf(DRIVERNAME
|
|
|
|
": dropped bad packet. Status: 0x%08x\n",
|
|
|
|
status);
|
2020-03-15 16:25:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-04-15 04:08:20 +00:00
|
|
|
|
2020-03-15 16:25:27 +00:00
|
|
|
return pktlen;
|
2008-04-15 04:08:20 +00:00
|
|
|
}
|
2009-07-21 05:01:11 +00:00
|
|
|
|
2020-03-15 16:39:01 +00:00
|
|
|
#ifndef CONFIG_DM_ETH
|
|
|
|
|
2011-06-29 00:12:14 +00:00
|
|
|
#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
|
|
|
|
/* wrapper for smc911x_eth_phy_read */
|
2016-08-08 16:28:38 +00:00
|
|
|
static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
|
|
|
|
int reg)
|
2011-06-29 00:12:14 +00:00
|
|
|
{
|
2016-08-08 16:28:38 +00:00
|
|
|
struct eth_device *dev = eth_get_dev_by_name(bus->name);
|
2020-03-15 14:36:09 +00:00
|
|
|
struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
|
2020-03-15 14:43:20 +00:00
|
|
|
u16 val = 0;
|
|
|
|
int ret;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
if (!dev || !priv)
|
2020-03-15 14:43:20 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
ret = smc911x_eth_phy_read(priv, phy, reg, &val);
|
2020-03-15 14:43:20 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return val;
|
2011-06-29 00:12:14 +00:00
|
|
|
}
|
2020-03-15 14:36:09 +00:00
|
|
|
|
2011-06-29 00:12:14 +00:00
|
|
|
/* wrapper for smc911x_eth_phy_write */
|
2016-08-08 16:28:38 +00:00
|
|
|
static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
|
|
|
|
int reg, u16 val)
|
2011-06-29 00:12:14 +00:00
|
|
|
{
|
2016-08-08 16:28:38 +00:00
|
|
|
struct eth_device *dev = eth_get_dev_by_name(bus->name);
|
2020-03-15 14:36:09 +00:00
|
|
|
struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
|
2020-03-15 14:43:20 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
if (!dev || !priv)
|
2020-03-15 14:43:20 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
return smc911x_eth_phy_write(priv, phy, reg, val);
|
2011-06-29 00:12:14 +00:00
|
|
|
}
|
2020-03-21 16:25:41 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
static int smc911x_initialize_mii(struct smc911x_priv *priv)
|
2020-03-21 16:25:41 +00:00
|
|
|
{
|
|
|
|
struct mii_dev *mdiodev = mdio_alloc();
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!mdiodev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
strncpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN);
|
2020-03-21 16:25:41 +00:00
|
|
|
mdiodev->read = smc911x_miiphy_read;
|
|
|
|
mdiodev->write = smc911x_miiphy_write;
|
|
|
|
|
|
|
|
ret = mdio_register(mdiodev);
|
|
|
|
if (ret < 0) {
|
|
|
|
mdio_free(mdiodev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2020-03-15 14:36:09 +00:00
|
|
|
static int smc911x_initialize_mii(struct smc911x_priv *priv)
|
2020-03-21 16:25:41 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-06-29 00:12:14 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-26 06:13:34 +00:00
|
|
|
static int smc911x_init(struct eth_device *dev, struct bd_info *bd)
|
2020-03-15 16:25:27 +00:00
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
|
|
|
|
|
|
|
|
return smc911x_init_common(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void smc911x_halt(struct eth_device *dev)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
|
|
|
|
|
|
|
|
smc911x_halt_common(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smc911x_send(struct eth_device *dev, void *packet, int length)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
|
|
|
|
|
|
|
|
return smc911x_send_common(priv, packet, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smc911x_recv(struct eth_device *dev)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev);
|
|
|
|
u32 *data = (u32 *)net_rx_packets[0];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = smc911x_recv_common(priv, data);
|
|
|
|
if (ret)
|
|
|
|
net_process_received_packet(net_rx_packets[0], ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-21 05:01:11 +00:00
|
|
|
int smc911x_initialize(u8 dev_num, int base_addr)
|
|
|
|
{
|
2020-03-15 14:36:09 +00:00
|
|
|
struct smc911x_priv *priv;
|
2020-03-21 16:25:41 +00:00
|
|
|
int ret;
|
2009-07-21 05:01:11 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
priv = calloc(1, sizeof(*priv));
|
|
|
|
if (!priv)
|
2020-03-15 14:14:18 +00:00
|
|
|
return -ENOMEM;
|
2009-07-21 05:01:11 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
priv->iobase = base_addr;
|
|
|
|
priv->dev.iobase = base_addr;
|
2009-07-21 05:01:11 +00:00
|
|
|
|
2009-10-20 16:21:18 +00:00
|
|
|
/* Try to detect chip. Will fail if not present. */
|
2020-03-15 14:36:09 +00:00
|
|
|
ret = smc911x_detect_chip(priv);
|
2020-03-21 16:25:41 +00:00
|
|
|
if (ret) {
|
|
|
|
ret = 0; /* Card not detected is not an error */
|
|
|
|
goto err_detect;
|
2009-10-20 16:21:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 11:03:17 +00:00
|
|
|
if (smc911x_read_mac_address(priv))
|
2020-03-15 16:39:01 +00:00
|
|
|
memcpy(priv->dev.enetaddr, priv->enetaddr, 6);
|
2009-07-21 05:01:11 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
priv->dev.init = smc911x_init;
|
|
|
|
priv->dev.halt = smc911x_halt;
|
|
|
|
priv->dev.send = smc911x_send;
|
|
|
|
priv->dev.recv = smc911x_recv;
|
|
|
|
sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num);
|
2009-07-21 05:01:11 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
eth_register(&priv->dev);
|
2011-06-29 00:12:14 +00:00
|
|
|
|
2020-03-15 14:36:09 +00:00
|
|
|
ret = smc911x_initialize_mii(priv);
|
2020-03-21 16:25:41 +00:00
|
|
|
if (ret)
|
|
|
|
goto err_mii;
|
2011-06-29 00:12:14 +00:00
|
|
|
|
2009-11-12 13:35:08 +00:00
|
|
|
return 1;
|
2020-03-21 16:25:41 +00:00
|
|
|
|
|
|
|
err_mii:
|
2020-03-15 14:36:09 +00:00
|
|
|
eth_unregister(&priv->dev);
|
2020-03-21 16:25:41 +00:00
|
|
|
err_detect:
|
2020-03-15 14:36:09 +00:00
|
|
|
free(priv);
|
2020-03-21 16:25:41 +00:00
|
|
|
return ret;
|
2009-07-21 05:01:11 +00:00
|
|
|
}
|
2020-03-15 16:39:01 +00:00
|
|
|
|
|
|
|
#else /* ifdef CONFIG_DM_ETH */
|
|
|
|
|
|
|
|
static int smc911x_start(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct eth_pdata *plat = dev_get_platdata(dev);
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
|
|
|
|
|
|
|
|
return smc911x_init_common(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void smc911x_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
smc911x_halt_common(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smc911x_send(struct udevice *dev, void *packet, int length)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = smc911x_send_common(priv, packet, length);
|
|
|
|
|
|
|
|
return ret ? 0 : -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
u32 *data = (u32 *)net_rx_packets[0];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = smc911x_recv_common(priv, data);
|
|
|
|
if (ret)
|
|
|
|
*packetp = (void *)data;
|
|
|
|
|
|
|
|
return ret ? ret : -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2020-06-11 11:03:17 +00:00
|
|
|
static int smc911x_read_rom_hwaddr(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
struct eth_pdata *pdata = dev_get_platdata(dev);
|
|
|
|
|
|
|
|
if (!smc911x_read_mac_address(priv))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:39:01 +00:00
|
|
|
static int smc911x_bind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return device_set_name(dev, dev->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smc911x_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Try to detect chip. Will fail if not present. */
|
|
|
|
ret = smc911x_detect_chip(priv);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-06-11 11:03:17 +00:00
|
|
|
smc911x_read_rom_hwaddr(dev);
|
2020-03-15 16:39:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smc911x_ofdata_to_platdata(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct smc911x_priv *priv = dev_get_priv(dev);
|
|
|
|
struct eth_pdata *pdata = dev_get_platdata(dev);
|
|
|
|
|
2020-07-17 05:36:48 +00:00
|
|
|
pdata->iobase = dev_read_addr(dev);
|
2020-03-15 16:39:01 +00:00
|
|
|
priv->iobase = pdata->iobase;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct eth_ops smc911x_ops = {
|
|
|
|
.start = smc911x_start,
|
|
|
|
.send = smc911x_send,
|
|
|
|
.recv = smc911x_recv,
|
|
|
|
.stop = smc911x_stop,
|
2020-06-11 11:03:17 +00:00
|
|
|
.read_rom_hwaddr = smc911x_read_rom_hwaddr,
|
2020-03-15 16:39:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id smc911x_ids[] = {
|
|
|
|
{ .compatible = "smsc,lan9115" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(smc911x) = {
|
|
|
|
.name = "eth_smc911x",
|
|
|
|
.id = UCLASS_ETH,
|
|
|
|
.of_match = smc911x_ids,
|
|
|
|
.bind = smc911x_bind,
|
|
|
|
.ofdata_to_platdata = smc911x_ofdata_to_platdata,
|
|
|
|
.probe = smc911x_probe,
|
|
|
|
.ops = &smc911x_ops,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct smc911x_priv),
|
|
|
|
.platdata_auto_alloc_size = sizeof(struct eth_pdata),
|
|
|
|
.flags = DM_FLAG_ALLOC_PRIV_DMA,
|
|
|
|
};
|
|
|
|
#endif
|