Skip to content

Commit b659e16

Browse files
committed
ether: Show link state in ether_print, add shared ethphy_print_status()
Add Link: UP/DOWN at top of ether_print() using NETIF_F_UP flag. Add ethphy_print_status() for standard BMSR/ANLPAR link/speed/duplex printing, used by both dp83826 and generic PHY drivers. Add print_info to generic PHY driver showing PHY ID and link status.
1 parent 2657102 commit b659e16

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

include/mios/ethphy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ error_t ethphy_mii_write(struct ether_netif *eni, uint16_t reg, uint16_t value);
3232
* Raises NETIF_TASK_STATUS_UP/DOWN on transitions.
3333
*/
3434
void ethphy_link_poll(struct ether_netif *eni) __attribute__((noreturn));
35+
36+
void ethphy_print_status(struct ether_netif *eni, struct stream *s);

src/net/ether.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ ether_netif_fini(ether_netif_t *eni)
341341
void
342342
ether_print(ether_netif_t *en, struct stream *st)
343343
{
344+
stprintf(st, "Link: %s\n",
345+
(en->eni_ni.ni_flags & NETIF_F_UP) ? "UP" : "DOWN");
346+
344347
stprintf(st, "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n",
345348
en->eni_addr[0],
346349
en->eni_addr[1],

src/net/ethphy/ethphy_dp83826.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ dp83826_print_info(struct device *dev, struct stream *s)
2424

2525
uint16_t id2 = ethphy_mii_read(eni, REG_PHYIDR2);
2626
uint16_t model = (id2 >> 4) & 0x3f;
27-
uint16_t bmsr = ethphy_mii_read(eni, REG_BMSR);
28-
bmsr = ethphy_mii_read(eni, REG_BMSR); // Read twice -- link status is latched-low
29-
uint16_t physts = ethphy_mii_read(eni, REG_PHYSTS);
30-
31-
stprintf(s, "DP83826%c rev %d\n",
32-
model == 0x13 ? 'E' : 'I',
33-
id2 & 0xf);
34-
35-
stprintf(s, "Link: %s, %s %s duplex\n",
36-
(bmsr & (1 << 2)) ? "UP" : "DOWN",
37-
(physts & (1 << 1)) ? "10M" : "100M",
38-
(physts & (1 << 2)) ? "full" : "half");
3927

4028
uint16_t rcsr = ethphy_mii_read(eni, REG_RCSR);
41-
stprintf(s, "Interface: %sMII\n",
29+
stprintf(s, "DP83826%c rev %d Interface: %sMII\n",
30+
model == 0x13 ? 'E' : 'I',
31+
id2 & 0xf,
4232
(rcsr & 0x20) ? "R" : "");
33+
ethphy_print_status(eni, s);
4334
}
4435

4536
static const device_class_t ethphy_dp83826 = {

src/net/ethphy/ethphy_generic.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
#include <mios/ethphy.h>
22
#include <mios/driver.h>
33
#include <mios/eventlog.h>
4+
#include <stdio.h>
45

56
#include <net/ether.h>
67

8+
void
9+
ethphy_print_status(struct ether_netif *eni, struct stream *s)
10+
{
11+
uint16_t bmsr = ethphy_mii_read(eni, 0x01);
12+
bmsr = ethphy_mii_read(eni, 0x01); // Read twice -- link status is latched-low
13+
14+
if(bmsr & (1 << 2)) {
15+
uint16_t adv = ethphy_mii_read(eni, 0x04);
16+
uint16_t lpa = ethphy_mii_read(eni, 0x05);
17+
int common = adv & lpa;
18+
const char *speed = "10M";
19+
const char *duplex = "half";
20+
if(common & (1 << 8)) {
21+
speed = "100M"; duplex = "full";
22+
} else if(common & (1 << 7)) {
23+
speed = "100M";
24+
} else if(common & (1 << 6)) {
25+
duplex = "full";
26+
}
27+
stprintf(s, "Link: UP, %s %s duplex\n", speed, duplex);
28+
} else {
29+
stprintf(s, "Link: DOWN\n");
30+
}
31+
}
32+
33+
static void
34+
generic_print_info(struct device *dev, struct stream *s)
35+
{
36+
struct ether_netif *eni = (struct ether_netif *)dev->d_parent;
37+
uint16_t id1 = ethphy_mii_read(eni, 0x02);
38+
uint16_t id2 = ethphy_mii_read(eni, 0x03);
39+
40+
stprintf(s, "PHY OUI:0x%05x Model:0x%02x Rev:%d\n",
41+
(id1 << 6) | (id2 >> 10), (id2 >> 4) & 0x3f, id2 & 0xf);
42+
ethphy_print_status(eni, s);
43+
}
44+
745
static const device_class_t ethphy_generic = {
846
.dc_class_name = "Generic PHY",
47+
.dc_print_info = generic_print_info,
948
};
1049

1150
static device_t *

0 commit comments

Comments
 (0)