github.com/usbarmory/tamago@v0.0.0-20240508072735-8612bbe1e454/soc/nxp/enet/mii.go (about)

     1  // NXP 10/100-Mbps Ethernet MAC (ENET)
     2  // https://github.com/usbarmory/tamago
     3  //
     4  // Copyright (c) WithSecure Corporation
     5  // https://foundry.withsecure.com
     6  //
     7  // Use of this source code is governed by the license
     8  // that can be found in the LICENSE file.
     9  
    10  package enet
    11  
    12  import (
    13  	"github.com/usbarmory/tamago/bits"
    14  	"github.com/usbarmory/tamago/internal/reg"
    15  )
    16  
    17  const (
    18  	// IEEE 802.3-2008 Clause 22
    19  	MDIO_ST       = 0b01
    20  	MDIO_OP_READ  = 0b10
    21  	MDIO_OP_WRITE = 0b01
    22  	MDIO_TA       = 0b10
    23  
    24  	// IEEE 802.3-2008 Clause 45
    25  	MDIO_45_ST          = 0b00
    26  	MDIO_45_OP_ADDR     = 0b00
    27  	MDIO_45_OP_WRITE    = 0b01
    28  	MDIO_45_OP_READ_INC = 0b10
    29  	MDIO_45_OP_READ     = 0b11
    30  	MDIO_45_TA          = 0b10
    31  )
    32  
    33  func mdio(st, op, pa, ra, ta uint32, data uint16) (frame uint32) {
    34  	bits.SetN(&frame, MMFR_ST, 0b11, st)
    35  	bits.SetN(&frame, MMFR_OP, 0b11, op)
    36  	bits.SetN(&frame, MMFR_PA, 0x1f, pa)
    37  	bits.SetN(&frame, MMFR_RA, 0x1f, ra)
    38  	bits.SetN(&frame, MMFR_TA, 0b11, ta)
    39  	bits.SetN(&frame, MMFR_DATA, 0xffff, uint32(data))
    40  
    41  	return
    42  }
    43  
    44  // MDIO22 transmits an MII frame (IEEE 802.3-2008 Clause 22) to a connected
    45  // Ethernet PHY, the transacted frame is returned.
    46  func (hw *ENET) MDIO22(op, pa, ra int, data uint16) (frame uint32) {
    47  	reg.Set(hw.eir, IRQ_MII)
    48  	defer reg.Set(hw.eir, IRQ_MII)
    49  
    50  	frame = mdio(MDIO_ST, uint32(op), uint32(pa), uint32(ra), MDIO_TA, data)
    51  	reg.Write(hw.mmfr, frame)
    52  
    53  	reg.Wait(hw.eir, IRQ_MII, 1, 1)
    54  	return reg.Read(hw.mmfr)
    55  }
    56  
    57  // MDIO45 transmits an MII frame (IEEE 802.3-2008 Clause 45) to a connected
    58  // Ethernet PHY, the transacted frame is returned.
    59  func (hw *ENET) MDIO45(op, prtad, devad int, data uint16) (frame uint32) {
    60  	reg.Set(hw.eir, IRQ_MII)
    61  	defer reg.Set(hw.eir, IRQ_MII)
    62  
    63  	frame = mdio(MDIO_45_ST, uint32(op), uint32(prtad), uint32(devad), MDIO_45_TA, data)
    64  	reg.Write(hw.mmfr, frame)
    65  
    66  	reg.Wait(hw.eir, IRQ_MII, 1, 1)
    67  	return reg.Read(hw.mmfr)
    68  }
    69  
    70  // ReadPHYRegister reads a standard management register of a connected Ethernet
    71  // PHY (IEE 802.3-2008 Clause 22).
    72  func (hw *ENET) ReadPHYRegister(pa int, ra int) (data uint16) {
    73  	return uint16(hw.MDIO22(MDIO_OP_READ, pa, ra, 0))
    74  }
    75  
    76  // WritePHYRegister writes a standard management register of a connected
    77  // Ethernet PHY (IEE 802.3-2008 Clause 22).
    78  func (hw *ENET) WritePHYRegister(pa int, ra int, data uint16) {
    79  	hw.MDIO22(MDIO_OP_WRITE, pa, ra, data)
    80  }