phy.h 29 KB

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