phy.c 28 KB

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