|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
RMAC off-chip PHY driver. More...
#include "ra8_rmac_phy.h"#include <stdint.h>#include "ra8_attributes.h"#include "ra8_check.h"#include "ra8_err.h"#include "ra8_log.h"#include "ra8_rmac_phy_internal.h"Go to the source code of this file.
Data Structures | |
| struct | ra8_rmac_phy_internal_t |
Enumerations | |
| enum | ra8_rmac_phy_reg_t : uint8_t { k_ra8_rmac_phy_reg_control = 0U , k_ra8_rmac_phy_reg_status = 1U , k_ra8_rmac_phy_reg_an_advert = 4U , k_ra8_rmac_phy_reg_an_partner = 5U , k_ra8_rmac_phy_reg_1000t_ctrl = 9U , k_ra8_rmac_phy_reg_1000t_status = 10U , k_ra8_rmac_phy_reset_poll_max = 32U } |
| enum | ra8_rmac_phy_bits_t : uint16_t { k_ra8_rmac_phy_bmcr_reset = 0x8000U , k_ra8_rmac_phy_bmcr_an_enable = 0x1000U , k_ra8_rmac_phy_bmcr_an_restart = 0x0200U , k_ra8_rmac_phy_bmsr_link_up = 0x0004U , k_ra8_rmac_phy_bmsr_an_complete = 0x0020U , k_ra8_rmac_phy_lpa_100full = 0x0100U , k_ra8_rmac_phy_lpa_100half = 0x0080U , k_ra8_rmac_phy_lpa_10full = 0x0040U , k_ra8_rmac_phy_lpa_10half = 0x0020U , k_ra8_rmac_phy_msr_1000full = 0x0800U , k_ra8_rmac_phy_msr_1000half = 0x0400U } |
Functions | |
| bool | priv_ra8_rmac_phy_internal_speed_ok (ra8_err_t err, uint16_t reg_value, uint16_t mask) |
| Pure speed-detected predicate – see header for full contract. | |
| static ra8_err_t | internal_open_validate (const ra8_rmac_phy_cfg_t *cfg) |
| Validate an ra8_rmac_phy_cfg_t before it is latched by open. | |
| static ra8_err_t | internal_reset_and_wait (uint16_t poll_max) |
| Issue BMCR.RESET to the PHY and poll until it self-clears. | |
| static ra8_err_t | internal_program_advertise (void) |
| Program the auto-negotiation advertisement registers. | |
| ra8_err_t | ra8_rmac_phy_open (const ra8_rmac_phy_cfg_t *cfg) |
| Open the RMAC PHY driver and bring the off-chip PHY out of reset. | |
| ra8_err_t | ra8_rmac_phy_close (void) |
| Close the PHY driver and forget all cached state. | |
| ra8_err_t | ra8_rmac_phy_mdio_read (uint8_t reg_addr, uint16_t *out_data) |
| Read a 16-bit Clause-22 PHY register. | |
| ra8_err_t | ra8_rmac_phy_mdio_write (uint8_t reg_addr, uint16_t data) |
| Write a 16-bit Clause-22 PHY register. | |
| ra8_err_t | ra8_rmac_phy_auto_negotiate_start (void) |
| Restart auto-negotiation by writing BMCR. | |
| static void | internal_resolve_speed (ra8_rmac_phy_link_t *out) |
| Resolve the negotiated speed once the link is up and AN done. | |
| ra8_err_t | ra8_rmac_phy_link_status_get (ra8_rmac_phy_link_t *out) |
| Read BMSR / 1000T-status / LPA and decode the link state. | |
| ra8_err_t | ra8_rmac_phy_lsi_get (ra8_rmac_phy_lsi_t *out) |
| Return the LSI tag the caller passed to ra8_rmac_phy_open. | |
Variables | |
| static const char * | s_tag = "RMPHY" |
| static ra8_rmac_phy_internal_t | s_state = {} |
RMAC off-chip PHY driver.
Companion driver to ra8_rmac.c. Same MDIO bus shape as ra8_ether_phy but tracks 1000BASE-T capabilities and a vendor LSI identifier (KSZ8041 / KSZ8091RNB / DP83620 / ICS1894 / GPY111 / VSC8541) so future quirks can fan out by lsi_type.
Definition in file ra8_rmac_phy.c.
| enum ra8_rmac_phy_bits_t : uint16_t |
Definition at line 61 of file ra8_rmac_phy.c.
| enum ra8_rmac_phy_reg_t : uint8_t |
Definition at line 51 of file ra8_rmac_phy.c.
|
static |
Validate an ra8_rmac_phy_cfg_t before it is latched by open.
Shared precondition gate for ra8_rmac_phy_open: rejects NULL configuration / IO callbacks, an out-of-range Clause-22 PHY address, and an unknown LSI family tag. Extracted so the open path stays within the NASA P10 Rule 4 statement cap.
| [in] | cfg | Caller-supplied configuration block (may be NULL). |
| k_ra8_ok | Configuration is well-formed. |
| k_ra8_err_null_ptr | cfg or a required IO callback NULL. |
| k_ra8_err_invalid_arg | phy_address or lsi_type bad. |
Definition at line 112 of file ra8_rmac_phy.c.
References ra8_rmac_phy_cfg_t::io, k_ra8_err_invalid_arg, k_ra8_ok, k_ra8_rmac_phy_addr_max, k_ra8_rmac_phy_lsi_count, ra8_rmac_phy_cfg_t::lsi_type, ra8_rmac_phy_cfg_t::phy_address, RA8_CHECK_NULL_PTR, ra8_rmac_phy_io_t::read, s_tag, and ra8_rmac_phy_io_t::write.
Referenced by ra8_rmac_phy_open().
|
static |
Program the auto-negotiation advertisement registers.
Writes the cached local-advertise word into Clause-22 register 4 (AN_ADVERT) and – if non-zero – the gbit-advertise word into register 9 (1000BASE-T control). Skipping the second write keeps 10/100-only PHYs from rejecting an unsupported register access.
| k_ra8_ok | Advertisement programmed. |
Definition at line 192 of file ra8_rmac_phy.c.
References k_ra8_ok, k_ra8_rmac_phy_reg_1000t_ctrl, k_ra8_rmac_phy_reg_an_advert, and s_state.
Referenced by ra8_rmac_phy_open().
|
static |
Issue BMCR.RESET to the PHY and poll until it self-clears.
IEEE 802.3 Clause 22 6.3.5.2.5 requires BMCR bit 15 (RESET) to be write-only; the PHY clears it once internal state has been re-initialized. Polls up to poll_max MDIO reads before giving up.
| [in] | poll_max | Maximum read iterations before timing out. |
| k_ra8_ok | BMCR.RESET observed clear. |
| k_ra8_err_hw_timeout | BMCR.RESET still set after poll_max reads. |
Definition at line 149 of file ra8_rmac_phy.c.
References k_ra8_err_hw_timeout, k_ra8_ok, k_ra8_rmac_phy_bmcr_reset, k_ra8_rmac_phy_reg_control, and s_state.
|
static |
Resolve the negotiated speed once the link is up and AN done.
Consults the 1000BASE-T status register (MSR, Clause-22 register 10) first when gigabit was advertised, then falls back to the partner ability word (LPA, register 5) for the 10/100 speeds. out->speed keeps its k_ra8_rmac_phy_speed_no_link preset when neither register yields a resolvable speed.
| [in,out] | out | Link snapshot being populated by the caller; speed / partner_ability are written here. |
Definition at line 442 of file ra8_rmac_phy.c.
References k_ra8_ok, k_ra8_rmac_phy_lpa_100full, k_ra8_rmac_phy_lpa_100half, k_ra8_rmac_phy_lpa_10full, k_ra8_rmac_phy_lpa_10half, k_ra8_rmac_phy_msr_1000full, k_ra8_rmac_phy_msr_1000half, k_ra8_rmac_phy_reg_1000t_status, k_ra8_rmac_phy_reg_an_partner, k_ra8_rmac_phy_speed_1000f, k_ra8_rmac_phy_speed_1000h, k_ra8_rmac_phy_speed_100f, k_ra8_rmac_phy_speed_100h, k_ra8_rmac_phy_speed_10f, k_ra8_rmac_phy_speed_10h, ra8_rmac_phy_link_t::partner_ability, priv_ra8_rmac_phy_internal_speed_ok(), s_state, and ra8_rmac_phy_link_t::speed.
Referenced by ra8_rmac_phy_link_status_get().
| bool priv_ra8_rmac_phy_internal_speed_ok | ( | ra8_err_t | err, |
| uint16_t | reg_value, | ||
| uint16_t | mask ) |
Pure speed-detected predicate – see header for full contract.
Pure predicate: PHY register read OK AND mask bit set.
Promoted helper so the line-352/356 AND can be driven under MC/DC.
| [in] | err | Result of the prior MIIM read. |
| [in] | reg_value | Register value just read. |
| [in] | mask | Speed-bit mask to test. |
| true | Speed bit set and read OK. |
| false | Otherwise. |
Definition at line 44 of file ra8_rmac_phy.c.
References k_ra8_ok.
Referenced by internal_resolve_speed().
|
nodiscard |
Restart auto-negotiation by writing BMCR.
Restart auto-negotiation on the PHY.
{ANE | ANR}.
Sets the AN-enable + AN-restart bits in the IEEE 802.3 Clause-22 BMCR (register 0). Useful after the link partner changes or after the local advertisement masks were updated.
| k_ra8_ok | Restart command issued. |
| k_ra8_err_not_initialized | ra8_rmac_phy_open not called. |
Definition at line 411 of file ra8_rmac_phy.c.
References k_ra8_err_not_initialized, k_ra8_rmac_phy_bmcr_an_enable, k_ra8_rmac_phy_bmcr_an_restart, k_ra8_rmac_phy_reg_control, and s_state.
|
nodiscard |
Close the PHY driver and forget all cached state.
Close the driver.
Marks the driver as closed so subsequent IO calls return k_ra8_err_not_initialized. The PHY itself is left in its current state (the caller may issue another reset via ra8_rmac_phy_open later).
| k_ra8_ok | Driver state cleared. |
| k_ra8_err_invalid_state | Driver was not open. |
Definition at line 307 of file ra8_rmac_phy.c.
References k_ra8_err_invalid_state, k_ra8_ok, and s_state.
|
nodiscard |
Read BMSR / 1000T-status / LPA and decode the link state.
Read BMSR + partner ability and resolve speed/duplex.
Reads BMSR (IEEE 802.3 register 1), then – if AN is complete and the link is up – consults the gigabit controller/peripheral status (MSR, register 10) and the partner ability (LPA, register 5) to map the advertised speeds to ra8_rmac_phy_speed_t.
| [out] | out | Receives the decoded link snapshot. |
| k_ra8_ok | *out populated. |
| k_ra8_err_null_ptr | out was NULL. |
| k_ra8_err_not_initialized | Driver not open. |
Definition at line 500 of file ra8_rmac_phy.c.
References ra8_rmac_phy_link_t::auto_neg_done, ra8_rmac_phy_link_t::bmsr, internal_resolve_speed(), k_ra8_err_not_initialized, k_ra8_ok, k_ra8_rmac_phy_bmsr_an_complete, k_ra8_rmac_phy_bmsr_link_up, k_ra8_rmac_phy_reg_status, k_ra8_rmac_phy_speed_no_link, ra8_rmac_phy_link_t::link_up, ra8_rmac_phy_link_t::partner_ability, RA8_CHECK_NULL_PTR, s_state, s_tag, and ra8_rmac_phy_link_t::speed.
|
nodiscard |
Return the LSI tag the caller passed to ra8_rmac_phy_open.
Get the currently-bound PHY-LSI identifier.
Echoes back the cached ra8_rmac_phy_lsi_t so higher-level stacks can branch on the on-board PHY model without re-querying MDIO.
| [out] | out | Receives the cached LSI tag. |
| k_ra8_ok | *out populated. |
| k_ra8_err_null_ptr | out was NULL. |
| k_ra8_err_not_initialized | Driver not open. |
Definition at line 548 of file ra8_rmac_phy.c.
References k_ra8_err_not_initialized, k_ra8_ok, RA8_CHECK_NULL_PTR, s_state, and s_tag.
|
nodiscard |
Read a 16-bit Clause-22 PHY register.
Read a Clause-22 register on the off-chip PHY.
Forwards the read to the injected io.read callback so the caller controls the actual MDIO transport. reg_addr must be in 0..31 (Clause-22 5-bit register space).
| [in] | reg_addr | Clause-22 register index 0..31. |
| [out] | out_data | Receives the 16-bit register value. |
| k_ra8_ok | Read completed. |
| k_ra8_err_null_ptr | out_data was NULL. |
| k_ra8_err_not_initialized | ra8_rmac_phy_open not called. |
| k_ra8_err_invalid_arg | reg_addr > 31. |
Definition at line 342 of file ra8_rmac_phy.c.
References k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_rmac_phy_reg_max, RA8_CHECK_NULL_PTR, s_state, and s_tag.
|
nodiscard |
Write a 16-bit Clause-22 PHY register.
Write a Clause-22 register on the off-chip PHY.
Forwards the write to the injected io.write callback. The PHY may treat the write as set-and-forget or as a self-clearing strobe depending on the register (e.g. BMCR.RESET).
| [in] | reg_addr | Clause-22 register index 0..31. |
| [in] | data | Value to write. |
| k_ra8_ok | Write completed. |
| k_ra8_err_not_initialized | ra8_rmac_phy_open not called. |
| k_ra8_err_invalid_arg | reg_addr > 31. |
Definition at line 379 of file ra8_rmac_phy.c.
References k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_rmac_phy_reg_max, and s_state.
|
nodiscard |
Open the RMAC PHY driver and bring the off-chip PHY out of reset.
Open the PHY and run a soft-reset sequence.
Validates the caller-supplied configuration, captures the MDIO IO seam (read/write function pointers) into module-private state, issues a soft reset over MDIO and polls BMCR.RESET to clear, then programs the advertised abilities into ANAR (and 1000BASE-T control where the LSI supports gigabit). Subsequent ra8_rmac_phy_* calls operate against the cached state; only one open instance is permitted at a time.
Algorithm:
| [in] | cfg | PHY configuration: IO seam, MDIO address (0..31), LSI family, advertised abilities, and reset poll budget. Must not be nullptr; cfg->io.read and cfg->io.write must be non-null. |
| k_ra8_ok | PHY reset and advertise programmed. |
| k_ra8_err_invalid_arg | cfg/io pointers null, phy_address out of range, or unknown lsi_type. |
| k_ra8_err_exists | Driver already opened; close first. |
| k_ra8_err_io | MDIO write to BMCR/ANAR failed. |
| k_ra8_err_timeout | BMCR.RESET did not clear within budget. |
Definition at line 251 of file ra8_rmac_phy.c.
References ra8_rmac_phy_cfg_t::gbit_advertise, internal_open_validate(), internal_program_advertise(), internal_reset_and_wait(), ra8_rmac_phy_cfg_t::io, k_ra8_err_exists, k_ra8_ok, k_ra8_rmac_phy_reset_poll_max, ra8_rmac_phy_cfg_t::local_advertise, ra8_rmac_phy_cfg_t::lsi_type, ra8_rmac_phy_cfg_t::phy_address, ra8_log_info_val, ra8_rmac_phy_cfg_t::reset_poll_max, s_state, and s_tag.
|
static |
Definition at line 85 of file ra8_rmac_phy.c.
|
static |
Definition at line 49 of file ra8_rmac_phy.c.