phy.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 = phy_is_internal(phydev) ?
  256. XCVR_INTERNAL : XCVR_EXTERNAL;
  257. cmd->autoneg = phydev->autoneg;
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(phy_ethtool_gset);
  261. /**
  262. * phy_mii_ioctl - generic PHY MII ioctl interface
  263. * @phydev: the phy_device struct
  264. * @ifr: &struct ifreq for socket ioctl's
  265. * @cmd: ioctl cmd to execute
  266. *
  267. * Note that this function is currently incompatible with the
  268. * PHYCONTROL layer. It changes registers without regard to
  269. * current state. Use at own risk.
  270. */
  271. int phy_mii_ioctl(struct phy_device *phydev,
  272. struct ifreq *ifr, int cmd)
  273. {
  274. struct mii_ioctl_data *mii_data = if_mii(ifr);
  275. u16 val = mii_data->val_in;
  276. switch (cmd) {
  277. case SIOCGMIIPHY:
  278. mii_data->phy_id = phydev->addr;
  279. /* fall through */
  280. case SIOCGMIIREG:
  281. mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
  282. mii_data->reg_num);
  283. break;
  284. case SIOCSMIIREG:
  285. if (mii_data->phy_id == phydev->addr) {
  286. switch(mii_data->reg_num) {
  287. case MII_BMCR:
  288. if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0)
  289. phydev->autoneg = AUTONEG_DISABLE;
  290. else
  291. phydev->autoneg = AUTONEG_ENABLE;
  292. if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
  293. phydev->duplex = DUPLEX_FULL;
  294. else
  295. phydev->duplex = DUPLEX_HALF;
  296. if ((!phydev->autoneg) &&
  297. (val & BMCR_SPEED1000))
  298. phydev->speed = SPEED_1000;
  299. else if ((!phydev->autoneg) &&
  300. (val & BMCR_SPEED100))
  301. phydev->speed = SPEED_100;
  302. break;
  303. case MII_ADVERTISE:
  304. phydev->advertising = val;
  305. break;
  306. default:
  307. /* do nothing */
  308. break;
  309. }
  310. }
  311. mdiobus_write(phydev->bus, mii_data->phy_id,
  312. mii_data->reg_num, val);
  313. if (mii_data->reg_num == MII_BMCR &&
  314. val & BMCR_RESET &&
  315. phydev->drv->config_init) {
  316. phy_scan_fixups(phydev);
  317. phydev->drv->config_init(phydev);
  318. }
  319. break;
  320. case SIOCSHWTSTAMP:
  321. if (phydev->drv->hwtstamp)
  322. return phydev->drv->hwtstamp(phydev, ifr);
  323. /* fall through */
  324. default:
  325. return -EOPNOTSUPP;
  326. }
  327. return 0;
  328. }
  329. EXPORT_SYMBOL(phy_mii_ioctl);
  330. /**
  331. * phy_start_aneg - start auto-negotiation for this PHY device
  332. * @phydev: the phy_device struct
  333. *
  334. * Description: Sanitizes the settings (if we're not autonegotiating
  335. * them), and then calls the driver's config_aneg function.
  336. * If the PHYCONTROL Layer is operating, we change the state to
  337. * reflect the beginning of Auto-negotiation or forcing.
  338. */
  339. int phy_start_aneg(struct phy_device *phydev)
  340. {
  341. int err;
  342. mutex_lock(&phydev->lock);
  343. if (AUTONEG_DISABLE == phydev->autoneg)
  344. phy_sanitize_settings(phydev);
  345. err = phydev->drv->config_aneg(phydev);
  346. if (err < 0)
  347. goto out_unlock;
  348. if (phydev->state != PHY_HALTED) {
  349. if (AUTONEG_ENABLE == phydev->autoneg) {
  350. phydev->state = PHY_AN;
  351. phydev->link_timeout = PHY_AN_TIMEOUT;
  352. } else {
  353. phydev->state = PHY_FORCING;
  354. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  355. }
  356. }
  357. out_unlock:
  358. mutex_unlock(&phydev->lock);
  359. return err;
  360. }
  361. EXPORT_SYMBOL(phy_start_aneg);
  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. queue_delayed_work(system_power_efficient_wq, &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. queue_work(system_power_efficient_wq, &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. 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. err = phy_enable_interrupts(phydev);
  490. return err;
  491. }
  492. EXPORT_SYMBOL(phy_start_interrupts);
  493. /**
  494. * phy_stop_interrupts - disable interrupts from a PHY device
  495. * @phydev: target phy_device struct
  496. */
  497. int phy_stop_interrupts(struct phy_device *phydev)
  498. {
  499. int err;
  500. err = phy_disable_interrupts(phydev);
  501. if (err)
  502. phy_error(phydev);
  503. free_irq(phydev->irq, phydev);
  504. /*
  505. * Cannot call flush_scheduled_work() here as desired because
  506. * of rtnl_lock(), but we do not really care about what would
  507. * be done, except from enable_irq(), so cancel any work
  508. * possibly pending and take care of the matter below.
  509. */
  510. cancel_work_sync(&phydev->phy_queue);
  511. /*
  512. * If work indeed has been cancelled, disable_irq() will have
  513. * been left unbalanced from phy_interrupt() and enable_irq()
  514. * has to be called so that other devices on the line work.
  515. */
  516. while (atomic_dec_return(&phydev->irq_disable) >= 0)
  517. enable_irq(phydev->irq);
  518. return err;
  519. }
  520. EXPORT_SYMBOL(phy_stop_interrupts);
  521. /**
  522. * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
  523. * @work: work_struct that describes the work to be done
  524. */
  525. void phy_change(struct work_struct *work)
  526. {
  527. int err;
  528. struct phy_device *phydev =
  529. container_of(work, struct phy_device, phy_queue);
  530. if (phydev->drv->did_interrupt &&
  531. !phydev->drv->did_interrupt(phydev))
  532. goto ignore;
  533. err = phy_disable_interrupts(phydev);
  534. if (err)
  535. goto phy_err;
  536. mutex_lock(&phydev->lock);
  537. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  538. phydev->state = PHY_CHANGELINK;
  539. mutex_unlock(&phydev->lock);
  540. atomic_dec(&phydev->irq_disable);
  541. enable_irq(phydev->irq);
  542. /* Reenable interrupts */
  543. if (PHY_HALTED != phydev->state)
  544. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  545. if (err)
  546. goto irq_enable_err;
  547. /* reschedule state queue work to run as soon as possible */
  548. cancel_delayed_work_sync(&phydev->state_queue);
  549. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  550. return;
  551. ignore:
  552. atomic_dec(&phydev->irq_disable);
  553. enable_irq(phydev->irq);
  554. return;
  555. irq_enable_err:
  556. disable_irq(phydev->irq);
  557. atomic_inc(&phydev->irq_disable);
  558. phy_err:
  559. phy_error(phydev);
  560. }
  561. /**
  562. * phy_stop - Bring down the PHY link, and stop checking the status
  563. * @phydev: target phy_device struct
  564. */
  565. void phy_stop(struct phy_device *phydev)
  566. {
  567. mutex_lock(&phydev->lock);
  568. if (PHY_HALTED == phydev->state)
  569. goto out_unlock;
  570. if (phy_interrupt_is_valid(phydev)) {
  571. /* Disable PHY Interrupts */
  572. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  573. /* Clear any pending interrupts */
  574. phy_clear_interrupt(phydev);
  575. }
  576. phydev->state = PHY_HALTED;
  577. out_unlock:
  578. mutex_unlock(&phydev->lock);
  579. /*
  580. * Cannot call flush_scheduled_work() here as desired because
  581. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  582. * will not reenable interrupts.
  583. */
  584. }
  585. /**
  586. * phy_start - start or restart a PHY device
  587. * @phydev: target phy_device struct
  588. *
  589. * Description: Indicates the attached device's readiness to
  590. * handle PHY-related work. Used during startup to start the
  591. * PHY, and after a call to phy_stop() to resume operation.
  592. * Also used to indicate the MDIO bus has cleared an error
  593. * condition.
  594. */
  595. void phy_start(struct phy_device *phydev)
  596. {
  597. mutex_lock(&phydev->lock);
  598. switch (phydev->state) {
  599. case PHY_STARTING:
  600. phydev->state = PHY_PENDING;
  601. break;
  602. case PHY_READY:
  603. phydev->state = PHY_UP;
  604. break;
  605. case PHY_HALTED:
  606. phydev->state = PHY_RESUMING;
  607. default:
  608. break;
  609. }
  610. mutex_unlock(&phydev->lock);
  611. }
  612. EXPORT_SYMBOL(phy_stop);
  613. EXPORT_SYMBOL(phy_start);
  614. /**
  615. * phy_state_machine - Handle the state machine
  616. * @work: work_struct that describes the work to be done
  617. */
  618. void phy_state_machine(struct work_struct *work)
  619. {
  620. struct delayed_work *dwork = to_delayed_work(work);
  621. struct phy_device *phydev =
  622. container_of(dwork, struct phy_device, state_queue);
  623. int needs_aneg = 0;
  624. int err = 0;
  625. mutex_lock(&phydev->lock);
  626. if (phydev->adjust_state)
  627. phydev->adjust_state(phydev->attached_dev);
  628. switch(phydev->state) {
  629. case PHY_DOWN:
  630. case PHY_STARTING:
  631. case PHY_READY:
  632. case PHY_PENDING:
  633. break;
  634. case PHY_UP:
  635. needs_aneg = 1;
  636. phydev->link_timeout = PHY_AN_TIMEOUT;
  637. break;
  638. case PHY_AN:
  639. err = phy_read_status(phydev);
  640. if (err < 0)
  641. break;
  642. /* If the link is down, give up on
  643. * negotiation for now */
  644. if (!phydev->link) {
  645. phydev->state = PHY_NOLINK;
  646. netif_carrier_off(phydev->attached_dev);
  647. phydev->adjust_link(phydev->attached_dev);
  648. break;
  649. }
  650. /* Check if negotiation is done. Break
  651. * if there's an error */
  652. err = phy_aneg_done(phydev);
  653. if (err < 0)
  654. break;
  655. /* If AN is done, we're running */
  656. if (err > 0) {
  657. phydev->state = PHY_RUNNING;
  658. netif_carrier_on(phydev->attached_dev);
  659. phydev->adjust_link(phydev->attached_dev);
  660. } else if (0 == phydev->link_timeout--) {
  661. needs_aneg = 1;
  662. /* If we have the magic_aneg bit,
  663. * we try again */
  664. if (phydev->drv->flags & PHY_HAS_MAGICANEG)
  665. break;
  666. }
  667. break;
  668. case PHY_NOLINK:
  669. err = phy_read_status(phydev);
  670. if (err)
  671. break;
  672. if (phydev->link) {
  673. phydev->state = PHY_RUNNING;
  674. netif_carrier_on(phydev->attached_dev);
  675. phydev->adjust_link(phydev->attached_dev);
  676. }
  677. break;
  678. case PHY_FORCING:
  679. err = genphy_update_link(phydev);
  680. if (err)
  681. break;
  682. if (phydev->link) {
  683. phydev->state = PHY_RUNNING;
  684. netif_carrier_on(phydev->attached_dev);
  685. } else {
  686. if (0 == phydev->link_timeout--)
  687. needs_aneg = 1;
  688. }
  689. phydev->adjust_link(phydev->attached_dev);
  690. break;
  691. case PHY_RUNNING:
  692. /* Only register a CHANGE if we are
  693. * polling or ignoring interrupts
  694. */
  695. if (!phy_interrupt_is_valid(phydev))
  696. phydev->state = PHY_CHANGELINK;
  697. break;
  698. case PHY_CHANGELINK:
  699. err = phy_read_status(phydev);
  700. if (err)
  701. break;
  702. if (phydev->link) {
  703. phydev->state = PHY_RUNNING;
  704. netif_carrier_on(phydev->attached_dev);
  705. } else {
  706. phydev->state = PHY_NOLINK;
  707. netif_carrier_off(phydev->attached_dev);
  708. }
  709. phydev->adjust_link(phydev->attached_dev);
  710. if (phy_interrupt_is_valid(phydev))
  711. err = phy_config_interrupt(phydev,
  712. PHY_INTERRUPT_ENABLED);
  713. break;
  714. case PHY_HALTED:
  715. if (phydev->link) {
  716. phydev->link = 0;
  717. netif_carrier_off(phydev->attached_dev);
  718. phydev->adjust_link(phydev->attached_dev);
  719. }
  720. break;
  721. case PHY_RESUMING:
  722. err = phy_clear_interrupt(phydev);
  723. if (err)
  724. break;
  725. err = phy_config_interrupt(phydev,
  726. PHY_INTERRUPT_ENABLED);
  727. if (err)
  728. break;
  729. if (AUTONEG_ENABLE == phydev->autoneg) {
  730. err = phy_aneg_done(phydev);
  731. if (err < 0)
  732. break;
  733. /* err > 0 if AN is done.
  734. * Otherwise, it's 0, and we're
  735. * still waiting for AN */
  736. if (err > 0) {
  737. err = phy_read_status(phydev);
  738. if (err)
  739. break;
  740. if (phydev->link) {
  741. phydev->state = PHY_RUNNING;
  742. netif_carrier_on(phydev->attached_dev);
  743. } else
  744. phydev->state = PHY_NOLINK;
  745. phydev->adjust_link(phydev->attached_dev);
  746. } else {
  747. phydev->state = PHY_AN;
  748. phydev->link_timeout = PHY_AN_TIMEOUT;
  749. }
  750. } else {
  751. err = phy_read_status(phydev);
  752. if (err)
  753. break;
  754. if (phydev->link) {
  755. phydev->state = PHY_RUNNING;
  756. netif_carrier_on(phydev->attached_dev);
  757. } else
  758. phydev->state = PHY_NOLINK;
  759. phydev->adjust_link(phydev->attached_dev);
  760. }
  761. break;
  762. }
  763. mutex_unlock(&phydev->lock);
  764. if (needs_aneg)
  765. err = phy_start_aneg(phydev);
  766. if (err < 0)
  767. phy_error(phydev);
  768. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  769. PHY_STATE_TIME * HZ);
  770. }
  771. void phy_mac_interrupt(struct phy_device *phydev, int new_link)
  772. {
  773. cancel_work_sync(&phydev->phy_queue);
  774. phydev->link = new_link;
  775. schedule_work(&phydev->phy_queue);
  776. }
  777. EXPORT_SYMBOL(phy_mac_interrupt);
  778. static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
  779. int addr)
  780. {
  781. /* Write the desired MMD Devad */
  782. bus->write(bus, addr, MII_MMD_CTRL, devad);
  783. /* Write the desired MMD register address */
  784. bus->write(bus, addr, MII_MMD_DATA, prtad);
  785. /* Select the Function : DATA with no post increment */
  786. bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  787. }
  788. /**
  789. * phy_read_mmd_indirect - reads data from the MMD registers
  790. * @bus: the target MII bus
  791. * @prtad: MMD Address
  792. * @devad: MMD DEVAD
  793. * @addr: PHY address on the MII bus
  794. *
  795. * Description: it reads data from the MMD registers (clause 22 to access to
  796. * clause 45) of the specified phy address.
  797. * To read these register we have:
  798. * 1) Write reg 13 // DEVAD
  799. * 2) Write reg 14 // MMD Address
  800. * 3) Write reg 13 // MMD Data Command for MMD DEVAD
  801. * 3) Read reg 14 // Read MMD data
  802. */
  803. static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
  804. int addr)
  805. {
  806. u32 ret;
  807. mmd_phy_indirect(bus, prtad, devad, addr);
  808. /* Read the content of the MMD's selected register */
  809. ret = bus->read(bus, addr, MII_MMD_DATA);
  810. return ret;
  811. }
  812. /**
  813. * phy_write_mmd_indirect - writes data to the MMD registers
  814. * @bus: the target MII bus
  815. * @prtad: MMD Address
  816. * @devad: MMD DEVAD
  817. * @addr: PHY address on the MII bus
  818. * @data: data to write in the MMD register
  819. *
  820. * Description: Write data from the MMD registers of the specified
  821. * phy address.
  822. * To write these register we have:
  823. * 1) Write reg 13 // DEVAD
  824. * 2) Write reg 14 // MMD Address
  825. * 3) Write reg 13 // MMD Data Command for MMD DEVAD
  826. * 3) Write reg 14 // Write MMD data
  827. */
  828. static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
  829. int addr, u32 data)
  830. {
  831. mmd_phy_indirect(bus, prtad, devad, addr);
  832. /* Write the data into MMD's selected register */
  833. bus->write(bus, addr, MII_MMD_DATA, data);
  834. }
  835. /**
  836. * phy_init_eee - init and check the EEE feature
  837. * @phydev: target phy_device struct
  838. * @clk_stop_enable: PHY may stop the clock during LPI
  839. *
  840. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  841. * is supported by looking at the MMD registers 3.20 and 7.60/61
  842. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  843. * bit if required.
  844. */
  845. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  846. {
  847. int ret = -EPROTONOSUPPORT;
  848. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  849. * Also EEE feature is active when core is operating with MII, GMII
  850. * or RGMII.
  851. */
  852. if ((phydev->duplex == DUPLEX_FULL) &&
  853. ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
  854. (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
  855. (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
  856. int eee_lp, eee_cap, eee_adv;
  857. u32 lp, cap, adv;
  858. int idx, status;
  859. /* Read phy status to properly get the right settings */
  860. status = phy_read_status(phydev);
  861. if (status)
  862. return status;
  863. /* First check if the EEE ability is supported */
  864. eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
  865. MDIO_MMD_PCS, phydev->addr);
  866. if (eee_cap < 0)
  867. return eee_cap;
  868. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  869. if (!cap)
  870. goto eee_exit;
  871. /* Check which link settings negotiated and verify it in
  872. * the EEE advertising registers.
  873. */
  874. eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
  875. MDIO_MMD_AN, phydev->addr);
  876. if (eee_lp < 0)
  877. return eee_lp;
  878. eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
  879. MDIO_MMD_AN, phydev->addr);
  880. if (eee_adv < 0)
  881. return eee_adv;
  882. adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
  883. lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
  884. idx = phy_find_setting(phydev->speed, phydev->duplex);
  885. if (!(lp & adv & settings[idx].setting))
  886. goto eee_exit;
  887. if (clk_stop_enable) {
  888. /* Configure the PHY to stop receiving xMII
  889. * clock while it is signaling LPI.
  890. */
  891. int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
  892. MDIO_MMD_PCS,
  893. phydev->addr);
  894. if (val < 0)
  895. return val;
  896. val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
  897. phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
  898. MDIO_MMD_PCS, phydev->addr, val);
  899. }
  900. ret = 0; /* EEE supported */
  901. }
  902. eee_exit:
  903. return ret;
  904. }
  905. EXPORT_SYMBOL(phy_init_eee);
  906. /**
  907. * phy_get_eee_err - report the EEE wake error count
  908. * @phydev: target phy_device struct
  909. *
  910. * Description: it is to report the number of time where the PHY
  911. * failed to complete its normal wake sequence.
  912. */
  913. int phy_get_eee_err(struct phy_device *phydev)
  914. {
  915. return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
  916. MDIO_MMD_PCS, phydev->addr);
  917. }
  918. EXPORT_SYMBOL(phy_get_eee_err);
  919. /**
  920. * phy_ethtool_get_eee - get EEE supported and status
  921. * @phydev: target phy_device struct
  922. * @data: ethtool_eee data
  923. *
  924. * Description: it reportes the Supported/Advertisement/LP Advertisement
  925. * capabilities.
  926. */
  927. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  928. {
  929. int val;
  930. /* Get Supported EEE */
  931. val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
  932. MDIO_MMD_PCS, phydev->addr);
  933. if (val < 0)
  934. return val;
  935. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  936. /* Get advertisement EEE */
  937. val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
  938. MDIO_MMD_AN, phydev->addr);
  939. if (val < 0)
  940. return val;
  941. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  942. /* Get LP advertisement EEE */
  943. val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
  944. MDIO_MMD_AN, phydev->addr);
  945. if (val < 0)
  946. return val;
  947. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  948. return 0;
  949. }
  950. EXPORT_SYMBOL(phy_ethtool_get_eee);
  951. /**
  952. * phy_ethtool_set_eee - set EEE supported and status
  953. * @phydev: target phy_device struct
  954. * @data: ethtool_eee data
  955. *
  956. * Description: it is to program the Advertisement EEE register.
  957. */
  958. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  959. {
  960. int val;
  961. val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
  962. phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
  963. phydev->addr, val);
  964. return 0;
  965. }
  966. EXPORT_SYMBOL(phy_ethtool_set_eee);
  967. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  968. {
  969. if (phydev->drv->set_wol)
  970. return phydev->drv->set_wol(phydev, wol);
  971. return -EOPNOTSUPP;
  972. }
  973. EXPORT_SYMBOL(phy_ethtool_set_wol);
  974. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  975. {
  976. if (phydev->drv->get_wol)
  977. phydev->drv->get_wol(phydev, wol);
  978. }
  979. EXPORT_SYMBOL(phy_ethtool_get_wol);