hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations

imx_fec models the same PHY as lan9118_phy. The code is almost the same with
imx_fec having more logging and tracing. Merge these improvements into
lan9118_phy and reuse in imx_fec to fix the code duplication.

Some migration state how resides in the new device model which breaks migration
compatibility for the following machines:
* imx25-pdk
* sabrelite
* mcimx7d-sabre
* mcimx6ul-evk

Signed-off-by: Bernhard Beschow <shentey@gmail.com>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-id: 20241102125724.532843-3-shentey@gmail.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Bernhard Beschow 2024-12-11 15:30:51 +00:00 committed by Peter Maydell
parent c0cf6b412e
commit c01194e17a
5 changed files with 85 additions and 163 deletions

View file

@ -93,6 +93,7 @@ config ALLWINNER_SUN8I_EMAC
config IMX_FEC config IMX_FEC
bool bool
select LAN9118_PHY
config CADENCE config CADENCE
bool bool

View file

@ -203,17 +203,12 @@ static const VMStateDescription vmstate_imx_eth_txdescs = {
static const VMStateDescription vmstate_imx_eth = { static const VMStateDescription vmstate_imx_eth = {
.name = TYPE_IMX_FEC, .name = TYPE_IMX_FEC,
.version_id = 2, .version_id = 3,
.minimum_version_id = 2, .minimum_version_id = 3,
.fields = (const VMStateField[]) { .fields = (const VMStateField[]) {
VMSTATE_UINT32_ARRAY(regs, IMXFECState, ENET_MAX), VMSTATE_UINT32_ARRAY(regs, IMXFECState, ENET_MAX),
VMSTATE_UINT32(rx_descriptor, IMXFECState), VMSTATE_UINT32(rx_descriptor, IMXFECState),
VMSTATE_UINT32(tx_descriptor[0], IMXFECState), VMSTATE_UINT32(tx_descriptor[0], IMXFECState),
VMSTATE_UINT32(phy_status, IMXFECState),
VMSTATE_UINT32(phy_control, IMXFECState),
VMSTATE_UINT32(phy_advertise, IMXFECState),
VMSTATE_UINT32(phy_int, IMXFECState),
VMSTATE_UINT32(phy_int_mask, IMXFECState),
VMSTATE_END_OF_LIST() VMSTATE_END_OF_LIST()
}, },
.subsections = (const VMStateDescription * const []) { .subsections = (const VMStateDescription * const []) {
@ -222,14 +217,6 @@ static const VMStateDescription vmstate_imx_eth = {
}, },
}; };
#define PHY_INT_ENERGYON (1 << 7)
#define PHY_INT_AUTONEG_COMPLETE (1 << 6)
#define PHY_INT_FAULT (1 << 5)
#define PHY_INT_DOWN (1 << 4)
#define PHY_INT_AUTONEG_LP (1 << 3)
#define PHY_INT_PARFAULT (1 << 2)
#define PHY_INT_AUTONEG_PAGE (1 << 1)
static void imx_eth_update(IMXFECState *s); static void imx_eth_update(IMXFECState *s);
/* /*
@ -238,47 +225,19 @@ static void imx_eth_update(IMXFECState *s);
* For now we don't handle any GPIO/interrupt line, so the OS will * For now we don't handle any GPIO/interrupt line, so the OS will
* have to poll for the PHY status. * have to poll for the PHY status.
*/ */
static void imx_phy_update_irq(IMXFECState *s) static void imx_phy_update_irq(void *opaque, int n, int level)
{ {
imx_eth_update(s); imx_eth_update(opaque);
}
static void imx_phy_update_link(IMXFECState *s)
{
/* Autonegotiation status mirrors link status. */
if (qemu_get_queue(s->nic)->link_down) {
trace_imx_phy_update_link("down");
s->phy_status &= ~0x0024;
s->phy_int |= PHY_INT_DOWN;
} else {
trace_imx_phy_update_link("up");
s->phy_status |= 0x0024;
s->phy_int |= PHY_INT_ENERGYON;
s->phy_int |= PHY_INT_AUTONEG_COMPLETE;
}
imx_phy_update_irq(s);
} }
static void imx_eth_set_link(NetClientState *nc) static void imx_eth_set_link(NetClientState *nc)
{ {
imx_phy_update_link(IMX_FEC(qemu_get_nic_opaque(nc))); lan9118_phy_update_link(&IMX_FEC(qemu_get_nic_opaque(nc))->mii,
} nc->link_down);
static void imx_phy_reset(IMXFECState *s)
{
trace_imx_phy_reset();
s->phy_status = 0x7809;
s->phy_control = 0x3000;
s->phy_advertise = 0x01e1;
s->phy_int_mask = 0;
s->phy_int = 0;
imx_phy_update_link(s);
} }
static uint32_t imx_phy_read(IMXFECState *s, int reg) static uint32_t imx_phy_read(IMXFECState *s, int reg)
{ {
uint32_t val;
uint32_t phy = reg / 32; uint32_t phy = reg / 32;
if (!s->phy_connected) { if (!s->phy_connected) {
@ -296,54 +255,7 @@ static uint32_t imx_phy_read(IMXFECState *s, int reg)
reg %= 32; reg %= 32;
switch (reg) { return lan9118_phy_read(&s->mii, reg);
case 0: /* Basic Control */
val = s->phy_control;
break;
case 1: /* Basic Status */
val = s->phy_status;
break;
case 2: /* ID1 */
val = 0x0007;
break;
case 3: /* ID2 */
val = 0xc0d1;
break;
case 4: /* Auto-neg advertisement */
val = s->phy_advertise;
break;
case 5: /* Auto-neg Link Partner Ability */
val = 0x0f71;
break;
case 6: /* Auto-neg Expansion */
val = 1;
break;
case 29: /* Interrupt source. */
val = s->phy_int;
s->phy_int = 0;
imx_phy_update_irq(s);
break;
case 30: /* Interrupt mask */
val = s->phy_int_mask;
break;
case 17:
case 18:
case 27:
case 31:
qemu_log_mask(LOG_UNIMP, "[%s.phy]%s: reg %d not implemented\n",
TYPE_IMX_FEC, __func__, reg);
val = 0;
break;
default:
qemu_log_mask(LOG_GUEST_ERROR, "[%s.phy]%s: Bad address at offset %d\n",
TYPE_IMX_FEC, __func__, reg);
val = 0;
break;
}
trace_imx_phy_read(val, phy, reg);
return val;
} }
static void imx_phy_write(IMXFECState *s, int reg, uint32_t val) static void imx_phy_write(IMXFECState *s, int reg, uint32_t val)
@ -365,39 +277,7 @@ static void imx_phy_write(IMXFECState *s, int reg, uint32_t val)
reg %= 32; reg %= 32;
trace_imx_phy_write(val, phy, reg); lan9118_phy_write(&s->mii, reg, val);
switch (reg) {
case 0: /* Basic Control */
if (val & 0x8000) {
imx_phy_reset(s);
} else {
s->phy_control = val & 0x7980;
/* Complete autonegotiation immediately. */
if (val & 0x1000) {
s->phy_status |= 0x0020;
}
}
break;
case 4: /* Auto-neg advertisement */
s->phy_advertise = (val & 0x2d7f) | 0x80;
break;
case 30: /* Interrupt mask */
s->phy_int_mask = val & 0xff;
imx_phy_update_irq(s);
break;
case 17:
case 18:
case 27:
case 31:
qemu_log_mask(LOG_UNIMP, "[%s.phy)%s: reg %d not implemented\n",
TYPE_IMX_FEC, __func__, reg);
break;
default:
qemu_log_mask(LOG_GUEST_ERROR, "[%s.phy]%s: Bad address at offset %d\n",
TYPE_IMX_FEC, __func__, reg);
break;
}
} }
static void imx_fec_read_bd(IMXFECBufDesc *bd, dma_addr_t addr) static void imx_fec_read_bd(IMXFECBufDesc *bd, dma_addr_t addr)
@ -682,9 +562,6 @@ static void imx_eth_reset(DeviceState *d)
s->rx_descriptor = 0; s->rx_descriptor = 0;
memset(s->tx_descriptor, 0, sizeof(s->tx_descriptor)); memset(s->tx_descriptor, 0, sizeof(s->tx_descriptor));
/* We also reset the PHY */
imx_phy_reset(s);
} }
static uint32_t imx_default_read(IMXFECState *s, uint32_t index) static uint32_t imx_default_read(IMXFECState *s, uint32_t index)
@ -1329,6 +1206,13 @@ static void imx_eth_realize(DeviceState *dev, Error **errp)
sysbus_init_irq(sbd, &s->irq[0]); sysbus_init_irq(sbd, &s->irq[0]);
sysbus_init_irq(sbd, &s->irq[1]); sysbus_init_irq(sbd, &s->irq[1]);
qemu_init_irq(&s->mii_irq, imx_phy_update_irq, s, 0);
object_initialize_child(OBJECT(s), "mii", &s->mii, TYPE_LAN9118_PHY);
if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(&s->mii), errp)) {
return;
}
qdev_connect_gpio_out(DEVICE(&s->mii), 0, &s->mii_irq);
qemu_macaddr_default_if_unset(&s->conf.macaddr); qemu_macaddr_default_if_unset(&s->conf.macaddr);
s->nic = qemu_new_nic(&imx_eth_net_info, &s->conf, s->nic = qemu_new_nic(&imx_eth_net_info, &s->conf,

View file

@ -4,6 +4,8 @@
* Copyright (c) 2009 CodeSourcery, LLC. * Copyright (c) 2009 CodeSourcery, LLC.
* Written by Paul Brook * Written by Paul Brook
* *
* Copyright (c) 2013 Jean-Christophe Dubois. <jcd@tribudubois.net>
*
* This code is licensed under the GNU GPL v2 * This code is licensed under the GNU GPL v2
* *
* Contributions after 2012-01-13 are licensed under the terms of the * Contributions after 2012-01-13 are licensed under the terms of the
@ -16,6 +18,7 @@
#include "hw/resettable.h" #include "hw/resettable.h"
#include "migration/vmstate.h" #include "migration/vmstate.h"
#include "qemu/log.h" #include "qemu/log.h"
#include "trace.h"
#define PHY_INT_ENERGYON (1 << 7) #define PHY_INT_ENERGYON (1 << 7)
#define PHY_INT_AUTONEG_COMPLETE (1 << 6) #define PHY_INT_AUTONEG_COMPLETE (1 << 6)
@ -36,59 +39,88 @@ uint16_t lan9118_phy_read(Lan9118PhyState *s, int reg)
switch (reg) { switch (reg) {
case 0: /* Basic Control */ case 0: /* Basic Control */
return s->control; val = s->control;
break;
case 1: /* Basic Status */ case 1: /* Basic Status */
return s->status; val = s->status;
break;
case 2: /* ID1 */ case 2: /* ID1 */
return 0x0007; val = 0x0007;
break;
case 3: /* ID2 */ case 3: /* ID2 */
return 0xc0d1; val = 0xc0d1;
break;
case 4: /* Auto-neg advertisement */ case 4: /* Auto-neg advertisement */
return s->advertise; val = s->advertise;
break;
case 5: /* Auto-neg Link Partner Ability */ case 5: /* Auto-neg Link Partner Ability */
return 0x0f71; val = 0x0f71;
break;
case 6: /* Auto-neg Expansion */ case 6: /* Auto-neg Expansion */
return 1; val = 1;
/* TODO 17, 18, 27, 29, 30, 31 */ break;
case 29: /* Interrupt source. */ case 29: /* Interrupt source. */
val = s->ints; val = s->ints;
s->ints = 0; s->ints = 0;
lan9118_phy_update_irq(s); lan9118_phy_update_irq(s);
return val; break;
case 30: /* Interrupt mask */ case 30: /* Interrupt mask */
return s->int_mask; val = s->int_mask;
break;
case 17:
case 18:
case 27:
case 31:
qemu_log_mask(LOG_UNIMP, "%s: reg %d not implemented\n",
__func__, reg);
val = 0;
break;
default: default:
qemu_log_mask(LOG_GUEST_ERROR, qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad address at offset %d\n",
"lan9118_phy_read: PHY read reg %d\n", reg); __func__, reg);
return 0; val = 0;
break;
} }
trace_lan9118_phy_read(val, reg);
return val;
} }
void lan9118_phy_write(Lan9118PhyState *s, int reg, uint16_t val) void lan9118_phy_write(Lan9118PhyState *s, int reg, uint16_t val)
{ {
trace_lan9118_phy_write(val, reg);
switch (reg) { switch (reg) {
case 0: /* Basic Control */ case 0: /* Basic Control */
if (val & 0x8000) { if (val & 0x8000) {
lan9118_phy_reset(s); lan9118_phy_reset(s);
break; } else {
} s->control = val & 0x7980;
s->control = val & 0x7980; /* Complete autonegotiation immediately. */
/* Complete autonegotiation immediately. */ if (val & 0x1000) {
if (val & 0x1000) { s->status |= 0x0020;
s->status |= 0x0020; }
} }
break; break;
case 4: /* Auto-neg advertisement */ case 4: /* Auto-neg advertisement */
s->advertise = (val & 0x2d7f) | 0x80; s->advertise = (val & 0x2d7f) | 0x80;
break; break;
/* TODO 17, 18, 27, 31 */
case 30: /* Interrupt mask */ case 30: /* Interrupt mask */
s->int_mask = val & 0xff; s->int_mask = val & 0xff;
lan9118_phy_update_irq(s); lan9118_phy_update_irq(s);
break; break;
case 17:
case 18:
case 27:
case 31:
qemu_log_mask(LOG_UNIMP, "%s: reg %d not implemented\n",
__func__, reg);
break;
default: default:
qemu_log_mask(LOG_GUEST_ERROR, qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad address at offset %d\n",
"lan9118_phy_write: PHY write reg %d = 0x%04x\n", reg, val); __func__, reg);
break;
} }
} }
@ -98,9 +130,11 @@ void lan9118_phy_update_link(Lan9118PhyState *s, bool link_down)
/* Autonegotiation status mirrors link status. */ /* Autonegotiation status mirrors link status. */
if (link_down) { if (link_down) {
trace_lan9118_phy_update_link("down");
s->status &= ~0x0024; s->status &= ~0x0024;
s->ints |= PHY_INT_DOWN; s->ints |= PHY_INT_DOWN;
} else { } else {
trace_lan9118_phy_update_link("up");
s->status |= 0x0024; s->status |= 0x0024;
s->ints |= PHY_INT_ENERGYON; s->ints |= PHY_INT_ENERGYON;
s->ints |= PHY_INT_AUTONEG_COMPLETE; s->ints |= PHY_INT_AUTONEG_COMPLETE;
@ -110,6 +144,8 @@ void lan9118_phy_update_link(Lan9118PhyState *s, bool link_down)
void lan9118_phy_reset(Lan9118PhyState *s) void lan9118_phy_reset(Lan9118PhyState *s)
{ {
trace_lan9118_phy_reset();
s->control = 0x3000; s->control = 0x3000;
s->status = 0x7809; s->status = 0x7809;
s->advertise = 0x01e1; s->advertise = 0x01e1;
@ -137,8 +173,8 @@ static const VMStateDescription vmstate_lan9118_phy = {
.version_id = 1, .version_id = 1,
.minimum_version_id = 1, .minimum_version_id = 1,
.fields = (const VMStateField[]) { .fields = (const VMStateField[]) {
VMSTATE_UINT16(control, Lan9118PhyState),
VMSTATE_UINT16(status, Lan9118PhyState), VMSTATE_UINT16(status, Lan9118PhyState),
VMSTATE_UINT16(control, Lan9118PhyState),
VMSTATE_UINT16(advertise, Lan9118PhyState), VMSTATE_UINT16(advertise, Lan9118PhyState),
VMSTATE_UINT16(ints, Lan9118PhyState), VMSTATE_UINT16(ints, Lan9118PhyState),
VMSTATE_UINT16(int_mask, Lan9118PhyState), VMSTATE_UINT16(int_mask, Lan9118PhyState),

View file

@ -10,6 +10,12 @@ allwinner_sun8i_emac_set_link(bool active) "Set link: active=%u"
allwinner_sun8i_emac_read(uint64_t offset, uint64_t val) "MMIO read: offset=0x%" PRIx64 " value=0x%" PRIx64 allwinner_sun8i_emac_read(uint64_t offset, uint64_t val) "MMIO read: offset=0x%" PRIx64 " value=0x%" PRIx64
allwinner_sun8i_emac_write(uint64_t offset, uint64_t val) "MMIO write: offset=0x%" PRIx64 " value=0x%" PRIx64 allwinner_sun8i_emac_write(uint64_t offset, uint64_t val) "MMIO write: offset=0x%" PRIx64 " value=0x%" PRIx64
# lan9118_phy.c
lan9118_phy_read(uint16_t val, int reg) "[0x%02x] -> 0x%04" PRIx16
lan9118_phy_write(uint16_t val, int reg) "[0x%02x] <- 0x%04" PRIx16
lan9118_phy_update_link(const char *s) "%s"
lan9118_phy_reset(void) ""
# lance.c # lance.c
lance_mem_readw(uint64_t addr, uint32_t ret) "addr=0x%"PRIx64"val=0x%04x" lance_mem_readw(uint64_t addr, uint32_t ret) "addr=0x%"PRIx64"val=0x%04x"
lance_mem_writew(uint64_t addr, uint32_t val) "addr=0x%"PRIx64"val=0x%04x" lance_mem_writew(uint64_t addr, uint32_t val) "addr=0x%"PRIx64"val=0x%04x"
@ -428,12 +434,8 @@ i82596_set_multicast(uint16_t count) "Added %d multicast entries"
i82596_channel_attention(void *s) "%p: Received CHANNEL ATTENTION" i82596_channel_attention(void *s) "%p: Received CHANNEL ATTENTION"
# imx_fec.c # imx_fec.c
imx_phy_read(uint32_t val, int phy, int reg) "0x%04"PRIx32" <= phy[%d].reg[%d]"
imx_phy_read_num(int phy, int configured) "read request from unconfigured phy %d (configured %d)" imx_phy_read_num(int phy, int configured) "read request from unconfigured phy %d (configured %d)"
imx_phy_write(uint32_t val, int phy, int reg) "0x%04"PRIx32" => phy[%d].reg[%d]"
imx_phy_write_num(int phy, int configured) "write request to unconfigured phy %d (configured %d)" imx_phy_write_num(int phy, int configured) "write request to unconfigured phy %d (configured %d)"
imx_phy_update_link(const char *s) "%s"
imx_phy_reset(void) ""
imx_fec_read_bd(uint64_t addr, int flags, int len, int data) "tx_bd 0x%"PRIx64" flags 0x%04x len %d data 0x%08x" imx_fec_read_bd(uint64_t addr, int flags, int len, int data) "tx_bd 0x%"PRIx64" flags 0x%04x len %d data 0x%08x"
imx_enet_read_bd(uint64_t addr, int flags, int len, int data, int options, int status) "tx_bd 0x%"PRIx64" flags 0x%04x len %d data 0x%08x option 0x%04x status 0x%04x" imx_enet_read_bd(uint64_t addr, int flags, int len, int data, int options, int status) "tx_bd 0x%"PRIx64" flags 0x%04x len %d data 0x%08x option 0x%04x status 0x%04x"
imx_eth_tx_bd_busy(void) "tx_bd ran out of descriptors to transmit" imx_eth_tx_bd_busy(void) "tx_bd ran out of descriptors to transmit"

View file

@ -31,6 +31,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(IMXFECState, IMX_FEC)
#define TYPE_IMX_ENET "imx.enet" #define TYPE_IMX_ENET "imx.enet"
#include "hw/sysbus.h" #include "hw/sysbus.h"
#include "hw/net/lan9118_phy.h"
#include "hw/irq.h"
#include "net/net.h" #include "net/net.h"
#define ENET_EIR 1 #define ENET_EIR 1
@ -264,11 +266,8 @@ struct IMXFECState {
uint32_t tx_descriptor[ENET_TX_RING_NUM]; uint32_t tx_descriptor[ENET_TX_RING_NUM];
uint32_t tx_ring_num; uint32_t tx_ring_num;
uint32_t phy_status; Lan9118PhyState mii;
uint32_t phy_control; IRQState mii_irq;
uint32_t phy_advertise;
uint32_t phy_int;
uint32_t phy_int_mask;
uint32_t phy_num; uint32_t phy_num;
bool phy_connected; bool phy_connected;
struct IMXFECState *phy_consumer; struct IMXFECState *phy_consumer;