phy.h 29 KB

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