phy.h 28 KB

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