phy.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. -------
  2. PHY Abstraction Layer
  3. (Updated 2006-11-30)
  4. Purpose
  5. Most network devices consist of set of registers which provide an interface
  6. to a MAC layer, which communicates with the physical connection through a
  7. PHY. The PHY concerns itself with negotiating link parameters with the link
  8. partner on the other side of the network connection (typically, an ethernet
  9. cable), and provides a register interface to allow drivers to determine what
  10. settings were chosen, and to configure what settings are allowed.
  11. While these devices are distinct from the network devices, and conform to a
  12. standard layout for the registers, it has been common practice to integrate
  13. the PHY management code with the network driver. This has resulted in large
  14. amounts of redundant code. Also, on embedded systems with multiple (and
  15. sometimes quite different) ethernet controllers connected to the same
  16. management bus, it is difficult to ensure safe use of the bus.
  17. Since the PHYs are devices, and the management busses through which they are
  18. accessed are, in fact, busses, the PHY Abstraction Layer treats them as such.
  19. In doing so, it has these goals:
  20. 1) Increase code-reuse
  21. 2) Increase overall code-maintainability
  22. 3) Speed development time for new network drivers, and for new systems
  23. Basically, this layer is meant to provide an interface to PHY devices which
  24. allows network driver writers to write as little code as possible, while
  25. still providing a full feature set.
  26. The MDIO bus
  27. Most network devices are connected to a PHY by means of a management bus.
  28. Different devices use different busses (though some share common interfaces).
  29. In order to take advantage of the PAL, each bus interface needs to be
  30. registered as a distinct device.
  31. 1) read and write functions must be implemented. Their prototypes are:
  32. int write(struct mii_bus *bus, int mii_id, int regnum, u16 value);
  33. int read(struct mii_bus *bus, int mii_id, int regnum);
  34. mii_id is the address on the bus for the PHY, and regnum is the register
  35. number. These functions are guaranteed not to be called from interrupt
  36. time, so it is safe for them to block, waiting for an interrupt to signal
  37. the operation is complete
  38. 2) A reset function is necessary. This is used to return the bus to an
  39. initialized state.
  40. 3) A probe function is needed. This function should set up anything the bus
  41. driver needs, setup the mii_bus structure, and register with the PAL using
  42. mdiobus_register. Similarly, there's a remove function to undo all of
  43. that (use mdiobus_unregister).
  44. 4) Like any driver, the device_driver structure must be configured, and init
  45. exit functions are used to register the driver.
  46. 5) The bus must also be declared somewhere as a device, and registered.
  47. As an example for how one driver implemented an mdio bus driver, see
  48. drivers/net/gianfar_mii.c and arch/ppc/syslib/mpc85xx_devices.c
  49. Connecting to a PHY
  50. Sometime during startup, the network driver needs to establish a connection
  51. between the PHY device, and the network device. At this time, the PHY's bus
  52. and drivers need to all have been loaded, so it is ready for the connection.
  53. At this point, there are several ways to connect to the PHY:
  54. 1) The PAL handles everything, and only calls the network driver when
  55. the link state changes, so it can react.
  56. 2) The PAL handles everything except interrupts (usually because the
  57. controller has the interrupt registers).
  58. 3) The PAL handles everything, but checks in with the driver every second,
  59. allowing the network driver to react first to any changes before the PAL
  60. does.
  61. 4) The PAL serves only as a library of functions, with the network device
  62. manually calling functions to update status, and configure the PHY
  63. Letting the PHY Abstraction Layer do Everything
  64. If you choose option 1 (The hope is that every driver can, but to still be
  65. useful to drivers that can't), connecting to the PHY is simple:
  66. First, you need a function to react to changes in the link state. This
  67. function follows this protocol:
  68. static void adjust_link(struct net_device *dev);
  69. Next, you need to know the device name of the PHY connected to this device.
  70. The name will look something like, "phy0:0", where the first number is the
  71. bus id, and the second is the PHY's address on that bus. Typically,
  72. the bus is responsible for making its ID unique.
  73. Now, to connect, just call this function:
  74. phydev = phy_connect(dev, phy_name, &adjust_link, flags, interface);
  75. phydev is a pointer to the phy_device structure which represents the PHY. If
  76. phy_connect is successful, it will return the pointer. dev, here, is the
  77. pointer to your net_device. Once done, this function will have started the
  78. PHY's software state machine, and registered for the PHY's interrupt, if it
  79. has one. The phydev structure will be populated with information about the
  80. current state, though the PHY will not yet be truly operational at this
  81. point.
  82. flags is a u32 which can optionally contain phy-specific flags.
  83. This is useful if the system has put hardware restrictions on
  84. the PHY/controller, of which the PHY needs to be aware.
  85. interface is a u32 which specifies the connection type used
  86. between the controller and the PHY. Examples are GMII, MII,
  87. RGMII, and SGMII. For a full list, see include/linux/phy.h
  88. Now just make sure that phydev->supported and phydev->advertising have any
  89. values pruned from them which don't make sense for your controller (a 10/100
  90. controller may be connected to a gigabit capable PHY, so you would need to
  91. mask off SUPPORTED_1000baseT*). See include/linux/ethtool.h for definitions
  92. for these bitfields. Note that you should not SET any bits, or the PHY may
  93. get put into an unsupported state.
  94. Lastly, once the controller is ready to handle network traffic, you call
  95. phy_start(phydev). This tells the PAL that you are ready, and configures the
  96. PHY to connect to the network. If you want to handle your own interrupts,
  97. just set phydev->irq to PHY_IGNORE_INTERRUPT before you call phy_start.
  98. Similarly, if you don't want to use interrupts, set phydev->irq to PHY_POLL.
  99. When you want to disconnect from the network (even if just briefly), you call
  100. phy_stop(phydev).
  101. Keeping Close Tabs on the PAL
  102. It is possible that the PAL's built-in state machine needs a little help to
  103. keep your network device and the PHY properly in sync. If so, you can
  104. register a helper function when connecting to the PHY, which will be called
  105. every second before the state machine reacts to any changes. To do this, you
  106. need to manually call phy_attach() and phy_prepare_link(), and then call
  107. phy_start_machine() with the second argument set to point to your special
  108. handler.
  109. Currently there are no examples of how to use this functionality, and testing
  110. on it has been limited because the author does not have any drivers which use
  111. it (they all use option 1). So Caveat Emptor.
  112. Doing it all yourself
  113. There's a remote chance that the PAL's built-in state machine cannot track
  114. the complex interactions between the PHY and your network device. If this is
  115. so, you can simply call phy_attach(), and not call phy_start_machine or
  116. phy_prepare_link(). This will mean that phydev->state is entirely yours to
  117. handle (phy_start and phy_stop toggle between some of the states, so you
  118. might need to avoid them).
  119. An effort has been made to make sure that useful functionality can be
  120. accessed without the state-machine running, and most of these functions are
  121. descended from functions which did not interact with a complex state-machine.
  122. However, again, no effort has been made so far to test running without the
  123. state machine, so tryer beware.
  124. Here is a brief rundown of the functions:
  125. int phy_read(struct phy_device *phydev, u16 regnum);
  126. int phy_write(struct phy_device *phydev, u16 regnum, u16 val);
  127. Simple read/write primitives. They invoke the bus's read/write function
  128. pointers.
  129. void phy_print_status(struct phy_device *phydev);
  130. A convenience function to print out the PHY status neatly.
  131. int phy_clear_interrupt(struct phy_device *phydev);
  132. int phy_config_interrupt(struct phy_device *phydev, u32 interrupts);
  133. Clear the PHY's interrupt, and configure which ones are allowed,
  134. respectively. Currently only supports all on, or all off.
  135. int phy_enable_interrupts(struct phy_device *phydev);
  136. int phy_disable_interrupts(struct phy_device *phydev);
  137. Functions which enable/disable PHY interrupts, clearing them
  138. before and after, respectively.
  139. int phy_start_interrupts(struct phy_device *phydev);
  140. int phy_stop_interrupts(struct phy_device *phydev);
  141. Requests the IRQ for the PHY interrupts, then enables them for
  142. start, or disables then frees them for stop.
  143. struct phy_device * phy_attach(struct net_device *dev, const char *phy_id,
  144. u32 flags, phy_interface_t interface);
  145. Attaches a network device to a particular PHY, binding the PHY to a generic
  146. driver if none was found during bus initialization. Passes in
  147. any phy-specific flags as needed.
  148. int phy_start_aneg(struct phy_device *phydev);
  149. Using variables inside the phydev structure, either configures advertising
  150. and resets autonegotiation, or disables autonegotiation, and configures
  151. forced settings.
  152. static inline int phy_read_status(struct phy_device *phydev);
  153. Fills the phydev structure with up-to-date information about the current
  154. settings in the PHY.
  155. void phy_sanitize_settings(struct phy_device *phydev)
  156. Resolves differences between currently desired settings, and
  157. supported settings for the given PHY device. Does not make
  158. the changes in the hardware, though.
  159. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
  160. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
  161. Ethtool convenience functions.
  162. int phy_mii_ioctl(struct phy_device *phydev,
  163. struct mii_ioctl_data *mii_data, int cmd);
  164. The MII ioctl. Note that this function will completely screw up the state
  165. machine if you write registers like BMCR, BMSR, ADVERTISE, etc. Best to
  166. use this only to write registers which are not standard, and don't set off
  167. a renegotiation.
  168. PHY Device Drivers
  169. With the PHY Abstraction Layer, adding support for new PHYs is
  170. quite easy. In some cases, no work is required at all! However,
  171. many PHYs require a little hand-holding to get up-and-running.
  172. Generic PHY driver
  173. If the desired PHY doesn't have any errata, quirks, or special
  174. features you want to support, then it may be best to not add
  175. support, and let the PHY Abstraction Layer's Generic PHY Driver
  176. do all of the work.
  177. Writing a PHY driver
  178. If you do need to write a PHY driver, the first thing to do is
  179. make sure it can be matched with an appropriate PHY device.
  180. This is done during bus initialization by reading the device's
  181. UID (stored in registers 2 and 3), then comparing it to each
  182. driver's phy_id field by ANDing it with each driver's
  183. phy_id_mask field. Also, it needs a name. Here's an example:
  184. static struct phy_driver dm9161_driver = {
  185. .phy_id = 0x0181b880,
  186. .name = "Davicom DM9161E",
  187. .phy_id_mask = 0x0ffffff0,
  188. ...
  189. }
  190. Next, you need to specify what features (speed, duplex, autoneg,
  191. etc) your PHY device and driver support. Most PHYs support
  192. PHY_BASIC_FEATURES, but you can look in include/mii.h for other
  193. features.
  194. Each driver consists of a number of function pointers:
  195. config_init: configures PHY into a sane state after a reset.
  196. For instance, a Davicom PHY requires descrambling disabled.
  197. probe: Does any setup needed by the driver
  198. suspend/resume: power management
  199. config_aneg: Changes the speed/duplex/negotiation settings
  200. read_status: Reads the current speed/duplex/negotiation settings
  201. ack_interrupt: Clear a pending interrupt
  202. config_intr: Enable or disable interrupts
  203. remove: Does any driver take-down
  204. Of these, only config_aneg and read_status are required to be
  205. assigned by the driver code. The rest are optional. Also, it is
  206. preferred to use the generic phy driver's versions of these two
  207. functions if at all possible: genphy_read_status and
  208. genphy_config_aneg. If this is not possible, it is likely that
  209. you only need to perform some actions before and after invoking
  210. these functions, and so your functions will wrap the generic
  211. ones.
  212. Feel free to look at the Marvell, Cicada, and Davicom drivers in
  213. drivers/net/phy/ for examples (the lxt and qsemi drivers have
  214. not been tested as of this writing)