phy.h 31 KB

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