phy.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* Framework for configuring and reading PHY devices
  2. * Based on code in sungem_phy.c and gianfar_phy.c
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/unistd.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/mii.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/phy.h>
  30. #include <linux/timer.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/mdio.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/atomic.h>
  36. #include <asm/irq.h>
  37. static const char *phy_speed_to_str(int speed)
  38. {
  39. switch (speed) {
  40. case SPEED_10:
  41. return "10Mbps";
  42. case SPEED_100:
  43. return "100Mbps";
  44. case SPEED_1000:
  45. return "1Gbps";
  46. case SPEED_2500:
  47. return "2.5Gbps";
  48. case SPEED_10000:
  49. return "10Gbps";
  50. case SPEED_UNKNOWN:
  51. return "Unknown";
  52. default:
  53. return "Unsupported (update phy.c)";
  54. }
  55. }
  56. /**
  57. * phy_print_status - Convenience function to print out the current phy status
  58. * @phydev: the phy_device struct
  59. */
  60. void phy_print_status(struct phy_device *phydev)
  61. {
  62. if (phydev->link) {
  63. netdev_info(phydev->attached_dev,
  64. "Link is Up - %s/%s - flow control %s\n",
  65. phy_speed_to_str(phydev->speed),
  66. DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
  67. phydev->pause ? "rx/tx" : "off");
  68. } else {
  69. netdev_info(phydev->attached_dev, "Link is Down\n");
  70. }
  71. }
  72. EXPORT_SYMBOL(phy_print_status);
  73. /**
  74. * phy_clear_interrupt - Ack the phy device's interrupt
  75. * @phydev: the phy_device struct
  76. *
  77. * If the @phydev driver has an ack_interrupt function, call it to
  78. * ack and clear the phy device's interrupt.
  79. *
  80. * Returns 0 on success on < 0 on error.
  81. */
  82. static int phy_clear_interrupt(struct phy_device *phydev)
  83. {
  84. if (phydev->drv->ack_interrupt)
  85. return phydev->drv->ack_interrupt(phydev);
  86. return 0;
  87. }
  88. /**
  89. * phy_config_interrupt - configure the PHY device for the requested interrupts
  90. * @phydev: the phy_device struct
  91. * @interrupts: interrupt flags to configure for this @phydev
  92. *
  93. * Returns 0 on success on < 0 on error.
  94. */
  95. static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
  96. {
  97. phydev->interrupts = interrupts;
  98. if (phydev->drv->config_intr)
  99. return phydev->drv->config_intr(phydev);
  100. return 0;
  101. }
  102. /**
  103. * phy_aneg_done - return auto-negotiation status
  104. * @phydev: target phy_device struct
  105. *
  106. * Description: Reads the status register and returns 0 either if
  107. * auto-negotiation is incomplete, or if there was an error.
  108. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  109. */
  110. static inline int phy_aneg_done(struct phy_device *phydev)
  111. {
  112. int retval = phy_read(phydev, MII_BMSR);
  113. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  114. }
  115. /* A structure for mapping a particular speed and duplex
  116. * combination to a particular SUPPORTED and ADVERTISED value
  117. */
  118. struct phy_setting {
  119. int speed;
  120. int duplex;
  121. u32 setting;
  122. };
  123. /* A mapping of all SUPPORTED settings to speed/duplex */
  124. static const struct phy_setting settings[] = {
  125. {
  126. .speed = 10000,
  127. .duplex = DUPLEX_FULL,
  128. .setting = SUPPORTED_10000baseT_Full,
  129. },
  130. {
  131. .speed = SPEED_1000,
  132. .duplex = DUPLEX_FULL,
  133. .setting = SUPPORTED_1000baseT_Full,
  134. },
  135. {
  136. .speed = SPEED_1000,
  137. .duplex = DUPLEX_HALF,
  138. .setting = SUPPORTED_1000baseT_Half,
  139. },
  140. {
  141. .speed = SPEED_100,
  142. .duplex = DUPLEX_FULL,
  143. .setting = SUPPORTED_100baseT_Full,
  144. },
  145. {
  146. .speed = SPEED_100,
  147. .duplex = DUPLEX_HALF,
  148. .setting = SUPPORTED_100baseT_Half,
  149. },
  150. {
  151. .speed = SPEED_10,
  152. .duplex = DUPLEX_FULL,
  153. .setting = SUPPORTED_10baseT_Full,
  154. },
  155. {
  156. .speed = SPEED_10,
  157. .duplex = DUPLEX_HALF,
  158. .setting = SUPPORTED_10baseT_Half,
  159. },
  160. };
  161. #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
  162. /**
  163. * phy_find_setting - find a PHY settings array entry that matches speed & duplex
  164. * @speed: speed to match
  165. * @duplex: duplex to match
  166. *
  167. * Description: Searches the settings array for the setting which
  168. * matches the desired speed and duplex, and returns the index
  169. * of that setting. Returns the index of the last setting if
  170. * none of the others match.
  171. */
  172. static inline int phy_find_setting(int speed, int duplex)
  173. {
  174. int idx = 0;
  175. while (idx < ARRAY_SIZE(settings) &&
  176. (settings[idx].speed != speed || settings[idx].duplex != duplex))
  177. idx++;
  178. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  179. }
  180. /**
  181. * phy_find_valid - find a PHY setting that matches the requested features mask
  182. * @idx: The first index in settings[] to search
  183. * @features: A mask of the valid settings
  184. *
  185. * Description: Returns the index of the first valid setting less
  186. * than or equal to the one pointed to by idx, as determined by
  187. * the mask in features. Returns the index of the last setting
  188. * if nothing else matches.
  189. */
  190. static inline int phy_find_valid(int idx, u32 features)
  191. {
  192. while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
  193. idx++;
  194. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  195. }
  196. /**
  197. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  198. * @phydev: the target phy_device struct
  199. *
  200. * Description: Make sure the PHY is set to supported speeds and
  201. * duplexes. Drop down by one in this order: 1000/FULL,
  202. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  203. */
  204. static void phy_sanitize_settings(struct phy_device *phydev)
  205. {
  206. u32 features = phydev->supported;
  207. int idx;
  208. /* Sanitize settings based on PHY capabilities */
  209. if ((features & SUPPORTED_Autoneg) == 0)
  210. phydev->autoneg = AUTONEG_DISABLE;
  211. idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
  212. features);
  213. phydev->speed = settings[idx].speed;
  214. phydev->duplex = settings[idx].duplex;
  215. }
  216. /**
  217. * phy_ethtool_sset - generic ethtool sset function, handles all the details
  218. * @phydev: target phy_device struct
  219. * @cmd: ethtool_cmd
  220. *
  221. * A few notes about parameter checking:
  222. * - We don't set port or transceiver, so we don't care what they
  223. * were set to.
  224. * - phy_start_aneg() will make sure forced settings are sane, and
  225. * choose the next best ones from the ones selected, so we don't
  226. * care if ethtool tries to give us bad values.
  227. */
  228. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  229. {
  230. u32 speed = ethtool_cmd_speed(cmd);
  231. if (cmd->phy_address != phydev->addr)
  232. return -EINVAL;
  233. /* We make sure that we don't pass unsupported values in to the PHY */
  234. cmd->advertising &= phydev->supported;
  235. /* Verify the settings we care about. */
  236. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  237. return -EINVAL;
  238. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  239. return -EINVAL;
  240. if (cmd->autoneg == AUTONEG_DISABLE &&
  241. ((speed != SPEED_1000 &&
  242. speed != SPEED_100 &&
  243. speed != SPEED_10) ||
  244. (cmd->duplex != DUPLEX_HALF &&
  245. cmd->duplex != DUPLEX_FULL)))
  246. return -EINVAL;
  247. phydev->autoneg = cmd->autoneg;
  248. phydev->speed = speed;
  249. phydev->advertising = cmd->advertising;
  250. if (AUTONEG_ENABLE == cmd->autoneg)
  251. phydev->advertising |= ADVERTISED_Autoneg;
  252. else
  253. phydev->advertising &= ~ADVERTISED_Autoneg;
  254. phydev->duplex = cmd->duplex;
  255. /* Restart the PHY */
  256. phy_start_aneg(phydev);
  257. return 0;
  258. }
  259. EXPORT_SYMBOL(phy_ethtool_sset);
  260. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  261. {
  262. cmd->supported = phydev->supported;
  263. cmd->advertising = phydev->advertising;
  264. cmd->lp_advertising = phydev->lp_advertising;
  265. ethtool_cmd_speed_set(cmd, phydev->speed);
  266. cmd->duplex = phydev->duplex;
  267. cmd->port = PORT_MII;
  268. cmd->phy_address = phydev->addr;
  269. cmd->transceiver = phy_is_internal(phydev) ?
  270. XCVR_INTERNAL : XCVR_EXTERNAL;
  271. cmd->autoneg = phydev->autoneg;
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(phy_ethtool_gset);
  275. /**
  276. * phy_mii_ioctl - generic PHY MII ioctl interface
  277. * @phydev: the phy_device struct
  278. * @ifr: &struct ifreq for socket ioctl's
  279. * @cmd: ioctl cmd to execute
  280. *
  281. * Note that this function is currently incompatible with the
  282. * PHYCONTROL layer. It changes registers without regard to
  283. * current state. Use at own risk.
  284. */
  285. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  286. {
  287. struct mii_ioctl_data *mii_data = if_mii(ifr);
  288. u16 val = mii_data->val_in;
  289. switch (cmd) {
  290. case SIOCGMIIPHY:
  291. mii_data->phy_id = phydev->addr;
  292. /* fall through */
  293. case SIOCGMIIREG:
  294. mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
  295. mii_data->reg_num);
  296. return 0;
  297. case SIOCSMIIREG:
  298. if (mii_data->phy_id == phydev->addr) {
  299. switch (mii_data->reg_num) {
  300. case MII_BMCR:
  301. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
  302. phydev->autoneg = AUTONEG_DISABLE;
  303. else
  304. phydev->autoneg = AUTONEG_ENABLE;
  305. if (!phydev->autoneg && (val & BMCR_FULLDPLX))
  306. phydev->duplex = DUPLEX_FULL;
  307. else
  308. phydev->duplex = DUPLEX_HALF;
  309. if (!phydev->autoneg && (val & BMCR_SPEED1000))
  310. phydev->speed = SPEED_1000;
  311. else if (!phydev->autoneg &&
  312. (val & BMCR_SPEED100))
  313. phydev->speed = SPEED_100;
  314. break;
  315. case MII_ADVERTISE:
  316. phydev->advertising = val;
  317. break;
  318. default:
  319. /* do nothing */
  320. break;
  321. }
  322. }
  323. mdiobus_write(phydev->bus, mii_data->phy_id,
  324. mii_data->reg_num, val);
  325. if (mii_data->reg_num == MII_BMCR &&
  326. val & BMCR_RESET)
  327. return phy_init_hw(phydev);
  328. return 0;
  329. case SIOCSHWTSTAMP:
  330. if (phydev->drv->hwtstamp)
  331. return phydev->drv->hwtstamp(phydev, ifr);
  332. /* fall through */
  333. default:
  334. return -EOPNOTSUPP;
  335. }
  336. }
  337. EXPORT_SYMBOL(phy_mii_ioctl);
  338. /**
  339. * phy_start_aneg - start auto-negotiation for this PHY device
  340. * @phydev: the phy_device struct
  341. *
  342. * Description: Sanitizes the settings (if we're not autonegotiating
  343. * them), and then calls the driver's config_aneg function.
  344. * If the PHYCONTROL Layer is operating, we change the state to
  345. * reflect the beginning of Auto-negotiation or forcing.
  346. */
  347. int phy_start_aneg(struct phy_device *phydev)
  348. {
  349. int err;
  350. mutex_lock(&phydev->lock);
  351. if (AUTONEG_DISABLE == phydev->autoneg)
  352. phy_sanitize_settings(phydev);
  353. err = phydev->drv->config_aneg(phydev);
  354. if (err < 0)
  355. goto out_unlock;
  356. if (phydev->state != PHY_HALTED) {
  357. if (AUTONEG_ENABLE == phydev->autoneg) {
  358. phydev->state = PHY_AN;
  359. phydev->link_timeout = PHY_AN_TIMEOUT;
  360. } else {
  361. phydev->state = PHY_FORCING;
  362. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  363. }
  364. }
  365. out_unlock:
  366. mutex_unlock(&phydev->lock);
  367. return err;
  368. }
  369. EXPORT_SYMBOL(phy_start_aneg);
  370. /**
  371. * phy_start_machine - start PHY state machine tracking
  372. * @phydev: the phy_device struct
  373. *
  374. * Description: The PHY infrastructure can run a state machine
  375. * which tracks whether the PHY is starting up, negotiating,
  376. * etc. This function starts the timer which tracks the state
  377. * of the PHY. If you want to maintain your own state machine,
  378. * do not call this function.
  379. */
  380. void phy_start_machine(struct phy_device *phydev)
  381. {
  382. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
  383. }
  384. /**
  385. * phy_stop_machine - stop the PHY state machine tracking
  386. * @phydev: target phy_device struct
  387. *
  388. * Description: Stops the state machine timer, sets the state to UP
  389. * (unless it wasn't up yet). This function must be called BEFORE
  390. * phy_detach.
  391. */
  392. void phy_stop_machine(struct phy_device *phydev)
  393. {
  394. cancel_delayed_work_sync(&phydev->state_queue);
  395. mutex_lock(&phydev->lock);
  396. if (phydev->state > PHY_UP)
  397. phydev->state = PHY_UP;
  398. mutex_unlock(&phydev->lock);
  399. }
  400. /**
  401. * phy_error - enter HALTED state for this PHY device
  402. * @phydev: target phy_device struct
  403. *
  404. * Moves the PHY to the HALTED state in response to a read
  405. * or write error, and tells the controller the link is down.
  406. * Must not be called from interrupt context, or while the
  407. * phydev->lock is held.
  408. */
  409. static void phy_error(struct phy_device *phydev)
  410. {
  411. mutex_lock(&phydev->lock);
  412. phydev->state = PHY_HALTED;
  413. mutex_unlock(&phydev->lock);
  414. }
  415. /**
  416. * phy_interrupt - PHY interrupt handler
  417. * @irq: interrupt line
  418. * @phy_dat: phy_device pointer
  419. *
  420. * Description: When a PHY interrupt occurs, the handler disables
  421. * interrupts, and schedules a work task to clear the interrupt.
  422. */
  423. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  424. {
  425. struct phy_device *phydev = phy_dat;
  426. if (PHY_HALTED == phydev->state)
  427. return IRQ_NONE; /* It can't be ours. */
  428. /* The MDIO bus is not allowed to be written in interrupt
  429. * context, so we need to disable the irq here. A work
  430. * queue will write the PHY to disable and clear the
  431. * interrupt, and then reenable the irq line.
  432. */
  433. disable_irq_nosync(irq);
  434. atomic_inc(&phydev->irq_disable);
  435. queue_work(system_power_efficient_wq, &phydev->phy_queue);
  436. return IRQ_HANDLED;
  437. }
  438. /**
  439. * phy_enable_interrupts - Enable the interrupts from the PHY side
  440. * @phydev: target phy_device struct
  441. */
  442. static int phy_enable_interrupts(struct phy_device *phydev)
  443. {
  444. int err = phy_clear_interrupt(phydev);
  445. if (err < 0)
  446. return err;
  447. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  448. }
  449. /**
  450. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  451. * @phydev: target phy_device struct
  452. */
  453. static int phy_disable_interrupts(struct phy_device *phydev)
  454. {
  455. int err;
  456. /* Disable PHY interrupts */
  457. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  458. if (err)
  459. goto phy_err;
  460. /* Clear the interrupt */
  461. err = phy_clear_interrupt(phydev);
  462. if (err)
  463. goto phy_err;
  464. return 0;
  465. phy_err:
  466. phy_error(phydev);
  467. return err;
  468. }
  469. /**
  470. * phy_start_interrupts - request and enable interrupts for a PHY device
  471. * @phydev: target phy_device struct
  472. *
  473. * Description: Request the interrupt for the given PHY.
  474. * If this fails, then we set irq to PHY_POLL.
  475. * Otherwise, we enable the interrupts in the PHY.
  476. * This should only be called with a valid IRQ number.
  477. * Returns 0 on success or < 0 on error.
  478. */
  479. int phy_start_interrupts(struct phy_device *phydev)
  480. {
  481. atomic_set(&phydev->irq_disable, 0);
  482. if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt",
  483. phydev) < 0) {
  484. pr_warn("%s: Can't get IRQ %d (PHY)\n",
  485. phydev->bus->name, phydev->irq);
  486. phydev->irq = PHY_POLL;
  487. return 0;
  488. }
  489. return phy_enable_interrupts(phydev);
  490. }
  491. EXPORT_SYMBOL(phy_start_interrupts);
  492. /**
  493. * phy_stop_interrupts - disable interrupts from a PHY device
  494. * @phydev: target phy_device struct
  495. */
  496. int phy_stop_interrupts(struct phy_device *phydev)
  497. {
  498. int err = phy_disable_interrupts(phydev);
  499. if (err)
  500. phy_error(phydev);
  501. free_irq(phydev->irq, phydev);
  502. /* Cannot call flush_scheduled_work() here as desired because
  503. * of rtnl_lock(), but we do not really care about what would
  504. * be done, except from enable_irq(), so cancel any work
  505. * possibly pending and take care of the matter below.
  506. */
  507. cancel_work_sync(&phydev->phy_queue);
  508. /* If work indeed has been cancelled, disable_irq() will have
  509. * been left unbalanced from phy_interrupt() and enable_irq()
  510. * has to be called so that other devices on the line work.
  511. */
  512. while (atomic_dec_return(&phydev->irq_disable) >= 0)
  513. enable_irq(phydev->irq);
  514. return err;
  515. }
  516. EXPORT_SYMBOL(phy_stop_interrupts);
  517. /**
  518. * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
  519. * @work: work_struct that describes the work to be done
  520. */
  521. void phy_change(struct work_struct *work)
  522. {
  523. struct phy_device *phydev =
  524. container_of(work, struct phy_device, phy_queue);
  525. if (phydev->drv->did_interrupt &&
  526. !phydev->drv->did_interrupt(phydev))
  527. goto ignore;
  528. if (phy_disable_interrupts(phydev))
  529. goto phy_err;
  530. mutex_lock(&phydev->lock);
  531. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  532. phydev->state = PHY_CHANGELINK;
  533. mutex_unlock(&phydev->lock);
  534. atomic_dec(&phydev->irq_disable);
  535. enable_irq(phydev->irq);
  536. /* Reenable interrupts */
  537. if (PHY_HALTED != phydev->state &&
  538. phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
  539. goto irq_enable_err;
  540. /* reschedule state queue work to run as soon as possible */
  541. cancel_delayed_work_sync(&phydev->state_queue);
  542. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  543. return;
  544. ignore:
  545. atomic_dec(&phydev->irq_disable);
  546. enable_irq(phydev->irq);
  547. return;
  548. irq_enable_err:
  549. disable_irq(phydev->irq);
  550. atomic_inc(&phydev->irq_disable);
  551. phy_err:
  552. phy_error(phydev);
  553. }
  554. /**
  555. * phy_stop - Bring down the PHY link, and stop checking the status
  556. * @phydev: target phy_device struct
  557. */
  558. void phy_stop(struct phy_device *phydev)
  559. {
  560. mutex_lock(&phydev->lock);
  561. if (PHY_HALTED == phydev->state)
  562. goto out_unlock;
  563. if (phy_interrupt_is_valid(phydev)) {
  564. /* Disable PHY Interrupts */
  565. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  566. /* Clear any pending interrupts */
  567. phy_clear_interrupt(phydev);
  568. }
  569. phydev->state = PHY_HALTED;
  570. out_unlock:
  571. mutex_unlock(&phydev->lock);
  572. /* Cannot call flush_scheduled_work() here as desired because
  573. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  574. * will not reenable interrupts.
  575. */
  576. }
  577. EXPORT_SYMBOL(phy_stop);
  578. /**
  579. * phy_start - start or restart a PHY device
  580. * @phydev: target phy_device struct
  581. *
  582. * Description: Indicates the attached device's readiness to
  583. * handle PHY-related work. Used during startup to start the
  584. * PHY, and after a call to phy_stop() to resume operation.
  585. * Also used to indicate the MDIO bus has cleared an error
  586. * condition.
  587. */
  588. void phy_start(struct phy_device *phydev)
  589. {
  590. mutex_lock(&phydev->lock);
  591. switch (phydev->state) {
  592. case PHY_STARTING:
  593. phydev->state = PHY_PENDING;
  594. break;
  595. case PHY_READY:
  596. phydev->state = PHY_UP;
  597. break;
  598. case PHY_HALTED:
  599. phydev->state = PHY_RESUMING;
  600. default:
  601. break;
  602. }
  603. mutex_unlock(&phydev->lock);
  604. }
  605. EXPORT_SYMBOL(phy_start);
  606. /**
  607. * phy_state_machine - Handle the state machine
  608. * @work: work_struct that describes the work to be done
  609. */
  610. void phy_state_machine(struct work_struct *work)
  611. {
  612. struct delayed_work *dwork = to_delayed_work(work);
  613. struct phy_device *phydev =
  614. container_of(dwork, struct phy_device, state_queue);
  615. int needs_aneg = 0, do_suspend = 0;
  616. int err = 0;
  617. mutex_lock(&phydev->lock);
  618. switch (phydev->state) {
  619. case PHY_DOWN:
  620. case PHY_STARTING:
  621. case PHY_READY:
  622. case PHY_PENDING:
  623. break;
  624. case PHY_UP:
  625. needs_aneg = 1;
  626. phydev->link_timeout = PHY_AN_TIMEOUT;
  627. break;
  628. case PHY_AN:
  629. err = phy_read_status(phydev);
  630. if (err < 0)
  631. break;
  632. /* If the link is down, give up on negotiation for now */
  633. if (!phydev->link) {
  634. phydev->state = PHY_NOLINK;
  635. netif_carrier_off(phydev->attached_dev);
  636. phydev->adjust_link(phydev->attached_dev);
  637. break;
  638. }
  639. /* Check if negotiation is done. Break if there's an error */
  640. err = phy_aneg_done(phydev);
  641. if (err < 0)
  642. break;
  643. /* If AN is done, we're running */
  644. if (err > 0) {
  645. phydev->state = PHY_RUNNING;
  646. netif_carrier_on(phydev->attached_dev);
  647. phydev->adjust_link(phydev->attached_dev);
  648. } else if (0 == phydev->link_timeout--) {
  649. needs_aneg = 1;
  650. /* If we have the magic_aneg bit, we try again */
  651. if (phydev->drv->flags & PHY_HAS_MAGICANEG)
  652. break;
  653. }
  654. break;
  655. case PHY_NOLINK:
  656. err = phy_read_status(phydev);
  657. if (err)
  658. break;
  659. if (phydev->link) {
  660. phydev->state = PHY_RUNNING;
  661. netif_carrier_on(phydev->attached_dev);
  662. phydev->adjust_link(phydev->attached_dev);
  663. }
  664. break;
  665. case PHY_FORCING:
  666. err = genphy_update_link(phydev);
  667. if (err)
  668. break;
  669. if (phydev->link) {
  670. phydev->state = PHY_RUNNING;
  671. netif_carrier_on(phydev->attached_dev);
  672. } else {
  673. if (0 == phydev->link_timeout--)
  674. needs_aneg = 1;
  675. }
  676. phydev->adjust_link(phydev->attached_dev);
  677. break;
  678. case PHY_RUNNING:
  679. /* Only register a CHANGE if we are
  680. * polling or ignoring interrupts
  681. */
  682. if (!phy_interrupt_is_valid(phydev))
  683. phydev->state = PHY_CHANGELINK;
  684. break;
  685. case PHY_CHANGELINK:
  686. err = phy_read_status(phydev);
  687. if (err)
  688. break;
  689. if (phydev->link) {
  690. phydev->state = PHY_RUNNING;
  691. netif_carrier_on(phydev->attached_dev);
  692. } else {
  693. phydev->state = PHY_NOLINK;
  694. netif_carrier_off(phydev->attached_dev);
  695. }
  696. phydev->adjust_link(phydev->attached_dev);
  697. if (phy_interrupt_is_valid(phydev))
  698. err = phy_config_interrupt(phydev,
  699. PHY_INTERRUPT_ENABLED);
  700. break;
  701. case PHY_HALTED:
  702. if (phydev->link) {
  703. phydev->link = 0;
  704. netif_carrier_off(phydev->attached_dev);
  705. phydev->adjust_link(phydev->attached_dev);
  706. do_suspend = 1;
  707. }
  708. break;
  709. case PHY_RESUMING:
  710. err = phy_clear_interrupt(phydev);
  711. if (err)
  712. break;
  713. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  714. if (err)
  715. break;
  716. if (AUTONEG_ENABLE == phydev->autoneg) {
  717. err = phy_aneg_done(phydev);
  718. if (err < 0)
  719. break;
  720. /* err > 0 if AN is done.
  721. * Otherwise, it's 0, and we're still waiting for AN
  722. */
  723. if (err > 0) {
  724. err = phy_read_status(phydev);
  725. if (err)
  726. break;
  727. if (phydev->link) {
  728. phydev->state = PHY_RUNNING;
  729. netif_carrier_on(phydev->attached_dev);
  730. } else {
  731. phydev->state = PHY_NOLINK;
  732. }
  733. phydev->adjust_link(phydev->attached_dev);
  734. } else {
  735. phydev->state = PHY_AN;
  736. phydev->link_timeout = PHY_AN_TIMEOUT;
  737. }
  738. } else {
  739. err = phy_read_status(phydev);
  740. if (err)
  741. break;
  742. if (phydev->link) {
  743. phydev->state = PHY_RUNNING;
  744. netif_carrier_on(phydev->attached_dev);
  745. } else {
  746. phydev->state = PHY_NOLINK;
  747. }
  748. phydev->adjust_link(phydev->attached_dev);
  749. }
  750. break;
  751. }
  752. mutex_unlock(&phydev->lock);
  753. if (needs_aneg)
  754. err = phy_start_aneg(phydev);
  755. if (do_suspend)
  756. phy_suspend(phydev);
  757. if (err < 0)
  758. phy_error(phydev);
  759. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  760. PHY_STATE_TIME * HZ);
  761. }
  762. void phy_mac_interrupt(struct phy_device *phydev, int new_link)
  763. {
  764. cancel_work_sync(&phydev->phy_queue);
  765. phydev->link = new_link;
  766. schedule_work(&phydev->phy_queue);
  767. }
  768. EXPORT_SYMBOL(phy_mac_interrupt);
  769. static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
  770. int addr)
  771. {
  772. /* Write the desired MMD Devad */
  773. bus->write(bus, addr, MII_MMD_CTRL, devad);
  774. /* Write the desired MMD register address */
  775. bus->write(bus, addr, MII_MMD_DATA, prtad);
  776. /* Select the Function : DATA with no post increment */
  777. bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  778. }
  779. /**
  780. * phy_read_mmd_indirect - reads data from the MMD registers
  781. * @bus: the target MII bus
  782. * @prtad: MMD Address
  783. * @devad: MMD DEVAD
  784. * @addr: PHY address on the MII bus
  785. *
  786. * Description: it reads data from the MMD registers (clause 22 to access to
  787. * clause 45) of the specified phy address.
  788. * To read these register we have:
  789. * 1) Write reg 13 // DEVAD
  790. * 2) Write reg 14 // MMD Address
  791. * 3) Write reg 13 // MMD Data Command for MMD DEVAD
  792. * 3) Read reg 14 // Read MMD data
  793. */
  794. static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
  795. int addr)
  796. {
  797. mmd_phy_indirect(bus, prtad, devad, addr);
  798. /* Read the content of the MMD's selected register */
  799. return bus->read(bus, addr, MII_MMD_DATA);
  800. }
  801. /**
  802. * phy_write_mmd_indirect - writes data to the MMD registers
  803. * @bus: the target MII bus
  804. * @prtad: MMD Address
  805. * @devad: MMD DEVAD
  806. * @addr: PHY address on the MII bus
  807. * @data: data to write in the MMD register
  808. *
  809. * Description: Write data from the MMD registers of the specified
  810. * phy address.
  811. * To write these register we have:
  812. * 1) Write reg 13 // DEVAD
  813. * 2) Write reg 14 // MMD Address
  814. * 3) Write reg 13 // MMD Data Command for MMD DEVAD
  815. * 3) Write reg 14 // Write MMD data
  816. */
  817. static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
  818. int addr, u32 data)
  819. {
  820. mmd_phy_indirect(bus, prtad, devad, addr);
  821. /* Write the data into MMD's selected register */
  822. bus->write(bus, addr, MII_MMD_DATA, data);
  823. }
  824. /**
  825. * phy_init_eee - init and check the EEE feature
  826. * @phydev: target phy_device struct
  827. * @clk_stop_enable: PHY may stop the clock during LPI
  828. *
  829. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  830. * is supported by looking at the MMD registers 3.20 and 7.60/61
  831. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  832. * bit if required.
  833. */
  834. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  835. {
  836. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  837. * Also EEE feature is active when core is operating with MII, GMII
  838. * or RGMII.
  839. */
  840. if ((phydev->duplex == DUPLEX_FULL) &&
  841. ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
  842. (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
  843. (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
  844. int eee_lp, eee_cap, eee_adv;
  845. u32 lp, cap, adv;
  846. int idx, status;
  847. /* Read phy status to properly get the right settings */
  848. status = phy_read_status(phydev);
  849. if (status)
  850. return status;
  851. /* First check if the EEE ability is supported */
  852. eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
  853. MDIO_MMD_PCS, phydev->addr);
  854. if (eee_cap < 0)
  855. return eee_cap;
  856. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  857. if (!cap)
  858. return -EPROTONOSUPPORT;
  859. /* Check which link settings negotiated and verify it in
  860. * the EEE advertising registers.
  861. */
  862. eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
  863. MDIO_MMD_AN, phydev->addr);
  864. if (eee_lp < 0)
  865. return eee_lp;
  866. eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
  867. MDIO_MMD_AN, phydev->addr);
  868. if (eee_adv < 0)
  869. return eee_adv;
  870. adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
  871. lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
  872. idx = phy_find_setting(phydev->speed, phydev->duplex);
  873. if (!(lp & adv & settings[idx].setting))
  874. return -EPROTONOSUPPORT;
  875. if (clk_stop_enable) {
  876. /* Configure the PHY to stop receiving xMII
  877. * clock while it is signaling LPI.
  878. */
  879. int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
  880. MDIO_MMD_PCS,
  881. phydev->addr);
  882. if (val < 0)
  883. return val;
  884. val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
  885. phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
  886. MDIO_MMD_PCS, phydev->addr, val);
  887. }
  888. return 0; /* EEE supported */
  889. }
  890. return -EPROTONOSUPPORT;
  891. }
  892. EXPORT_SYMBOL(phy_init_eee);
  893. /**
  894. * phy_get_eee_err - report the EEE wake error count
  895. * @phydev: target phy_device struct
  896. *
  897. * Description: it is to report the number of time where the PHY
  898. * failed to complete its normal wake sequence.
  899. */
  900. int phy_get_eee_err(struct phy_device *phydev)
  901. {
  902. return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
  903. MDIO_MMD_PCS, phydev->addr);
  904. }
  905. EXPORT_SYMBOL(phy_get_eee_err);
  906. /**
  907. * phy_ethtool_get_eee - get EEE supported and status
  908. * @phydev: target phy_device struct
  909. * @data: ethtool_eee data
  910. *
  911. * Description: it reportes the Supported/Advertisement/LP Advertisement
  912. * capabilities.
  913. */
  914. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  915. {
  916. int val;
  917. /* Get Supported EEE */
  918. val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
  919. MDIO_MMD_PCS, phydev->addr);
  920. if (val < 0)
  921. return val;
  922. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  923. /* Get advertisement EEE */
  924. val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
  925. MDIO_MMD_AN, phydev->addr);
  926. if (val < 0)
  927. return val;
  928. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  929. /* Get LP advertisement EEE */
  930. val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
  931. MDIO_MMD_AN, phydev->addr);
  932. if (val < 0)
  933. return val;
  934. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  935. return 0;
  936. }
  937. EXPORT_SYMBOL(phy_ethtool_get_eee);
  938. /**
  939. * phy_ethtool_set_eee - set EEE supported and status
  940. * @phydev: target phy_device struct
  941. * @data: ethtool_eee data
  942. *
  943. * Description: it is to program the Advertisement EEE register.
  944. */
  945. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  946. {
  947. int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
  948. phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
  949. phydev->addr, val);
  950. return 0;
  951. }
  952. EXPORT_SYMBOL(phy_ethtool_set_eee);
  953. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  954. {
  955. if (phydev->drv->set_wol)
  956. return phydev->drv->set_wol(phydev, wol);
  957. return -EOPNOTSUPP;
  958. }
  959. EXPORT_SYMBOL(phy_ethtool_set_wol);
  960. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  961. {
  962. if (phydev->drv->get_wol)
  963. phydev->drv->get_wol(phydev, wol);
  964. }
  965. EXPORT_SYMBOL(phy_ethtool_get_wol);