phy.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * Framework and drivers for configuring and reading different PHYs
  3. * Based on code in sungem_phy.c and gianfar_phy.c
  4. *
  5. * Author: Andy Fleming
  6. *
  7. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #ifndef __PHY_H
  16. #define __PHY_H
  17. #include <linux/spinlock.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/mii.h>
  20. #include <linux/module.h>
  21. #include <linux/timer.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/atomic.h>
  25. #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
  26. SUPPORTED_TP | \
  27. SUPPORTED_MII)
  28. #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
  29. SUPPORTED_10baseT_Full)
  30. #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
  31. SUPPORTED_100baseT_Full)
  32. #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
  33. SUPPORTED_1000baseT_Full)
  34. #define PHY_BASIC_FEATURES (PHY_10BT_FEATURES | \
  35. PHY_100BT_FEATURES | \
  36. PHY_DEFAULT_FEATURES)
  37. #define PHY_GBIT_FEATURES (PHY_BASIC_FEATURES | \
  38. PHY_1000BT_FEATURES)
  39. /*
  40. * Set phydev->irq to PHY_POLL if interrupts are not supported,
  41. * or not desired for this PHY. Set to PHY_IGNORE_INTERRUPT if
  42. * the attached driver handles the interrupt
  43. */
  44. #define PHY_POLL -1
  45. #define PHY_IGNORE_INTERRUPT -2
  46. #define PHY_HAS_INTERRUPT 0x00000001
  47. #define PHY_HAS_MAGICANEG 0x00000002
  48. #define PHY_IS_INTERNAL 0x00000004
  49. /* Interface Mode definitions */
  50. typedef enum {
  51. PHY_INTERFACE_MODE_NA,
  52. PHY_INTERFACE_MODE_MII,
  53. PHY_INTERFACE_MODE_GMII,
  54. PHY_INTERFACE_MODE_SGMII,
  55. PHY_INTERFACE_MODE_TBI,
  56. PHY_INTERFACE_MODE_REVMII,
  57. PHY_INTERFACE_MODE_RMII,
  58. PHY_INTERFACE_MODE_RGMII,
  59. PHY_INTERFACE_MODE_RGMII_ID,
  60. PHY_INTERFACE_MODE_RGMII_RXID,
  61. PHY_INTERFACE_MODE_RGMII_TXID,
  62. PHY_INTERFACE_MODE_RTBI,
  63. PHY_INTERFACE_MODE_SMII,
  64. PHY_INTERFACE_MODE_XGMII,
  65. PHY_INTERFACE_MODE_MOCA,
  66. PHY_INTERFACE_MODE_QSGMII,
  67. PHY_INTERFACE_MODE_MAX,
  68. } phy_interface_t;
  69. /**
  70. * It maps 'enum phy_interface_t' found in include/linux/phy.h
  71. * into the device tree binding of 'phy-mode', so that Ethernet
  72. * device driver can get phy interface from device tree.
  73. */
  74. static inline const char *phy_modes(phy_interface_t interface)
  75. {
  76. switch (interface) {
  77. case PHY_INTERFACE_MODE_NA:
  78. return "";
  79. case PHY_INTERFACE_MODE_MII:
  80. return "mii";
  81. case PHY_INTERFACE_MODE_GMII:
  82. return "gmii";
  83. case PHY_INTERFACE_MODE_SGMII:
  84. return "sgmii";
  85. case PHY_INTERFACE_MODE_TBI:
  86. return "tbi";
  87. case PHY_INTERFACE_MODE_REVMII:
  88. return "rev-mii";
  89. case PHY_INTERFACE_MODE_RMII:
  90. return "rmii";
  91. case PHY_INTERFACE_MODE_RGMII:
  92. return "rgmii";
  93. case PHY_INTERFACE_MODE_RGMII_ID:
  94. return "rgmii-id";
  95. case PHY_INTERFACE_MODE_RGMII_RXID:
  96. return "rgmii-rxid";
  97. case PHY_INTERFACE_MODE_RGMII_TXID:
  98. return "rgmii-txid";
  99. case PHY_INTERFACE_MODE_RTBI:
  100. return "rtbi";
  101. case PHY_INTERFACE_MODE_SMII:
  102. return "smii";
  103. case PHY_INTERFACE_MODE_XGMII:
  104. return "xgmii";
  105. case PHY_INTERFACE_MODE_MOCA:
  106. return "moca";
  107. case PHY_INTERFACE_MODE_QSGMII:
  108. return "qsgmii";
  109. default:
  110. return "unknown";
  111. }
  112. }
  113. #define PHY_INIT_TIMEOUT 100000
  114. #define PHY_STATE_TIME 1
  115. #define PHY_FORCE_TIMEOUT 10
  116. #define PHY_AN_TIMEOUT 10
  117. #define PHY_MAX_ADDR 32
  118. /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
  119. #define PHY_ID_FMT "%s:%02x"
  120. /*
  121. * Need to be a little smaller than phydev->dev.bus_id to leave room
  122. * for the ":%02x"
  123. */
  124. #define MII_BUS_ID_SIZE (20 - 3)
  125. /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
  126. IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips. */
  127. #define MII_ADDR_C45 (1<<30)
  128. struct device;
  129. struct sk_buff;
  130. /*
  131. * The Bus class for PHYs. Devices which provide access to
  132. * PHYs should register using this structure
  133. */
  134. struct mii_bus {
  135. struct module *owner;
  136. const char *name;
  137. char id[MII_BUS_ID_SIZE];
  138. void *priv;
  139. int (*read)(struct mii_bus *bus, int phy_id, int regnum);
  140. int (*write)(struct mii_bus *bus, int phy_id, int regnum, u16 val);
  141. int (*reset)(struct mii_bus *bus);
  142. /*
  143. * A lock to ensure that only one thing can read/write
  144. * the MDIO bus at a time
  145. */
  146. struct mutex mdio_lock;
  147. struct device *parent;
  148. enum {
  149. MDIOBUS_ALLOCATED = 1,
  150. MDIOBUS_REGISTERED,
  151. MDIOBUS_UNREGISTERED,
  152. MDIOBUS_RELEASED,
  153. } state;
  154. struct device dev;
  155. /* list of all PHYs on bus */
  156. struct phy_device *phy_map[PHY_MAX_ADDR];
  157. /* PHY addresses to be ignored when probing */
  158. u32 phy_mask;
  159. /* PHY addresses to ignore the TA/read failure */
  160. u32 phy_ignore_ta_mask;
  161. /*
  162. * Pointer to an array of interrupts, each PHY's
  163. * interrupt at the index matching its address
  164. */
  165. int *irq;
  166. };
  167. #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
  168. struct mii_bus *mdiobus_alloc_size(size_t);
  169. static inline struct mii_bus *mdiobus_alloc(void)
  170. {
  171. return mdiobus_alloc_size(0);
  172. }
  173. int __mdiobus_register(struct mii_bus *bus, struct module *owner);
  174. #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
  175. void mdiobus_unregister(struct mii_bus *bus);
  176. void mdiobus_free(struct mii_bus *bus);
  177. struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
  178. static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
  179. {
  180. return devm_mdiobus_alloc_size(dev, 0);
  181. }
  182. void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
  183. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
  184. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
  185. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
  186. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  187. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  188. #define PHY_INTERRUPT_DISABLED 0x0
  189. #define PHY_INTERRUPT_ENABLED 0x80000000
  190. /* PHY state machine states:
  191. *
  192. * DOWN: PHY device and driver are not ready for anything. probe
  193. * should be called if and only if the PHY is in this state,
  194. * given that the PHY device exists.
  195. * - PHY driver probe function will, depending on the PHY, set
  196. * the state to STARTING or READY
  197. *
  198. * STARTING: PHY device is coming up, and the ethernet driver is
  199. * not ready. PHY drivers may set this in the probe function.
  200. * If they do, they are responsible for making sure the state is
  201. * eventually set to indicate whether the PHY is UP or READY,
  202. * depending on the state when the PHY is done starting up.
  203. * - PHY driver will set the state to READY
  204. * - start will set the state to PENDING
  205. *
  206. * READY: PHY is ready to send and receive packets, but the
  207. * controller is not. By default, PHYs which do not implement
  208. * probe will be set to this state by phy_probe(). If the PHY
  209. * driver knows the PHY is ready, and the PHY state is STARTING,
  210. * then it sets this STATE.
  211. * - start will set the state to UP
  212. *
  213. * PENDING: PHY device is coming up, but the ethernet driver is
  214. * ready. phy_start will set this state if the PHY state is
  215. * STARTING.
  216. * - PHY driver will set the state to UP when the PHY is ready
  217. *
  218. * UP: The PHY and attached device are ready to do work.
  219. * Interrupts should be started here.
  220. * - timer moves to AN
  221. *
  222. * AN: The PHY is currently negotiating the link state. Link is
  223. * therefore down for now. phy_timer will set this state when it
  224. * detects the state is UP. config_aneg will set this state
  225. * whenever called with phydev->autoneg set to AUTONEG_ENABLE.
  226. * - If autonegotiation finishes, but there's no link, it sets
  227. * the state to NOLINK.
  228. * - If aneg finishes with link, it sets the state to RUNNING,
  229. * and calls adjust_link
  230. * - If autonegotiation did not finish after an arbitrary amount
  231. * of time, autonegotiation should be tried again if the PHY
  232. * supports "magic" autonegotiation (back to AN)
  233. * - If it didn't finish, and no magic_aneg, move to FORCING.
  234. *
  235. * NOLINK: PHY is up, but not currently plugged in.
  236. * - If the timer notes that the link comes back, we move to RUNNING
  237. * - config_aneg moves to AN
  238. * - phy_stop moves to HALTED
  239. *
  240. * FORCING: PHY is being configured with forced settings
  241. * - if link is up, move to RUNNING
  242. * - If link is down, we drop to the next highest setting, and
  243. * retry (FORCING) after a timeout
  244. * - phy_stop moves to HALTED
  245. *
  246. * RUNNING: PHY is currently up, running, and possibly sending
  247. * and/or receiving packets
  248. * - timer will set CHANGELINK if we're polling (this ensures the
  249. * link state is polled every other cycle of this state machine,
  250. * which makes it every other second)
  251. * - irq will set CHANGELINK
  252. * - config_aneg will set AN
  253. * - phy_stop moves to HALTED
  254. *
  255. * CHANGELINK: PHY experienced a change in link state
  256. * - timer moves to RUNNING if link
  257. * - timer moves to NOLINK if the link is down
  258. * - phy_stop moves to HALTED
  259. *
  260. * HALTED: PHY is up, but no polling or interrupts are done. Or
  261. * PHY is in an error state.
  262. *
  263. * - phy_start moves to RESUMING
  264. *
  265. * RESUMING: PHY was halted, but now wants to run again.
  266. * - If we are forcing, or aneg is done, timer moves to RUNNING
  267. * - If aneg is not done, timer moves to AN
  268. * - phy_stop moves to HALTED
  269. */
  270. enum phy_state {
  271. PHY_DOWN = 0,
  272. PHY_STARTING,
  273. PHY_READY,
  274. PHY_PENDING,
  275. PHY_UP,
  276. PHY_AN,
  277. PHY_RUNNING,
  278. PHY_NOLINK,
  279. PHY_FORCING,
  280. PHY_CHANGELINK,
  281. PHY_HALTED,
  282. PHY_RESUMING
  283. };
  284. /**
  285. * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
  286. * @devices_in_package: Bit vector of devices present.
  287. * @device_ids: The device identifer for each present device.
  288. */
  289. struct phy_c45_device_ids {
  290. u32 devices_in_package;
  291. u32 device_ids[8];
  292. };
  293. /* phy_device: An instance of a PHY
  294. *
  295. * drv: Pointer to the driver for this PHY instance
  296. * bus: Pointer to the bus this PHY is on
  297. * dev: driver model device structure for this PHY
  298. * phy_id: UID for this device found during discovery
  299. * c45_ids: 802.3-c45 Device Identifers if is_c45.
  300. * is_c45: Set to true if this phy uses clause 45 addressing.
  301. * is_internal: Set to true if this phy is internal to a MAC.
  302. * is_pseudo_fixed_link: Set to true if this phy is an Ethernet switch, etc.
  303. * has_fixups: Set to true if this phy has fixups/quirks.
  304. * suspended: Set to true if this phy has been suspended successfully.
  305. * state: state of the PHY for management purposes
  306. * dev_flags: Device-specific flags used by the PHY driver.
  307. * addr: Bus address of PHY
  308. * link_timeout: The number of timer firings to wait before the
  309. * giving up on the current attempt at acquiring a link
  310. * irq: IRQ number of the PHY's interrupt (-1 if none)
  311. * phy_timer: The timer for handling the state machine
  312. * phy_queue: A work_queue for the interrupt
  313. * attached_dev: The attached enet driver's device instance ptr
  314. * adjust_link: Callback for the enet controller to respond to
  315. * changes in the link state.
  316. *
  317. * speed, duplex, pause, supported, advertising, lp_advertising,
  318. * and autoneg are used like in mii_if_info
  319. *
  320. * interrupts currently only supports enabled or disabled,
  321. * but could be changed in the future to support enabling
  322. * and disabling specific interrupts
  323. *
  324. * Contains some infrastructure for polling and interrupt
  325. * handling, as well as handling shifts in PHY hardware state
  326. */
  327. struct phy_device {
  328. /* Information about the PHY type */
  329. /* And management functions */
  330. struct phy_driver *drv;
  331. struct mii_bus *bus;
  332. struct device dev;
  333. u32 phy_id;
  334. struct phy_c45_device_ids c45_ids;
  335. bool is_c45;
  336. bool is_internal;
  337. bool is_pseudo_fixed_link;
  338. bool has_fixups;
  339. bool suspended;
  340. enum phy_state state;
  341. u32 dev_flags;
  342. phy_interface_t interface;
  343. /* Bus address of the PHY (0-31) */
  344. int addr;
  345. /*
  346. * forced speed & duplex (no autoneg)
  347. * partner speed & duplex & pause (autoneg)
  348. */
  349. int speed;
  350. int duplex;
  351. int pause;
  352. int asym_pause;
  353. /* The most recently read link state */
  354. int link;
  355. /* Enabled Interrupts */
  356. u32 interrupts;
  357. /* Union of PHY and Attached devices' supported modes */
  358. /* See mii.h for more info */
  359. u32 supported;
  360. u32 advertising;
  361. u32 lp_advertising;
  362. int autoneg;
  363. int link_timeout;
  364. /*
  365. * Interrupt number for this PHY
  366. * -1 means no interrupt
  367. */
  368. int irq;
  369. /* private data pointer */
  370. /* For use by PHYs to maintain extra state */
  371. void *priv;
  372. /* Interrupt and Polling infrastructure */
  373. struct work_struct phy_queue;
  374. struct delayed_work state_queue;
  375. atomic_t irq_disable;
  376. struct mutex lock;
  377. struct net_device *attached_dev;
  378. u8 mdix;
  379. void (*adjust_link)(struct net_device *dev);
  380. };
  381. #define to_phy_device(d) container_of(d, struct phy_device, dev)
  382. /* struct phy_driver: Driver structure for a particular PHY type
  383. *
  384. * phy_id: The result of reading the UID registers of this PHY
  385. * type, and ANDing them with the phy_id_mask. This driver
  386. * only works for PHYs with IDs which match this field
  387. * name: The friendly name of this PHY type
  388. * phy_id_mask: Defines the important bits of the phy_id
  389. * features: A list of features (speed, duplex, etc) supported
  390. * by this PHY
  391. * flags: A bitfield defining certain other features this PHY
  392. * supports (like interrupts)
  393. * driver_data: static driver data
  394. *
  395. * The drivers must implement config_aneg and read_status. All
  396. * other functions are optional. Note that none of these
  397. * functions should be called from interrupt time. The goal is
  398. * for the bus read/write functions to be able to block when the
  399. * bus transaction is happening, and be freed up by an interrupt
  400. * (The MPC85xx has this ability, though it is not currently
  401. * supported in the driver).
  402. */
  403. struct phy_driver {
  404. u32 phy_id;
  405. char *name;
  406. unsigned int phy_id_mask;
  407. u32 features;
  408. u32 flags;
  409. const void *driver_data;
  410. /*
  411. * Called to issue a PHY software reset
  412. */
  413. int (*soft_reset)(struct phy_device *phydev);
  414. /*
  415. * Called to initialize the PHY,
  416. * including after a reset
  417. */
  418. int (*config_init)(struct phy_device *phydev);
  419. /*
  420. * Called during discovery. Used to set
  421. * up device-specific structures, if any
  422. */
  423. int (*probe)(struct phy_device *phydev);
  424. /* PHY Power Management */
  425. int (*suspend)(struct phy_device *phydev);
  426. int (*resume)(struct phy_device *phydev);
  427. /*
  428. * Configures the advertisement and resets
  429. * autonegotiation if phydev->autoneg is on,
  430. * forces the speed to the current settings in phydev
  431. * if phydev->autoneg is off
  432. */
  433. int (*config_aneg)(struct phy_device *phydev);
  434. /* Determines the auto negotiation result */
  435. int (*aneg_done)(struct phy_device *phydev);
  436. /* Determines the negotiated speed and duplex */
  437. int (*read_status)(struct phy_device *phydev);
  438. /* Clears any pending interrupts */
  439. int (*ack_interrupt)(struct phy_device *phydev);
  440. /* Enables or disables interrupts */
  441. int (*config_intr)(struct phy_device *phydev);
  442. /*
  443. * Checks if the PHY generated an interrupt.
  444. * For multi-PHY devices with shared PHY interrupt pin
  445. */
  446. int (*did_interrupt)(struct phy_device *phydev);
  447. /* Clears up any memory if needed */
  448. void (*remove)(struct phy_device *phydev);
  449. /* Returns true if this is a suitable driver for the given
  450. * phydev. If NULL, matching is based on phy_id and
  451. * phy_id_mask.
  452. */
  453. int (*match_phy_device)(struct phy_device *phydev);
  454. /* Handles ethtool queries for hardware time stamping. */
  455. int (*ts_info)(struct phy_device *phydev, struct ethtool_ts_info *ti);
  456. /* Handles SIOCSHWTSTAMP ioctl for hardware time stamping. */
  457. int (*hwtstamp)(struct phy_device *phydev, struct ifreq *ifr);
  458. /*
  459. * Requests a Rx timestamp for 'skb'. If the skb is accepted,
  460. * the phy driver promises to deliver it using netif_rx() as
  461. * soon as a timestamp becomes available. One of the
  462. * PTP_CLASS_ values is passed in 'type'. The function must
  463. * return true if the skb is accepted for delivery.
  464. */
  465. bool (*rxtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
  466. /*
  467. * Requests a Tx timestamp for 'skb'. The phy driver promises
  468. * to deliver it using skb_complete_tx_timestamp() as soon as a
  469. * timestamp becomes available. One of the PTP_CLASS_ values
  470. * is passed in 'type'.
  471. */
  472. void (*txtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
  473. /* Some devices (e.g. qnap TS-119P II) require PHY register changes to
  474. * enable Wake on LAN, so set_wol is provided to be called in the
  475. * ethernet driver's set_wol function. */
  476. int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
  477. /* See set_wol, but for checking whether Wake on LAN is enabled. */
  478. void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
  479. /*
  480. * Called to inform a PHY device driver when the core is about to
  481. * change the link state. This callback is supposed to be used as
  482. * fixup hook for drivers that need to take action when the link
  483. * state changes. Drivers are by no means allowed to mess with the
  484. * PHY device structure in their implementations.
  485. */
  486. void (*link_change_notify)(struct phy_device *dev);
  487. /* A function provided by a phy specific driver to override the
  488. * the PHY driver framework support for reading a MMD register
  489. * from the PHY. If not supported, return -1. This function is
  490. * optional for PHY specific drivers, if not provided then the
  491. * default MMD read function is used by the PHY framework.
  492. */
  493. int (*read_mmd_indirect)(struct phy_device *dev, int ptrad,
  494. int devnum, int regnum);
  495. /* A function provided by a phy specific driver to override the
  496. * the PHY driver framework support for writing a MMD register
  497. * from the PHY. This function is optional for PHY specific drivers,
  498. * if not provided then the default MMD read function is used by
  499. * the PHY framework.
  500. */
  501. void (*write_mmd_indirect)(struct phy_device *dev, int ptrad,
  502. int devnum, int regnum, u32 val);
  503. /* Get the size and type of the eeprom contained within a plug-in
  504. * module */
  505. int (*module_info)(struct phy_device *dev,
  506. struct ethtool_modinfo *modinfo);
  507. /* Get the eeprom information from the plug-in module */
  508. int (*module_eeprom)(struct phy_device *dev,
  509. struct ethtool_eeprom *ee, u8 *data);
  510. /* Get statistics from the phy using ethtool */
  511. int (*get_sset_count)(struct phy_device *dev);
  512. void (*get_strings)(struct phy_device *dev, u8 *data);
  513. void (*get_stats)(struct phy_device *dev,
  514. struct ethtool_stats *stats, u64 *data);
  515. struct device_driver driver;
  516. };
  517. #define to_phy_driver(d) container_of(d, struct phy_driver, driver)
  518. #define PHY_ANY_ID "MATCH ANY PHY"
  519. #define PHY_ANY_UID 0xffffffff
  520. /* A Structure for boards to register fixups with the PHY Lib */
  521. struct phy_fixup {
  522. struct list_head list;
  523. char bus_id[20];
  524. u32 phy_uid;
  525. u32 phy_uid_mask;
  526. int (*run)(struct phy_device *phydev);
  527. };
  528. /**
  529. * phy_read_mmd - Convenience function for reading a register
  530. * from an MMD on a given PHY.
  531. * @phydev: The phy_device struct
  532. * @devad: The MMD to read from
  533. * @regnum: The register on the MMD to read
  534. *
  535. * Same rules as for phy_read();
  536. */
  537. static inline int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  538. {
  539. if (!phydev->is_c45)
  540. return -EOPNOTSUPP;
  541. return mdiobus_read(phydev->bus, phydev->addr,
  542. MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff));
  543. }
  544. /**
  545. * phy_read_mmd_indirect - reads data from the MMD registers
  546. * @phydev: The PHY device bus
  547. * @prtad: MMD Address
  548. * @devad: MMD DEVAD
  549. * @addr: PHY address on the MII bus
  550. *
  551. * Description: it reads data from the MMD registers (clause 22 to access to
  552. * clause 45) of the specified phy address.
  553. */
  554. int phy_read_mmd_indirect(struct phy_device *phydev, int prtad,
  555. int devad, int addr);
  556. /**
  557. * phy_read - Convenience function for reading a given PHY register
  558. * @phydev: the phy_device struct
  559. * @regnum: register number to read
  560. *
  561. * NOTE: MUST NOT be called from interrupt context,
  562. * because the bus read/write functions may wait for an interrupt
  563. * to conclude the operation.
  564. */
  565. static inline int phy_read(struct phy_device *phydev, u32 regnum)
  566. {
  567. return mdiobus_read(phydev->bus, phydev->addr, regnum);
  568. }
  569. /**
  570. * phy_write - Convenience function for writing a given PHY register
  571. * @phydev: the phy_device struct
  572. * @regnum: register number to write
  573. * @val: value to write to @regnum
  574. *
  575. * NOTE: MUST NOT be called from interrupt context,
  576. * because the bus read/write functions may wait for an interrupt
  577. * to conclude the operation.
  578. */
  579. static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
  580. {
  581. return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
  582. }
  583. /**
  584. * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
  585. * @phydev: the phy_device struct
  586. *
  587. * NOTE: must be kept in sync with addition/removal of PHY_POLL and
  588. * PHY_IGNORE_INTERRUPT
  589. */
  590. static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
  591. {
  592. return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
  593. }
  594. /**
  595. * phy_is_internal - Convenience function for testing if a PHY is internal
  596. * @phydev: the phy_device struct
  597. */
  598. static inline bool phy_is_internal(struct phy_device *phydev)
  599. {
  600. return phydev->is_internal;
  601. }
  602. /**
  603. * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
  604. * is RGMII (all variants)
  605. * @phydev: the phy_device struct
  606. */
  607. static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  608. {
  609. return phydev->interface >= PHY_INTERFACE_MODE_RGMII &&
  610. phydev->interface <= PHY_INTERFACE_MODE_RGMII_TXID;
  611. };
  612. /*
  613. * phy_is_pseudo_fixed_link - Convenience function for testing if this
  614. * PHY is the CPU port facing side of an Ethernet switch, or similar.
  615. * @phydev: the phy_device struct
  616. */
  617. static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
  618. {
  619. return phydev->is_pseudo_fixed_link;
  620. }
  621. /**
  622. * phy_write_mmd - Convenience function for writing a register
  623. * on an MMD on a given PHY.
  624. * @phydev: The phy_device struct
  625. * @devad: The MMD to read from
  626. * @regnum: The register on the MMD to read
  627. * @val: value to write to @regnum
  628. *
  629. * Same rules as for phy_write();
  630. */
  631. static inline int phy_write_mmd(struct phy_device *phydev, int devad,
  632. u32 regnum, u16 val)
  633. {
  634. if (!phydev->is_c45)
  635. return -EOPNOTSUPP;
  636. regnum = MII_ADDR_C45 | ((devad & 0x1f) << 16) | (regnum & 0xffff);
  637. return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
  638. }
  639. /**
  640. * phy_write_mmd_indirect - writes data to the MMD registers
  641. * @phydev: The PHY device
  642. * @prtad: MMD Address
  643. * @devad: MMD DEVAD
  644. * @addr: PHY address on the MII bus
  645. * @data: data to write in the MMD register
  646. *
  647. * Description: Write data from the MMD registers of the specified
  648. * phy address.
  649. */
  650. void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
  651. int devad, int addr, u32 data);
  652. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
  653. bool is_c45,
  654. struct phy_c45_device_ids *c45_ids);
  655. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
  656. int phy_device_register(struct phy_device *phy);
  657. void phy_device_remove(struct phy_device *phydev);
  658. int phy_init_hw(struct phy_device *phydev);
  659. int phy_suspend(struct phy_device *phydev);
  660. int phy_resume(struct phy_device *phydev);
  661. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  662. phy_interface_t interface);
  663. struct phy_device *phy_find_first(struct mii_bus *bus);
  664. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  665. u32 flags, phy_interface_t interface);
  666. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  667. void (*handler)(struct net_device *),
  668. phy_interface_t interface);
  669. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  670. void (*handler)(struct net_device *),
  671. phy_interface_t interface);
  672. void phy_disconnect(struct phy_device *phydev);
  673. void phy_detach(struct phy_device *phydev);
  674. void phy_start(struct phy_device *phydev);
  675. void phy_stop(struct phy_device *phydev);
  676. int phy_start_aneg(struct phy_device *phydev);
  677. int phy_stop_interrupts(struct phy_device *phydev);
  678. static inline int phy_read_status(struct phy_device *phydev)
  679. {
  680. return phydev->drv->read_status(phydev);
  681. }
  682. int genphy_config_init(struct phy_device *phydev);
  683. int genphy_setup_forced(struct phy_device *phydev);
  684. int genphy_restart_aneg(struct phy_device *phydev);
  685. int genphy_config_aneg(struct phy_device *phydev);
  686. int genphy_aneg_done(struct phy_device *phydev);
  687. int genphy_update_link(struct phy_device *phydev);
  688. int genphy_read_status(struct phy_device *phydev);
  689. int genphy_suspend(struct phy_device *phydev);
  690. int genphy_resume(struct phy_device *phydev);
  691. int genphy_soft_reset(struct phy_device *phydev);
  692. void phy_driver_unregister(struct phy_driver *drv);
  693. void phy_drivers_unregister(struct phy_driver *drv, int n);
  694. int phy_driver_register(struct phy_driver *new_driver);
  695. int phy_drivers_register(struct phy_driver *new_driver, int n);
  696. void phy_state_machine(struct work_struct *work);
  697. void phy_change(struct work_struct *work);
  698. void phy_mac_interrupt(struct phy_device *phydev, int new_link);
  699. void phy_start_machine(struct phy_device *phydev);
  700. void phy_stop_machine(struct phy_device *phydev);
  701. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
  702. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
  703. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
  704. int phy_start_interrupts(struct phy_device *phydev);
  705. void phy_print_status(struct phy_device *phydev);
  706. void phy_device_free(struct phy_device *phydev);
  707. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
  708. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  709. int (*run)(struct phy_device *));
  710. int phy_register_fixup_for_id(const char *bus_id,
  711. int (*run)(struct phy_device *));
  712. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  713. int (*run)(struct phy_device *));
  714. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
  715. int phy_get_eee_err(struct phy_device *phydev);
  716. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
  717. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
  718. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
  719. void phy_ethtool_get_wol(struct phy_device *phydev,
  720. struct ethtool_wolinfo *wol);
  721. int __init mdio_bus_init(void);
  722. void mdio_bus_exit(void);
  723. extern struct bus_type mdio_bus_type;
  724. /**
  725. * module_phy_driver() - Helper macro for registering PHY drivers
  726. * @__phy_drivers: array of PHY drivers to register
  727. *
  728. * Helper macro for PHY drivers which do not do anything special in module
  729. * init/exit. Each module may only use this macro once, and calling it
  730. * replaces module_init() and module_exit().
  731. */
  732. #define phy_module_driver(__phy_drivers, __count) \
  733. static int __init phy_module_init(void) \
  734. { \
  735. return phy_drivers_register(__phy_drivers, __count); \
  736. } \
  737. module_init(phy_module_init); \
  738. static void __exit phy_module_exit(void) \
  739. { \
  740. phy_drivers_unregister(__phy_drivers, __count); \
  741. } \
  742. module_exit(phy_module_exit)
  743. #define module_phy_driver(__phy_drivers) \
  744. phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
  745. #endif /* __PHY_H */