phy.c 28 KB

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