-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "phy.h" | ||
|
||
enum { | ||
MG_PHY_KSZ8x = 0x22, | ||
MG_PHY_DP83x = 0x2000, | ||
MG_PHY_LAN87x = 0x7, | ||
}; | ||
|
||
enum { | ||
MG_PHY_REG_BCR = 0, | ||
MG_PHY_REG_BSR = 1, | ||
MG_PHY_REG_ID1 = 2, | ||
MG_PHY_REG_ID2 = 3, | ||
MG_PHY_REG_CSCR = 31, | ||
}; | ||
|
||
static const char *mg_phy_id_to_str(uint16_t id) { | ||
switch (id) { | ||
case MG_PHY_KSZ8x: return "KSZ8x"; | ||
case MG_PHY_DP83x: return "DP83x"; | ||
case MG_PHY_LAN87x: return "LAN87x"; | ||
default: return "unknown"; | ||
} | ||
} | ||
|
||
void mg_phy_init(struct mg_phy *phy) { | ||
uint16_t id1 = phy->read_reg(phy->addr, MG_PHY_REG_ID1); | ||
uint16_t id2 = phy->read_reg(phy->addr, MG_PHY_REG_ID2); | ||
MG_INFO(("PHY ID: %#04x %#04x (%s)", id1, id2, mg_phy_id_to_str(id1))); | ||
|
||
phy->write_reg(phy->addr, MG_PHY_REG_BCR, MG_BIT(15)); // Reset PHY | ||
phy->write_reg(phy->addr, MG_PHY_REG_BCR, MG_BIT(12)); // Autonegotiation | ||
|
||
if (id1 == MG_PHY_KSZ8x) { | ||
phy->write_reg(phy->addr, MG_PHY_REG_CSCR, | ||
MG_BIT(15) | MG_BIT(8) | MG_BIT(7)); | ||
} else if (id1 == MG_PHY_DP83x) { | ||
phy->write_reg(phy->addr, 23, 0x81); // 50MHz clock input | ||
phy->write_reg(phy->addr, 24, 0x280); // LED status, active high | ||
} | ||
} | ||
|
||
void mg_phy_status(struct mg_phy *phy, bool *up, bool *full_duplex, | ||
uint8_t *speed) { | ||
uint16_t bsr = phy->read_reg(phy->addr, MG_PHY_REG_BSR); | ||
*up = bsr & MG_BIT(2); | ||
if (*up) { | ||
uint16_t scsr = phy->read_reg(phy->addr, MG_PHY_REG_CSCR); | ||
*full_duplex = scsr & (1U << 4U); | ||
*speed = scsr & MG_BIT(3) ? MG_PHY_SPEED_100M : MG_PHY_SPEED_10M; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include "net_builtin.h" | ||
|
||
struct mg_phy { | ||
uint8_t addr; | ||
uint16_t (*read_reg)(uint8_t addr, uint8_t reg); | ||
void (*write_reg)(uint8_t addr, uint8_t reg, uint16_t value); | ||
}; | ||
|
||
enum { MG_PHY_SPEED_10M, MG_PHY_SPEED_100M, MG_PHY_SPEED_1000M }; | ||
|
||
void mg_phy_init(struct mg_phy *); | ||
void mg_phy_status(struct mg_phy *, bool *up, bool *full_duplex, | ||
uint8_t *speed); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters