phy.c 30 KB

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