Lines Matching +full:t +full:- +full:phy

1 // SPDX-License-Identifier: GPL-2.0
5 //! Network PHY device.
7 //! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h).
14 /// PHY state machine states.
18 /// Some of PHY drivers access to the state of PHY's software state machine.
20 /// [`enum phy_state`]: srctree/include/linux/phy.h
23 /// PHY device and driver are not ready for anything.
25 /// PHY is ready to send and receive packets.
27 /// PHY is up, but no polling or interrupts are done.
29 /// PHY is up, but is in an error state.
31 /// PHY and attached device are ready to do work.
33 /// PHY is currently running.
35 /// PHY is up, but not currently plugged in.
37 /// PHY is performing a cable test.
43 /// PHY drivers get duplex information from hardware and update the current state.
45 /// PHY is in full-duplex mode.
47 /// PHY is in half-duplex mode.
49 /// PHY is in unknown duplex mode.
53 /// An instance of a PHY device.
57 /// A [`Device`] instance is created when a callback in [`Driver`] is executed. A PHY driver
62 /// - Referencing a `phy_device` using this struct asserts that you are in
64 /// - This struct always has a valid `self.0.mdio.dev`.
66 /// [`struct phy_device`]: srctree/include/linux/phy.h
82 /// - the pointer must point at a valid `phy_device`, and the caller
85 /// - `(*ptr).mdio.dev` must be a valid.
86 unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self {
94 /// Gets the id of the PHY.
95 pub fn phy_id(&self) -> u32 {
102 /// Gets the state of PHY state machine states.
103 pub fn state(&self) -> DeviceState {
126 pub fn is_link_up(&self) -> bool {
136 /// Gets the current auto-negotiation configuration.
138 /// It returns true if auto-negotiation is enabled.
139 pub fn is_autoneg_enabled(&self) -> bool {
148 /// Gets the current auto-negotiation state.
150 /// It returns true if auto-negotiation is completed.
151 pub fn is_autoneg_completed(&self) -> bool {
161 /// Sets the speed of the PHY.
182 /// Reads a PHY register.
184 pub fn read<R: reg::Register>(&mut self, reg: R) -> Result<u16> {
188 /// Writes a PHY register.
189 pub fn write<R: reg::Register>(&mut self, reg: R, val: u16) -> Result {
194 pub fn read_paged(&mut self, page: u16, regnum: u16) -> Result<u16> {
203 /// Resolves the advertisements into PHY settings.
211 /// Executes software reset the PHY via `BMCR_RESET` bit.
212 pub fn genphy_soft_reset(&mut self) -> Result {
219 /// Initializes the PHY.
220 pub fn init_hw(&mut self) -> Result {
227 /// Starts auto-negotiation.
228 pub fn start_aneg(&mut self) -> Result {
235 /// Resumes the PHY via `BMCR_PDOWN` bit.
236 pub fn genphy_resume(&mut self) -> Result {
243 /// Suspends the PHY via `BMCR_PDOWN` bit.
244 pub fn genphy_suspend(&mut self) -> Result {
252 pub fn genphy_read_status<R: reg::Register>(&mut self) -> Result<u16> {
257 pub fn genphy_update_link(&mut self) -> Result {
265 pub fn genphy_read_lpa(&mut self) -> Result {
272 /// Reads PHY abilities.
273 pub fn genphy_read_abilities(&mut self) -> Result {
282 fn as_ref(&self) -> &kernel::device::Device {
289 /// Defines certain other features this PHY supports (like interrupts).
293 /// PHY is internal.
295 /// PHY needs to be reset after the refclk is enabled.
297 /// Polling is used to detect PHY status changes.
299 /// Don't suspend.
303 /// An adapter for the registration of a PHY driver.
304 struct Adapter<T: Driver> {
305 _p: PhantomData<T>,
308 impl<T: Driver> Adapter<T> {
312 unsafe extern "C" fn soft_reset_callback(phydev: *mut bindings::phy_device) -> c_int {
315 // where we hold `phy_device->lock`, so the accessors on
318 T::soft_reset(dev)?;
326 unsafe extern "C" fn probe_callback(phydev: *mut bindings::phy_device) -> c_int {
333 T::probe(dev)?;
341 unsafe extern "C" fn get_features_callback(phydev: *mut bindings::phy_device) -> c_int {
344 // where we hold `phy_device->lock`, so the accessors on
347 T::get_features(dev)?;
355 unsafe extern "C" fn suspend_callback(phydev: *mut bindings::phy_device) -> c_int {
358 // `Device` are okay to call even though `phy_device->lock`
361 T::suspend(dev)?;
369 unsafe extern "C" fn resume_callback(phydev: *mut bindings::phy_device) -> c_int {
372 // `Device` are okay to call even though `phy_device->lock`
375 T::resume(dev)?;
383 unsafe extern "C" fn config_aneg_callback(phydev: *mut bindings::phy_device) -> c_int {
386 // where we hold `phy_device->lock`, so the accessors on
389 T::config_aneg(dev)?;
397 unsafe extern "C" fn read_status_callback(phydev: *mut bindings::phy_device) -> c_int {
400 // where we hold `phy_device->lock`, so the accessors on
403 T::read_status(dev)?;
414 ) -> c_int {
416 // where we hold `phy_device->lock`, so the accessors on
419 T::match_phy_device(dev).into()
429 ) -> i32 {
432 // where we hold `phy_device->lock`, so the accessors on
436 let ret = T::read_mmd(dev, devnum as u8, regnum)?;
449 ) -> i32 {
452 // where we hold `phy_device->lock`, so the accessors on
455 T::write_mmd(dev, devnum as u8, regnum, val)?;
465 // where we hold `phy_device->lock`, so the accessors on
468 T::link_change_notify(dev);
472 /// Driver structure for a particular PHY type.
475 /// This is used to register a driver for a particular PHY type with the kernel.
481 /// [`struct phy_driver`]: srctree/include/linux/phy.h
485 // SAFETY: `DriverVTable` doesn't expose any &self method to access internal data, so it's safe to
494 pub const fn create_phy_driver<T: Driver>() -> DriverVTable {
497 name: crate::str::as_char_ptr_in_const_context(T::NAME).cast_mut(),
498 flags: T::FLAGS,
499 phy_id: T::PHY_DEVICE_ID.id(),
500 phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(),
501 soft_reset: if T::HAS_SOFT_RESET {
502 Some(Adapter::<T>::soft_reset_callback)
506 probe: if T::HAS_PROBE {
507 Some(Adapter::<T>::probe_callback)
511 get_features: if T::HAS_GET_FEATURES {
512 Some(Adapter::<T>::get_features_callback)
516 match_phy_device: if T::HAS_MATCH_PHY_DEVICE {
517 Some(Adapter::<T>::match_phy_device_callback)
521 suspend: if T::HAS_SUSPEND {
522 Some(Adapter::<T>::suspend_callback)
526 resume: if T::HAS_RESUME {
527 Some(Adapter::<T>::resume_callback)
531 config_aneg: if T::HAS_CONFIG_ANEG {
532 Some(Adapter::<T>::config_aneg_callback)
536 read_status: if T::HAS_READ_STATUS {
537 Some(Adapter::<T>::read_status_callback)
541 read_mmd: if T::HAS_READ_MMD {
542 Some(Adapter::<T>::read_mmd_callback)
546 write_mmd: if T::HAS_WRITE_MMD {
547 Some(Adapter::<T>::write_mmd_callback)
551 link_change_notify: if T::HAS_LINK_CHANGE_NOTIFY {
552 Some(Adapter::<T>::link_change_notify_callback)
562 /// Driver implementation for a particular PHY type.
567 /// Defines certain other features this PHY supports.
571 /// The friendly name of this PHY type.
578 /// Issues a PHY software reset.
579 fn soft_reset(_dev: &mut Device) -> Result {
583 /// Sets up device-specific structures during discovery.
584 fn probe(_dev: &mut Device) -> Result {
589 fn get_features(_dev: &mut Device) -> Result {
595 fn match_phy_device(_dev: &Device) -> bool {
599 /// Configures the advertisement and resets auto-negotiation
600 /// if auto-negotiation is enabled.
601 fn config_aneg(_dev: &mut Device) -> Result {
606 fn read_status(_dev: &mut Device) -> Result<u16> {
611 fn suspend(_dev: &mut Device) -> Result {
616 fn resume(_dev: &mut Device) -> Result {
621 fn read_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16) -> Result<u16> {
626 fn write_mmd(_dev: &mut Device, _devnum: u8, _regnum: u16, _val: u16) -> Result {
634 /// Registration structure for PHY drivers.
650 /// Registers a PHY driver.
654 ) -> Result<Self> {
679 /// An identifier for PHY devices on an MDIO/MII bus.
682 /// PHY driver.
689 pub const fn new_with_exact_mask(id: u32) -> Self {
697 pub const fn new_with_model_mask(id: u32) -> Self {
705 pub const fn new_with_vendor_mask(id: u32) -> Self {
713 pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self {
721 pub const fn new_with_driver<T: Driver>() -> Self {
722 T::PHY_DEVICE_ID
725 /// Get the MDIO device's PHY ID.
726 pub const fn id(&self) -> u32 {
731 pub const fn mask_as_int(&self) -> u32 {
737 pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
760 const fn as_int(&self) -> u32 {
781 /// use kernel::net::phy::{self, DeviceId};
798 /// impl phy::Driver for PhySample {
800 /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
809 /// use kernel::net::phy::{self, DeviceId};
813 /// _reg: ::kernel::net::phy::Registration,
827 /// impl phy::Driver for PhySample {
829 /// const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x00000001);
833 /// static mut DRIVERS: [::kernel::net::phy::DriverVTable; 1] =
834 /// [::kernel::net::phy::create_phy_driver::<PhySample>()];
837 /// fn init(module: &'static ::kernel::ThisModule) -> Result<Self> {
839 /// let mut reg = ::kernel::net::phy::Registration::register(
850 /// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId, (), N> =
852 /// ::kernel::net::phy::DeviceId(
872 const TABLE: $crate::device_id::IdArray<$crate::net::phy::DeviceId, (), N> =
880 _reg: $crate::net::phy::Registration,
889 static mut DRIVERS: [$crate::net::phy::DriverVTable;
891 [$($crate::net::phy::create_phy_driver::<$driver>()),+];
894 fn init(module: &'static $crate::ThisModule) -> Result<Self> {
898 let mut reg = $crate::net::phy::Registration::register(