reg.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2024 FUJITA Tomonori <fujita.tomonori@gmail.com>
  3. //! PHY register interfaces.
  4. //!
  5. //! This module provides support for accessing PHY registers in the
  6. //! Ethernet management interface clauses 22 and 45 register namespaces, as
  7. //! defined in IEEE 802.3.
  8. use super::Device;
  9. use crate::build_assert;
  10. use crate::error::*;
  11. use crate::uapi;
  12. mod private {
  13. /// Marker that a trait cannot be implemented outside of this crate
  14. pub trait Sealed {}
  15. }
  16. /// Accesses PHY registers.
  17. ///
  18. /// This trait is used to implement the unified interface to access
  19. /// C22 and C45 PHY registers.
  20. ///
  21. /// # Examples
  22. ///
  23. /// ```ignore
  24. /// fn link_change_notify(dev: &mut Device) {
  25. /// // read C22 BMCR register
  26. /// dev.read(C22::BMCR);
  27. /// // read C45 PMA/PMD control 1 register
  28. /// dev.read(C45::new(Mmd::PMAPMD, 0));
  29. ///
  30. /// // Checks the link status as reported by registers in the C22 namespace
  31. /// // and updates current link state.
  32. /// dev.genphy_read_status::<phy::C22>();
  33. /// // Checks the link status as reported by registers in the C45 namespace
  34. /// // and updates current link state.
  35. /// dev.genphy_read_status::<phy::C45>();
  36. /// }
  37. /// ```
  38. pub trait Register: private::Sealed {
  39. /// Reads a PHY register.
  40. fn read(&self, dev: &mut Device) -> Result<u16>;
  41. /// Writes a PHY register.
  42. fn write(&self, dev: &mut Device, val: u16) -> Result;
  43. /// Checks the link status and updates current link state.
  44. fn read_status(dev: &mut Device) -> Result<u16>;
  45. }
  46. /// A single MDIO clause 22 register address (5 bits).
  47. #[derive(Copy, Clone, Debug)]
  48. pub struct C22(u8);
  49. impl C22 {
  50. /// Basic mode control.
  51. pub const BMCR: Self = C22(0x00);
  52. /// Basic mode status.
  53. pub const BMSR: Self = C22(0x01);
  54. /// PHY identifier 1.
  55. pub const PHYSID1: Self = C22(0x02);
  56. /// PHY identifier 2.
  57. pub const PHYSID2: Self = C22(0x03);
  58. /// Auto-negotiation advertisement.
  59. pub const ADVERTISE: Self = C22(0x04);
  60. /// Auto-negotiation link partner base page ability.
  61. pub const LPA: Self = C22(0x05);
  62. /// Auto-negotiation expansion.
  63. pub const EXPANSION: Self = C22(0x06);
  64. /// Auto-negotiation next page transmit.
  65. pub const NEXT_PAGE_TRANSMIT: Self = C22(0x07);
  66. /// Auto-negotiation link partner received next page.
  67. pub const LP_RECEIVED_NEXT_PAGE: Self = C22(0x08);
  68. /// Master-slave control.
  69. pub const MASTER_SLAVE_CONTROL: Self = C22(0x09);
  70. /// Master-slave status.
  71. pub const MASTER_SLAVE_STATUS: Self = C22(0x0a);
  72. /// PSE Control.
  73. pub const PSE_CONTROL: Self = C22(0x0b);
  74. /// PSE Status.
  75. pub const PSE_STATUS: Self = C22(0x0c);
  76. /// MMD Register control.
  77. pub const MMD_CONTROL: Self = C22(0x0d);
  78. /// MMD Register address data.
  79. pub const MMD_DATA: Self = C22(0x0e);
  80. /// Extended status.
  81. pub const EXTENDED_STATUS: Self = C22(0x0f);
  82. /// Creates a new instance of `C22` with a vendor specific register.
  83. pub const fn vendor_specific<const N: u8>() -> Self {
  84. build_assert!(
  85. N > 0x0f && N < 0x20,
  86. "Vendor-specific register address must be between 16 and 31"
  87. );
  88. C22(N)
  89. }
  90. }
  91. impl private::Sealed for C22 {}
  92. impl Register for C22 {
  93. fn read(&self, dev: &mut Device) -> Result<u16> {
  94. let phydev = dev.0.get();
  95. // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
  96. // So it's just an FFI call, open code of `phy_read()` with a valid `phy_device` pointer
  97. // `phydev`.
  98. let ret = unsafe {
  99. bindings::mdiobus_read((*phydev).mdio.bus, (*phydev).mdio.addr, self.0.into())
  100. };
  101. to_result(ret)?;
  102. Ok(ret as u16)
  103. }
  104. fn write(&self, dev: &mut Device, val: u16) -> Result {
  105. let phydev = dev.0.get();
  106. // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
  107. // So it's just an FFI call, open code of `phy_write()` with a valid `phy_device` pointer
  108. // `phydev`.
  109. to_result(unsafe {
  110. bindings::mdiobus_write((*phydev).mdio.bus, (*phydev).mdio.addr, self.0.into(), val)
  111. })
  112. }
  113. fn read_status(dev: &mut Device) -> Result<u16> {
  114. let phydev = dev.0.get();
  115. // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  116. // So it's just an FFI call.
  117. let ret = unsafe { bindings::genphy_read_status(phydev) };
  118. to_result(ret)?;
  119. Ok(ret as u16)
  120. }
  121. }
  122. /// A single MDIO clause 45 register device and address.
  123. #[derive(Copy, Clone, Debug)]
  124. pub struct Mmd(u8);
  125. impl Mmd {
  126. /// Physical Medium Attachment/Dependent.
  127. pub const PMAPMD: Self = Mmd(uapi::MDIO_MMD_PMAPMD as u8);
  128. /// WAN interface sublayer.
  129. pub const WIS: Self = Mmd(uapi::MDIO_MMD_WIS as u8);
  130. /// Physical coding sublayer.
  131. pub const PCS: Self = Mmd(uapi::MDIO_MMD_PCS as u8);
  132. /// PHY Extender sublayer.
  133. pub const PHYXS: Self = Mmd(uapi::MDIO_MMD_PHYXS as u8);
  134. /// DTE Extender sublayer.
  135. pub const DTEXS: Self = Mmd(uapi::MDIO_MMD_DTEXS as u8);
  136. /// Transmission convergence.
  137. pub const TC: Self = Mmd(uapi::MDIO_MMD_TC as u8);
  138. /// Auto negotiation.
  139. pub const AN: Self = Mmd(uapi::MDIO_MMD_AN as u8);
  140. /// Separated PMA (1).
  141. pub const SEPARATED_PMA1: Self = Mmd(8);
  142. /// Separated PMA (2).
  143. pub const SEPARATED_PMA2: Self = Mmd(9);
  144. /// Separated PMA (3).
  145. pub const SEPARATED_PMA3: Self = Mmd(10);
  146. /// Separated PMA (4).
  147. pub const SEPARATED_PMA4: Self = Mmd(11);
  148. /// OFDM PMA/PMD.
  149. pub const OFDM_PMAPMD: Self = Mmd(12);
  150. /// Power unit.
  151. pub const POWER_UNIT: Self = Mmd(13);
  152. /// Clause 22 extension.
  153. pub const C22_EXT: Self = Mmd(uapi::MDIO_MMD_C22EXT as u8);
  154. /// Vendor specific 1.
  155. pub const VEND1: Self = Mmd(uapi::MDIO_MMD_VEND1 as u8);
  156. /// Vendor specific 2.
  157. pub const VEND2: Self = Mmd(uapi::MDIO_MMD_VEND2 as u8);
  158. }
  159. /// A single MDIO clause 45 register device and address.
  160. ///
  161. /// Clause 45 uses a 5-bit device address to access a specific MMD within
  162. /// a port, then a 16-bit register address to access a location within
  163. /// that device. `C45` represents this by storing a [`Mmd`] and
  164. /// a register number.
  165. pub struct C45 {
  166. devad: Mmd,
  167. regnum: u16,
  168. }
  169. impl C45 {
  170. /// Creates a new instance of `C45`.
  171. pub fn new(devad: Mmd, regnum: u16) -> Self {
  172. Self { devad, regnum }
  173. }
  174. }
  175. impl private::Sealed for C45 {}
  176. impl Register for C45 {
  177. fn read(&self, dev: &mut Device) -> Result<u16> {
  178. let phydev = dev.0.get();
  179. // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
  180. // So it's just an FFI call.
  181. let ret =
  182. unsafe { bindings::phy_read_mmd(phydev, self.devad.0.into(), self.regnum.into()) };
  183. to_result(ret)?;
  184. Ok(ret as u16)
  185. }
  186. fn write(&self, dev: &mut Device, val: u16) -> Result {
  187. let phydev = dev.0.get();
  188. // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
  189. // So it's just an FFI call.
  190. to_result(unsafe {
  191. bindings::phy_write_mmd(phydev, self.devad.0.into(), self.regnum.into(), val)
  192. })
  193. }
  194. fn read_status(dev: &mut Device) -> Result<u16> {
  195. let phydev = dev.0.get();
  196. // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`.
  197. // So it's just an FFI call.
  198. let ret = unsafe { bindings::genphy_c45_read_status(phydev) };
  199. to_result(ret)?;
  200. Ok(ret as u16)
  201. }
  202. }