phy.h 29 KB

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