phy.c 29 KB

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