1*4882a593Smuzhiyun===================== 2*4882a593SmuzhiyunPHY Abstraction Layer 3*4882a593Smuzhiyun===================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunPurpose 6*4882a593Smuzhiyun======= 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunMost network devices consist of set of registers which provide an interface 9*4882a593Smuzhiyunto a MAC layer, which communicates with the physical connection through a 10*4882a593SmuzhiyunPHY. The PHY concerns itself with negotiating link parameters with the link 11*4882a593Smuzhiyunpartner on the other side of the network connection (typically, an ethernet 12*4882a593Smuzhiyuncable), and provides a register interface to allow drivers to determine what 13*4882a593Smuzhiyunsettings were chosen, and to configure what settings are allowed. 14*4882a593Smuzhiyun 15*4882a593SmuzhiyunWhile these devices are distinct from the network devices, and conform to a 16*4882a593Smuzhiyunstandard layout for the registers, it has been common practice to integrate 17*4882a593Smuzhiyunthe PHY management code with the network driver. This has resulted in large 18*4882a593Smuzhiyunamounts of redundant code. Also, on embedded systems with multiple (and 19*4882a593Smuzhiyunsometimes quite different) ethernet controllers connected to the same 20*4882a593Smuzhiyunmanagement bus, it is difficult to ensure safe use of the bus. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunSince the PHYs are devices, and the management busses through which they are 23*4882a593Smuzhiyunaccessed are, in fact, busses, the PHY Abstraction Layer treats them as such. 24*4882a593SmuzhiyunIn doing so, it has these goals: 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun#. Increase code-reuse 27*4882a593Smuzhiyun#. Increase overall code-maintainability 28*4882a593Smuzhiyun#. Speed development time for new network drivers, and for new systems 29*4882a593Smuzhiyun 30*4882a593SmuzhiyunBasically, this layer is meant to provide an interface to PHY devices which 31*4882a593Smuzhiyunallows network driver writers to write as little code as possible, while 32*4882a593Smuzhiyunstill providing a full feature set. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunThe MDIO bus 35*4882a593Smuzhiyun============ 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunMost network devices are connected to a PHY by means of a management bus. 38*4882a593SmuzhiyunDifferent devices use different busses (though some share common interfaces). 39*4882a593SmuzhiyunIn order to take advantage of the PAL, each bus interface needs to be 40*4882a593Smuzhiyunregistered as a distinct device. 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun#. read and write functions must be implemented. Their prototypes are:: 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun int write(struct mii_bus *bus, int mii_id, int regnum, u16 value); 45*4882a593Smuzhiyun int read(struct mii_bus *bus, int mii_id, int regnum); 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun mii_id is the address on the bus for the PHY, and regnum is the register 48*4882a593Smuzhiyun number. These functions are guaranteed not to be called from interrupt 49*4882a593Smuzhiyun time, so it is safe for them to block, waiting for an interrupt to signal 50*4882a593Smuzhiyun the operation is complete 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun#. A reset function is optional. This is used to return the bus to an 53*4882a593Smuzhiyun initialized state. 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun#. A probe function is needed. This function should set up anything the bus 56*4882a593Smuzhiyun driver needs, setup the mii_bus structure, and register with the PAL using 57*4882a593Smuzhiyun mdiobus_register. Similarly, there's a remove function to undo all of 58*4882a593Smuzhiyun that (use mdiobus_unregister). 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun#. Like any driver, the device_driver structure must be configured, and init 61*4882a593Smuzhiyun exit functions are used to register the driver. 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun#. The bus must also be declared somewhere as a device, and registered. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunAs an example for how one driver implemented an mdio bus driver, see 66*4882a593Smuzhiyundrivers/net/ethernet/freescale/fsl_pq_mdio.c and an associated DTS file 67*4882a593Smuzhiyunfor one of the users. (e.g. "git grep fsl,.*-mdio arch/powerpc/boot/dts/") 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun(RG)MII/electrical interface considerations 70*4882a593Smuzhiyun=========================================== 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunThe Reduced Gigabit Medium Independent Interface (RGMII) is a 12-pin 73*4882a593Smuzhiyunelectrical signal interface using a synchronous 125Mhz clock signal and several 74*4882a593Smuzhiyundata lines. Due to this design decision, a 1.5ns to 2ns delay must be added 75*4882a593Smuzhiyunbetween the clock line (RXC or TXC) and the data lines to let the PHY (clock 76*4882a593Smuzhiyunsink) have a large enough setup and hold time to sample the data lines correctly. The 77*4882a593SmuzhiyunPHY library offers different types of PHY_INTERFACE_MODE_RGMII* values to let 78*4882a593Smuzhiyunthe PHY driver and optionally the MAC driver, implement the required delay. The 79*4882a593Smuzhiyunvalues of phy_interface_t must be understood from the perspective of the PHY 80*4882a593Smuzhiyundevice itself, leading to the following: 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun* PHY_INTERFACE_MODE_RGMII: the PHY is not responsible for inserting any 83*4882a593Smuzhiyun internal delay by itself, it assumes that either the Ethernet MAC (if capable 84*4882a593Smuzhiyun or the PCB traces) insert the correct 1.5-2ns delay 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun* PHY_INTERFACE_MODE_RGMII_TXID: the PHY should insert an internal delay 87*4882a593Smuzhiyun for the transmit data lines (TXD[3:0]) processed by the PHY device 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun* PHY_INTERFACE_MODE_RGMII_RXID: the PHY should insert an internal delay 90*4882a593Smuzhiyun for the receive data lines (RXD[3:0]) processed by the PHY device 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun* PHY_INTERFACE_MODE_RGMII_ID: the PHY should insert internal delays for 93*4882a593Smuzhiyun both transmit AND receive data lines from/to the PHY device 94*4882a593Smuzhiyun 95*4882a593SmuzhiyunWhenever possible, use the PHY side RGMII delay for these reasons: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun* PHY devices may offer sub-nanosecond granularity in how they allow a 98*4882a593Smuzhiyun receiver/transmitter side delay (e.g: 0.5, 1.0, 1.5ns) to be specified. Such 99*4882a593Smuzhiyun precision may be required to account for differences in PCB trace lengths 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun* PHY devices are typically qualified for a large range of applications 102*4882a593Smuzhiyun (industrial, medical, automotive...), and they provide a constant and 103*4882a593Smuzhiyun reliable delay across temperature/pressure/voltage ranges 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun* PHY device drivers in PHYLIB being reusable by nature, being able to 106*4882a593Smuzhiyun configure correctly a specified delay enables more designs with similar delay 107*4882a593Smuzhiyun requirements to be operate correctly 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunFor cases where the PHY is not capable of providing this delay, but the 110*4882a593SmuzhiyunEthernet MAC driver is capable of doing so, the correct phy_interface_t value 111*4882a593Smuzhiyunshould be PHY_INTERFACE_MODE_RGMII, and the Ethernet MAC driver should be 112*4882a593Smuzhiyunconfigured correctly in order to provide the required transmit and/or receive 113*4882a593Smuzhiyunside delay from the perspective of the PHY device. Conversely, if the Ethernet 114*4882a593SmuzhiyunMAC driver looks at the phy_interface_t value, for any other mode but 115*4882a593SmuzhiyunPHY_INTERFACE_MODE_RGMII, it should make sure that the MAC-level delays are 116*4882a593Smuzhiyundisabled. 117*4882a593Smuzhiyun 118*4882a593SmuzhiyunIn case neither the Ethernet MAC, nor the PHY are capable of providing the 119*4882a593Smuzhiyunrequired delays, as defined per the RGMII standard, several options may be 120*4882a593Smuzhiyunavailable: 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun* Some SoCs may offer a pin pad/mux/controller capable of configuring a given 123*4882a593Smuzhiyun set of pins'strength, delays, and voltage; and it may be a suitable 124*4882a593Smuzhiyun option to insert the expected 2ns RGMII delay. 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun* Modifying the PCB design to include a fixed delay (e.g: using a specifically 127*4882a593Smuzhiyun designed serpentine), which may not require software configuration at all. 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunCommon problems with RGMII delay mismatch 130*4882a593Smuzhiyun----------------------------------------- 131*4882a593Smuzhiyun 132*4882a593SmuzhiyunWhen there is a RGMII delay mismatch between the Ethernet MAC and the PHY, this 133*4882a593Smuzhiyunwill most likely result in the clock and data line signals to be unstable when 134*4882a593Smuzhiyunthe PHY or MAC take a snapshot of these signals to translate them into logical 135*4882a593Smuzhiyun1 or 0 states and reconstruct the data being transmitted/received. Typical 136*4882a593Smuzhiyunsymptoms include: 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun* Transmission/reception partially works, and there is frequent or occasional 139*4882a593Smuzhiyun packet loss observed 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun* Ethernet MAC may report some or all packets ingressing with a FCS/CRC error, 142*4882a593Smuzhiyun or just discard them all 143*4882a593Smuzhiyun 144*4882a593Smuzhiyun* Switching to lower speeds such as 10/100Mbits/sec makes the problem go away 145*4882a593Smuzhiyun (since there is enough setup/hold time in that case) 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunConnecting to a PHY 148*4882a593Smuzhiyun=================== 149*4882a593Smuzhiyun 150*4882a593SmuzhiyunSometime during startup, the network driver needs to establish a connection 151*4882a593Smuzhiyunbetween the PHY device, and the network device. At this time, the PHY's bus 152*4882a593Smuzhiyunand drivers need to all have been loaded, so it is ready for the connection. 153*4882a593SmuzhiyunAt this point, there are several ways to connect to the PHY: 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun#. The PAL handles everything, and only calls the network driver when 156*4882a593Smuzhiyun the link state changes, so it can react. 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun#. The PAL handles everything except interrupts (usually because the 159*4882a593Smuzhiyun controller has the interrupt registers). 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun#. The PAL handles everything, but checks in with the driver every second, 162*4882a593Smuzhiyun allowing the network driver to react first to any changes before the PAL 163*4882a593Smuzhiyun does. 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun#. The PAL serves only as a library of functions, with the network device 166*4882a593Smuzhiyun manually calling functions to update status, and configure the PHY 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunLetting the PHY Abstraction Layer do Everything 170*4882a593Smuzhiyun=============================================== 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunIf you choose option 1 (The hope is that every driver can, but to still be 173*4882a593Smuzhiyunuseful to drivers that can't), connecting to the PHY is simple: 174*4882a593Smuzhiyun 175*4882a593SmuzhiyunFirst, you need a function to react to changes in the link state. This 176*4882a593Smuzhiyunfunction follows this protocol:: 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun static void adjust_link(struct net_device *dev); 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunNext, you need to know the device name of the PHY connected to this device. 181*4882a593SmuzhiyunThe name will look something like, "0:00", where the first number is the 182*4882a593Smuzhiyunbus id, and the second is the PHY's address on that bus. Typically, 183*4882a593Smuzhiyunthe bus is responsible for making its ID unique. 184*4882a593Smuzhiyun 185*4882a593SmuzhiyunNow, to connect, just call this function:: 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun phydev = phy_connect(dev, phy_name, &adjust_link, interface); 188*4882a593Smuzhiyun 189*4882a593Smuzhiyun*phydev* is a pointer to the phy_device structure which represents the PHY. 190*4882a593SmuzhiyunIf phy_connect is successful, it will return the pointer. dev, here, is the 191*4882a593Smuzhiyunpointer to your net_device. Once done, this function will have started the 192*4882a593SmuzhiyunPHY's software state machine, and registered for the PHY's interrupt, if it 193*4882a593Smuzhiyunhas one. The phydev structure will be populated with information about the 194*4882a593Smuzhiyuncurrent state, though the PHY will not yet be truly operational at this 195*4882a593Smuzhiyunpoint. 196*4882a593Smuzhiyun 197*4882a593SmuzhiyunPHY-specific flags should be set in phydev->dev_flags prior to the call 198*4882a593Smuzhiyunto phy_connect() such that the underlying PHY driver can check for flags 199*4882a593Smuzhiyunand perform specific operations based on them. 200*4882a593SmuzhiyunThis is useful if the system has put hardware restrictions on 201*4882a593Smuzhiyunthe PHY/controller, of which the PHY needs to be aware. 202*4882a593Smuzhiyun 203*4882a593Smuzhiyun*interface* is a u32 which specifies the connection type used 204*4882a593Smuzhiyunbetween the controller and the PHY. Examples are GMII, MII, 205*4882a593SmuzhiyunRGMII, and SGMII. See "PHY interface mode" below. For a full 206*4882a593Smuzhiyunlist, see include/linux/phy.h 207*4882a593Smuzhiyun 208*4882a593SmuzhiyunNow just make sure that phydev->supported and phydev->advertising have any 209*4882a593Smuzhiyunvalues pruned from them which don't make sense for your controller (a 10/100 210*4882a593Smuzhiyuncontroller may be connected to a gigabit capable PHY, so you would need to 211*4882a593Smuzhiyunmask off SUPPORTED_1000baseT*). See include/linux/ethtool.h for definitions 212*4882a593Smuzhiyunfor these bitfields. Note that you should not SET any bits, except the 213*4882a593SmuzhiyunSUPPORTED_Pause and SUPPORTED_AsymPause bits (see below), or the PHY may get 214*4882a593Smuzhiyunput into an unsupported state. 215*4882a593Smuzhiyun 216*4882a593SmuzhiyunLastly, once the controller is ready to handle network traffic, you call 217*4882a593Smuzhiyunphy_start(phydev). This tells the PAL that you are ready, and configures the 218*4882a593SmuzhiyunPHY to connect to the network. If the MAC interrupt of your network driver 219*4882a593Smuzhiyunalso handles PHY status changes, just set phydev->irq to PHY_IGNORE_INTERRUPT 220*4882a593Smuzhiyunbefore you call phy_start and use phy_mac_interrupt() from the network 221*4882a593Smuzhiyundriver. If you don't want to use interrupts, set phydev->irq to PHY_POLL. 222*4882a593Smuzhiyunphy_start() enables the PHY interrupts (if applicable) and starts the 223*4882a593Smuzhiyunphylib state machine. 224*4882a593Smuzhiyun 225*4882a593SmuzhiyunWhen you want to disconnect from the network (even if just briefly), you call 226*4882a593Smuzhiyunphy_stop(phydev). This function also stops the phylib state machine and 227*4882a593Smuzhiyundisables PHY interrupts. 228*4882a593Smuzhiyun 229*4882a593SmuzhiyunPHY interface modes 230*4882a593Smuzhiyun=================== 231*4882a593Smuzhiyun 232*4882a593SmuzhiyunThe PHY interface mode supplied in the phy_connect() family of functions 233*4882a593Smuzhiyundefines the initial operating mode of the PHY interface. This is not 234*4882a593Smuzhiyunguaranteed to remain constant; there are PHYs which dynamically change 235*4882a593Smuzhiyuntheir interface mode without software interaction depending on the 236*4882a593Smuzhiyunnegotiation results. 237*4882a593Smuzhiyun 238*4882a593SmuzhiyunSome of the interface modes are described below: 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun``PHY_INTERFACE_MODE_1000BASEX`` 241*4882a593Smuzhiyun This defines the 1000BASE-X single-lane serdes link as defined by the 242*4882a593Smuzhiyun 802.3 standard section 36. The link operates at a fixed bit rate of 243*4882a593Smuzhiyun 1.25Gbaud using a 10B/8B encoding scheme, resulting in an underlying 244*4882a593Smuzhiyun data rate of 1Gbps. Embedded in the data stream is a 16-bit control 245*4882a593Smuzhiyun word which is used to negotiate the duplex and pause modes with the 246*4882a593Smuzhiyun remote end. This does not include "up-clocked" variants such as 2.5Gbps 247*4882a593Smuzhiyun speeds (see below.) 248*4882a593Smuzhiyun 249*4882a593Smuzhiyun``PHY_INTERFACE_MODE_2500BASEX`` 250*4882a593Smuzhiyun This defines a variant of 1000BASE-X which is clocked 2.5 times as fast 251*4882a593Smuzhiyun as the 802.3 standard, giving a fixed bit rate of 3.125Gbaud. 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun``PHY_INTERFACE_MODE_SGMII`` 254*4882a593Smuzhiyun This is used for Cisco SGMII, which is a modification of 1000BASE-X 255*4882a593Smuzhiyun as defined by the 802.3 standard. The SGMII link consists of a single 256*4882a593Smuzhiyun serdes lane running at a fixed bit rate of 1.25Gbaud with 10B/8B 257*4882a593Smuzhiyun encoding. The underlying data rate is 1Gbps, with the slower speeds of 258*4882a593Smuzhiyun 100Mbps and 10Mbps being achieved through replication of each data symbol. 259*4882a593Smuzhiyun The 802.3 control word is re-purposed to send the negotiated speed and 260*4882a593Smuzhiyun duplex information from to the MAC, and for the MAC to acknowledge 261*4882a593Smuzhiyun receipt. This does not include "up-clocked" variants such as 2.5Gbps 262*4882a593Smuzhiyun speeds. 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun Note: mismatched SGMII vs 1000BASE-X configuration on a link can 265*4882a593Smuzhiyun successfully pass data in some circumstances, but the 16-bit control 266*4882a593Smuzhiyun word will not be correctly interpreted, which may cause mismatches in 267*4882a593Smuzhiyun duplex, pause or other settings. This is dependent on the MAC and/or 268*4882a593Smuzhiyun PHY behaviour. 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun``PHY_INTERFACE_MODE_10GBASER`` 271*4882a593Smuzhiyun This is the IEEE 802.3 Clause 49 defined 10GBASE-R protocol used with 272*4882a593Smuzhiyun various different mediums. Please refer to the IEEE standard for a 273*4882a593Smuzhiyun definition of this. 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun Note: 10GBASE-R is just one protocol that can be used with XFI and SFI. 276*4882a593Smuzhiyun XFI and SFI permit multiple protocols over a single SERDES lane, and 277*4882a593Smuzhiyun also defines the electrical characteristics of the signals with a host 278*4882a593Smuzhiyun compliance board plugged into the host XFP/SFP connector. Therefore, 279*4882a593Smuzhiyun XFI and SFI are not PHY interface types in their own right. 280*4882a593Smuzhiyun 281*4882a593Smuzhiyun``PHY_INTERFACE_MODE_10GKR`` 282*4882a593Smuzhiyun This is the IEEE 802.3 Clause 49 defined 10GBASE-R with Clause 73 283*4882a593Smuzhiyun autonegotiation. Please refer to the IEEE standard for further 284*4882a593Smuzhiyun information. 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun Note: due to legacy usage, some 10GBASE-R usage incorrectly makes 287*4882a593Smuzhiyun use of this definition. 288*4882a593Smuzhiyun 289*4882a593SmuzhiyunPause frames / flow control 290*4882a593Smuzhiyun=========================== 291*4882a593Smuzhiyun 292*4882a593SmuzhiyunThe PHY does not participate directly in flow control/pause frames except by 293*4882a593Smuzhiyunmaking sure that the SUPPORTED_Pause and SUPPORTED_AsymPause bits are set in 294*4882a593SmuzhiyunMII_ADVERTISE to indicate towards the link partner that the Ethernet MAC 295*4882a593Smuzhiyuncontroller supports such a thing. Since flow control/pause frames generation 296*4882a593Smuzhiyuninvolves the Ethernet MAC driver, it is recommended that this driver takes care 297*4882a593Smuzhiyunof properly indicating advertisement and support for such features by setting 298*4882a593Smuzhiyunthe SUPPORTED_Pause and SUPPORTED_AsymPause bits accordingly. This can be done 299*4882a593Smuzhiyuneither before or after phy_connect() and/or as a result of implementing the 300*4882a593Smuzhiyunethtool::set_pauseparam feature. 301*4882a593Smuzhiyun 302*4882a593Smuzhiyun 303*4882a593SmuzhiyunKeeping Close Tabs on the PAL 304*4882a593Smuzhiyun============================= 305*4882a593Smuzhiyun 306*4882a593SmuzhiyunIt is possible that the PAL's built-in state machine needs a little help to 307*4882a593Smuzhiyunkeep your network device and the PHY properly in sync. If so, you can 308*4882a593Smuzhiyunregister a helper function when connecting to the PHY, which will be called 309*4882a593Smuzhiyunevery second before the state machine reacts to any changes. To do this, you 310*4882a593Smuzhiyunneed to manually call phy_attach() and phy_prepare_link(), and then call 311*4882a593Smuzhiyunphy_start_machine() with the second argument set to point to your special 312*4882a593Smuzhiyunhandler. 313*4882a593Smuzhiyun 314*4882a593SmuzhiyunCurrently there are no examples of how to use this functionality, and testing 315*4882a593Smuzhiyunon it has been limited because the author does not have any drivers which use 316*4882a593Smuzhiyunit (they all use option 1). So Caveat Emptor. 317*4882a593Smuzhiyun 318*4882a593SmuzhiyunDoing it all yourself 319*4882a593Smuzhiyun===================== 320*4882a593Smuzhiyun 321*4882a593SmuzhiyunThere's a remote chance that the PAL's built-in state machine cannot track 322*4882a593Smuzhiyunthe complex interactions between the PHY and your network device. If this is 323*4882a593Smuzhiyunso, you can simply call phy_attach(), and not call phy_start_machine or 324*4882a593Smuzhiyunphy_prepare_link(). This will mean that phydev->state is entirely yours to 325*4882a593Smuzhiyunhandle (phy_start and phy_stop toggle between some of the states, so you 326*4882a593Smuzhiyunmight need to avoid them). 327*4882a593Smuzhiyun 328*4882a593SmuzhiyunAn effort has been made to make sure that useful functionality can be 329*4882a593Smuzhiyunaccessed without the state-machine running, and most of these functions are 330*4882a593Smuzhiyundescended from functions which did not interact with a complex state-machine. 331*4882a593SmuzhiyunHowever, again, no effort has been made so far to test running without the 332*4882a593Smuzhiyunstate machine, so tryer beware. 333*4882a593Smuzhiyun 334*4882a593SmuzhiyunHere is a brief rundown of the functions:: 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun int phy_read(struct phy_device *phydev, u16 regnum); 337*4882a593Smuzhiyun int phy_write(struct phy_device *phydev, u16 regnum, u16 val); 338*4882a593Smuzhiyun 339*4882a593SmuzhiyunSimple read/write primitives. They invoke the bus's read/write function 340*4882a593Smuzhiyunpointers. 341*4882a593Smuzhiyun:: 342*4882a593Smuzhiyun 343*4882a593Smuzhiyun void phy_print_status(struct phy_device *phydev); 344*4882a593Smuzhiyun 345*4882a593SmuzhiyunA convenience function to print out the PHY status neatly. 346*4882a593Smuzhiyun:: 347*4882a593Smuzhiyun 348*4882a593Smuzhiyun void phy_request_interrupt(struct phy_device *phydev); 349*4882a593Smuzhiyun 350*4882a593SmuzhiyunRequests the IRQ for the PHY interrupts. 351*4882a593Smuzhiyun:: 352*4882a593Smuzhiyun 353*4882a593Smuzhiyun struct phy_device * phy_attach(struct net_device *dev, const char *phy_id, 354*4882a593Smuzhiyun phy_interface_t interface); 355*4882a593Smuzhiyun 356*4882a593SmuzhiyunAttaches a network device to a particular PHY, binding the PHY to a generic 357*4882a593Smuzhiyundriver if none was found during bus initialization. 358*4882a593Smuzhiyun:: 359*4882a593Smuzhiyun 360*4882a593Smuzhiyun int phy_start_aneg(struct phy_device *phydev); 361*4882a593Smuzhiyun 362*4882a593SmuzhiyunUsing variables inside the phydev structure, either configures advertising 363*4882a593Smuzhiyunand resets autonegotiation, or disables autonegotiation, and configures 364*4882a593Smuzhiyunforced settings. 365*4882a593Smuzhiyun:: 366*4882a593Smuzhiyun 367*4882a593Smuzhiyun static inline int phy_read_status(struct phy_device *phydev); 368*4882a593Smuzhiyun 369*4882a593SmuzhiyunFills the phydev structure with up-to-date information about the current 370*4882a593Smuzhiyunsettings in the PHY. 371*4882a593Smuzhiyun:: 372*4882a593Smuzhiyun 373*4882a593Smuzhiyun int phy_ethtool_ksettings_set(struct phy_device *phydev, 374*4882a593Smuzhiyun const struct ethtool_link_ksettings *cmd); 375*4882a593Smuzhiyun 376*4882a593SmuzhiyunEthtool convenience functions. 377*4882a593Smuzhiyun:: 378*4882a593Smuzhiyun 379*4882a593Smuzhiyun int phy_mii_ioctl(struct phy_device *phydev, 380*4882a593Smuzhiyun struct mii_ioctl_data *mii_data, int cmd); 381*4882a593Smuzhiyun 382*4882a593SmuzhiyunThe MII ioctl. Note that this function will completely screw up the state 383*4882a593Smuzhiyunmachine if you write registers like BMCR, BMSR, ADVERTISE, etc. Best to 384*4882a593Smuzhiyunuse this only to write registers which are not standard, and don't set off 385*4882a593Smuzhiyuna renegotiation. 386*4882a593Smuzhiyun 387*4882a593SmuzhiyunPHY Device Drivers 388*4882a593Smuzhiyun================== 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunWith the PHY Abstraction Layer, adding support for new PHYs is 391*4882a593Smuzhiyunquite easy. In some cases, no work is required at all! However, 392*4882a593Smuzhiyunmany PHYs require a little hand-holding to get up-and-running. 393*4882a593Smuzhiyun 394*4882a593SmuzhiyunGeneric PHY driver 395*4882a593Smuzhiyun------------------ 396*4882a593Smuzhiyun 397*4882a593SmuzhiyunIf the desired PHY doesn't have any errata, quirks, or special 398*4882a593Smuzhiyunfeatures you want to support, then it may be best to not add 399*4882a593Smuzhiyunsupport, and let the PHY Abstraction Layer's Generic PHY Driver 400*4882a593Smuzhiyundo all of the work. 401*4882a593Smuzhiyun 402*4882a593SmuzhiyunWriting a PHY driver 403*4882a593Smuzhiyun-------------------- 404*4882a593Smuzhiyun 405*4882a593SmuzhiyunIf you do need to write a PHY driver, the first thing to do is 406*4882a593Smuzhiyunmake sure it can be matched with an appropriate PHY device. 407*4882a593SmuzhiyunThis is done during bus initialization by reading the device's 408*4882a593SmuzhiyunUID (stored in registers 2 and 3), then comparing it to each 409*4882a593Smuzhiyundriver's phy_id field by ANDing it with each driver's 410*4882a593Smuzhiyunphy_id_mask field. Also, it needs a name. Here's an example:: 411*4882a593Smuzhiyun 412*4882a593Smuzhiyun static struct phy_driver dm9161_driver = { 413*4882a593Smuzhiyun .phy_id = 0x0181b880, 414*4882a593Smuzhiyun .name = "Davicom DM9161E", 415*4882a593Smuzhiyun .phy_id_mask = 0x0ffffff0, 416*4882a593Smuzhiyun ... 417*4882a593Smuzhiyun } 418*4882a593Smuzhiyun 419*4882a593SmuzhiyunNext, you need to specify what features (speed, duplex, autoneg, 420*4882a593Smuzhiyunetc) your PHY device and driver support. Most PHYs support 421*4882a593SmuzhiyunPHY_BASIC_FEATURES, but you can look in include/mii.h for other 422*4882a593Smuzhiyunfeatures. 423*4882a593Smuzhiyun 424*4882a593SmuzhiyunEach driver consists of a number of function pointers, documented 425*4882a593Smuzhiyunin include/linux/phy.h under the phy_driver structure. 426*4882a593Smuzhiyun 427*4882a593SmuzhiyunOf these, only config_aneg and read_status are required to be 428*4882a593Smuzhiyunassigned by the driver code. The rest are optional. Also, it is 429*4882a593Smuzhiyunpreferred to use the generic phy driver's versions of these two 430*4882a593Smuzhiyunfunctions if at all possible: genphy_read_status and 431*4882a593Smuzhiyungenphy_config_aneg. If this is not possible, it is likely that 432*4882a593Smuzhiyunyou only need to perform some actions before and after invoking 433*4882a593Smuzhiyunthese functions, and so your functions will wrap the generic 434*4882a593Smuzhiyunones. 435*4882a593Smuzhiyun 436*4882a593SmuzhiyunFeel free to look at the Marvell, Cicada, and Davicom drivers in 437*4882a593Smuzhiyundrivers/net/phy/ for examples (the lxt and qsemi drivers have 438*4882a593Smuzhiyunnot been tested as of this writing). 439*4882a593Smuzhiyun 440*4882a593SmuzhiyunThe PHY's MMD register accesses are handled by the PAL framework 441*4882a593Smuzhiyunby default, but can be overridden by a specific PHY driver if 442*4882a593Smuzhiyunrequired. This could be the case if a PHY was released for 443*4882a593Smuzhiyunmanufacturing before the MMD PHY register definitions were 444*4882a593Smuzhiyunstandardized by the IEEE. Most modern PHYs will be able to use 445*4882a593Smuzhiyunthe generic PAL framework for accessing the PHY's MMD registers. 446*4882a593SmuzhiyunAn example of such usage is for Energy Efficient Ethernet support, 447*4882a593Smuzhiyunimplemented in the PAL. This support uses the PAL to access MMD 448*4882a593Smuzhiyunregisters for EEE query and configuration if the PHY supports 449*4882a593Smuzhiyunthe IEEE standard access mechanisms, or can use the PHY's specific 450*4882a593Smuzhiyunaccess interfaces if overridden by the specific PHY driver. See 451*4882a593Smuzhiyunthe Micrel driver in drivers/net/phy/ for an example of how this 452*4882a593Smuzhiyuncan be implemented. 453*4882a593Smuzhiyun 454*4882a593SmuzhiyunBoard Fixups 455*4882a593Smuzhiyun============ 456*4882a593Smuzhiyun 457*4882a593SmuzhiyunSometimes the specific interaction between the platform and the PHY requires 458*4882a593Smuzhiyunspecial handling. For instance, to change where the PHY's clock input is, 459*4882a593Smuzhiyunor to add a delay to account for latency issues in the data path. In order 460*4882a593Smuzhiyunto support such contingencies, the PHY Layer allows platform code to register 461*4882a593Smuzhiyunfixups to be run when the PHY is brought up (or subsequently reset). 462*4882a593Smuzhiyun 463*4882a593SmuzhiyunWhen the PHY Layer brings up a PHY it checks to see if there are any fixups 464*4882a593Smuzhiyunregistered for it, matching based on UID (contained in the PHY device's phy_id 465*4882a593Smuzhiyunfield) and the bus identifier (contained in phydev->dev.bus_id). Both must 466*4882a593Smuzhiyunmatch, however two constants, PHY_ANY_ID and PHY_ANY_UID, are provided as 467*4882a593Smuzhiyunwildcards for the bus ID and UID, respectively. 468*4882a593Smuzhiyun 469*4882a593SmuzhiyunWhen a match is found, the PHY layer will invoke the run function associated 470*4882a593Smuzhiyunwith the fixup. This function is passed a pointer to the phy_device of 471*4882a593Smuzhiyuninterest. It should therefore only operate on that PHY. 472*4882a593Smuzhiyun 473*4882a593SmuzhiyunThe platform code can either register the fixup using phy_register_fixup():: 474*4882a593Smuzhiyun 475*4882a593Smuzhiyun int phy_register_fixup(const char *phy_id, 476*4882a593Smuzhiyun u32 phy_uid, u32 phy_uid_mask, 477*4882a593Smuzhiyun int (*run)(struct phy_device *)); 478*4882a593Smuzhiyun 479*4882a593SmuzhiyunOr using one of the two stubs, phy_register_fixup_for_uid() and 480*4882a593Smuzhiyunphy_register_fixup_for_id():: 481*4882a593Smuzhiyun 482*4882a593Smuzhiyun int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, 483*4882a593Smuzhiyun int (*run)(struct phy_device *)); 484*4882a593Smuzhiyun int phy_register_fixup_for_id(const char *phy_id, 485*4882a593Smuzhiyun int (*run)(struct phy_device *)); 486*4882a593Smuzhiyun 487*4882a593SmuzhiyunThe stubs set one of the two matching criteria, and set the other one to 488*4882a593Smuzhiyunmatch anything. 489*4882a593Smuzhiyun 490*4882a593SmuzhiyunWhen phy_register_fixup() or \*_for_uid()/\*_for_id() is called at module load 491*4882a593Smuzhiyuntime, the module needs to unregister the fixup and free allocated memory when 492*4882a593Smuzhiyunit's unloaded. 493*4882a593Smuzhiyun 494*4882a593SmuzhiyunCall one of following function before unloading module:: 495*4882a593Smuzhiyun 496*4882a593Smuzhiyun int phy_unregister_fixup(const char *phy_id, u32 phy_uid, u32 phy_uid_mask); 497*4882a593Smuzhiyun int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask); 498*4882a593Smuzhiyun int phy_register_fixup_for_id(const char *phy_id); 499*4882a593Smuzhiyun 500*4882a593SmuzhiyunStandards 501*4882a593Smuzhiyun========= 502*4882a593Smuzhiyun 503*4882a593SmuzhiyunIEEE Standard 802.3: CSMA/CD Access Method and Physical Layer Specifications, Section Two: 504*4882a593Smuzhiyunhttp://standards.ieee.org/getieee802/download/802.3-2008_section2.pdf 505*4882a593Smuzhiyun 506*4882a593SmuzhiyunRGMII v1.3: 507*4882a593Smuzhiyunhttp://web.archive.org/web/20160303212629/http://www.hp.com/rnd/pdfs/RGMIIv1_3.pdf 508*4882a593Smuzhiyun 509*4882a593SmuzhiyunRGMII v2.0: 510*4882a593Smuzhiyunhttp://web.archive.org/web/20160303171328/http://www.hp.com/rnd/pdfs/RGMIIv2_0_final_hp.pdf 511