phy_device.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. /* Framework for finding and configuring PHYs.
  2. * Also contains generic PHY driver
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/unistd.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/mii.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/phy.h>
  31. #include <linux/phy_led_triggers.h>
  32. #include <linux/mdio.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/of.h>
  36. #include <asm/irq.h>
  37. MODULE_DESCRIPTION("PHY library");
  38. MODULE_AUTHOR("Andy Fleming");
  39. MODULE_LICENSE("GPL");
  40. void phy_device_free(struct phy_device *phydev)
  41. {
  42. put_device(&phydev->mdio.dev);
  43. }
  44. EXPORT_SYMBOL(phy_device_free);
  45. static void phy_mdio_device_free(struct mdio_device *mdiodev)
  46. {
  47. struct phy_device *phydev;
  48. phydev = container_of(mdiodev, struct phy_device, mdio);
  49. phy_device_free(phydev);
  50. }
  51. static void phy_device_release(struct device *dev)
  52. {
  53. kfree(to_phy_device(dev));
  54. }
  55. static void phy_mdio_device_remove(struct mdio_device *mdiodev)
  56. {
  57. struct phy_device *phydev;
  58. phydev = container_of(mdiodev, struct phy_device, mdio);
  59. phy_device_remove(phydev);
  60. }
  61. enum genphy_driver {
  62. GENPHY_DRV_1G,
  63. GENPHY_DRV_10G,
  64. GENPHY_DRV_MAX
  65. };
  66. static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
  67. static LIST_HEAD(phy_fixup_list);
  68. static DEFINE_MUTEX(phy_fixup_lock);
  69. #ifdef CONFIG_PM
  70. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  71. {
  72. struct device_driver *drv = phydev->mdio.dev.driver;
  73. struct phy_driver *phydrv = to_phy_driver(drv);
  74. struct net_device *netdev = phydev->attached_dev;
  75. if (!drv || !phydrv->suspend)
  76. return false;
  77. /* PHY not attached? May suspend if the PHY has not already been
  78. * suspended as part of a prior call to phy_disconnect() ->
  79. * phy_detach() -> phy_suspend() because the parent netdev might be the
  80. * MDIO bus driver and clock gated at this point.
  81. */
  82. if (!netdev)
  83. return !phydev->suspended;
  84. /* Don't suspend PHY if the attached netdev parent may wakeup.
  85. * The parent may point to a PCI device, as in tg3 driver.
  86. */
  87. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  88. return false;
  89. /* Also don't suspend PHY if the netdev itself may wakeup. This
  90. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  91. * e.g. SoC devices.
  92. */
  93. if (device_may_wakeup(&netdev->dev))
  94. return false;
  95. return true;
  96. }
  97. static int mdio_bus_phy_suspend(struct device *dev)
  98. {
  99. struct phy_device *phydev = to_phy_device(dev);
  100. /* We must stop the state machine manually, otherwise it stops out of
  101. * control, possibly with the phydev->lock held. Upon resume, netdev
  102. * may call phy routines that try to grab the same lock, and that may
  103. * lead to a deadlock.
  104. */
  105. if (phydev->attached_dev && phydev->adjust_link)
  106. phy_stop_machine(phydev);
  107. if (!mdio_bus_phy_may_suspend(phydev))
  108. return 0;
  109. return phy_suspend(phydev);
  110. }
  111. static int mdio_bus_phy_resume(struct device *dev)
  112. {
  113. struct phy_device *phydev = to_phy_device(dev);
  114. int ret;
  115. if (!mdio_bus_phy_may_suspend(phydev))
  116. goto no_resume;
  117. ret = phy_resume(phydev);
  118. if (ret < 0)
  119. return ret;
  120. no_resume:
  121. if (phydev->attached_dev && phydev->adjust_link)
  122. phy_start_machine(phydev);
  123. return 0;
  124. }
  125. static int mdio_bus_phy_restore(struct device *dev)
  126. {
  127. struct phy_device *phydev = to_phy_device(dev);
  128. struct net_device *netdev = phydev->attached_dev;
  129. int ret;
  130. if (!netdev)
  131. return 0;
  132. ret = phy_init_hw(phydev);
  133. if (ret < 0)
  134. return ret;
  135. /* The PHY needs to renegotiate. */
  136. phydev->link = 0;
  137. phydev->state = PHY_UP;
  138. phy_start_machine(phydev);
  139. return 0;
  140. }
  141. static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
  142. .suspend = mdio_bus_phy_suspend,
  143. .resume = mdio_bus_phy_resume,
  144. .freeze = mdio_bus_phy_suspend,
  145. .thaw = mdio_bus_phy_resume,
  146. .restore = mdio_bus_phy_restore,
  147. };
  148. #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
  149. #else
  150. #define MDIO_BUS_PHY_PM_OPS NULL
  151. #endif /* CONFIG_PM */
  152. /**
  153. * phy_register_fixup - creates a new phy_fixup and adds it to the list
  154. * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
  155. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  156. * It can also be PHY_ANY_UID
  157. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  158. * comparison
  159. * @run: The actual code to be run when a matching PHY is found
  160. */
  161. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  162. int (*run)(struct phy_device *))
  163. {
  164. struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
  165. if (!fixup)
  166. return -ENOMEM;
  167. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  168. fixup->phy_uid = phy_uid;
  169. fixup->phy_uid_mask = phy_uid_mask;
  170. fixup->run = run;
  171. mutex_lock(&phy_fixup_lock);
  172. list_add_tail(&fixup->list, &phy_fixup_list);
  173. mutex_unlock(&phy_fixup_lock);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(phy_register_fixup);
  177. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  178. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  179. int (*run)(struct phy_device *))
  180. {
  181. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  182. }
  183. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  184. /* Registers a fixup to be run on the PHY with id string bus_id */
  185. int phy_register_fixup_for_id(const char *bus_id,
  186. int (*run)(struct phy_device *))
  187. {
  188. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  189. }
  190. EXPORT_SYMBOL(phy_register_fixup_for_id);
  191. /**
  192. * phy_unregister_fixup - remove a phy_fixup from the list
  193. * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
  194. * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
  195. * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
  196. */
  197. int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
  198. {
  199. struct list_head *pos, *n;
  200. struct phy_fixup *fixup;
  201. int ret;
  202. ret = -ENODEV;
  203. mutex_lock(&phy_fixup_lock);
  204. list_for_each_safe(pos, n, &phy_fixup_list) {
  205. fixup = list_entry(pos, struct phy_fixup, list);
  206. if ((!strcmp(fixup->bus_id, bus_id)) &&
  207. ((fixup->phy_uid & phy_uid_mask) ==
  208. (phy_uid & phy_uid_mask))) {
  209. list_del(&fixup->list);
  210. kfree(fixup);
  211. ret = 0;
  212. break;
  213. }
  214. }
  215. mutex_unlock(&phy_fixup_lock);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL(phy_unregister_fixup);
  219. /* Unregisters a fixup of any PHY with the UID in phy_uid */
  220. int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
  221. {
  222. return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
  223. }
  224. EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
  225. /* Unregisters a fixup of the PHY with id string bus_id */
  226. int phy_unregister_fixup_for_id(const char *bus_id)
  227. {
  228. return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
  229. }
  230. EXPORT_SYMBOL(phy_unregister_fixup_for_id);
  231. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  232. * Fixups can be set to match any in one or more fields.
  233. */
  234. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  235. {
  236. if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
  237. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  238. return 0;
  239. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  240. (phydev->phy_id & fixup->phy_uid_mask))
  241. if (fixup->phy_uid != PHY_ANY_UID)
  242. return 0;
  243. return 1;
  244. }
  245. /* Runs any matching fixups for this phydev */
  246. static int phy_scan_fixups(struct phy_device *phydev)
  247. {
  248. struct phy_fixup *fixup;
  249. mutex_lock(&phy_fixup_lock);
  250. list_for_each_entry(fixup, &phy_fixup_list, list) {
  251. if (phy_needs_fixup(phydev, fixup)) {
  252. int err = fixup->run(phydev);
  253. if (err < 0) {
  254. mutex_unlock(&phy_fixup_lock);
  255. return err;
  256. }
  257. phydev->has_fixups = true;
  258. }
  259. }
  260. mutex_unlock(&phy_fixup_lock);
  261. return 0;
  262. }
  263. static int phy_bus_match(struct device *dev, struct device_driver *drv)
  264. {
  265. struct phy_device *phydev = to_phy_device(dev);
  266. struct phy_driver *phydrv = to_phy_driver(drv);
  267. const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
  268. int i;
  269. if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
  270. return 0;
  271. if (phydrv->match_phy_device)
  272. return phydrv->match_phy_device(phydev);
  273. if (phydev->is_c45) {
  274. for (i = 1; i < num_ids; i++) {
  275. if (!(phydev->c45_ids.devices_in_package & (1 << i)))
  276. continue;
  277. if ((phydrv->phy_id & phydrv->phy_id_mask) ==
  278. (phydev->c45_ids.device_ids[i] &
  279. phydrv->phy_id_mask))
  280. return 1;
  281. }
  282. return 0;
  283. } else {
  284. return (phydrv->phy_id & phydrv->phy_id_mask) ==
  285. (phydev->phy_id & phydrv->phy_id_mask);
  286. }
  287. }
  288. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
  289. bool is_c45,
  290. struct phy_c45_device_ids *c45_ids)
  291. {
  292. struct phy_device *dev;
  293. struct mdio_device *mdiodev;
  294. /* We allocate the device, and initialize the default values */
  295. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  296. if (!dev)
  297. return ERR_PTR(-ENOMEM);
  298. mdiodev = &dev->mdio;
  299. mdiodev->dev.release = phy_device_release;
  300. mdiodev->dev.parent = &bus->dev;
  301. mdiodev->dev.bus = &mdio_bus_type;
  302. mdiodev->bus = bus;
  303. mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS;
  304. mdiodev->bus_match = phy_bus_match;
  305. mdiodev->addr = addr;
  306. mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
  307. mdiodev->device_free = phy_mdio_device_free;
  308. mdiodev->device_remove = phy_mdio_device_remove;
  309. dev->speed = 0;
  310. dev->duplex = -1;
  311. dev->pause = 0;
  312. dev->asym_pause = 0;
  313. dev->link = 1;
  314. dev->interface = PHY_INTERFACE_MODE_GMII;
  315. dev->autoneg = AUTONEG_ENABLE;
  316. dev->is_c45 = is_c45;
  317. dev->phy_id = phy_id;
  318. if (c45_ids)
  319. dev->c45_ids = *c45_ids;
  320. dev->irq = bus->irq[addr];
  321. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  322. dev->state = PHY_DOWN;
  323. mutex_init(&dev->lock);
  324. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  325. INIT_WORK(&dev->phy_queue, phy_change_work);
  326. /* Request the appropriate module unconditionally; don't
  327. * bother trying to do so only if it isn't already loaded,
  328. * because that gets complicated. A hotplug event would have
  329. * done an unconditional modprobe anyway.
  330. * We don't do normal hotplug because it won't work for MDIO
  331. * -- because it relies on the device staying around for long
  332. * enough for the driver to get loaded. With MDIO, the NIC
  333. * driver will get bored and give up as soon as it finds that
  334. * there's no driver _already_ loaded.
  335. */
  336. request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
  337. device_initialize(&mdiodev->dev);
  338. return dev;
  339. }
  340. EXPORT_SYMBOL(phy_device_create);
  341. /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
  342. * @bus: the target MII bus
  343. * @addr: PHY address on the MII bus
  344. * @dev_addr: MMD address in the PHY.
  345. * @devices_in_package: where to store the devices in package information.
  346. *
  347. * Description: reads devices in package registers of a MMD at @dev_addr
  348. * from PHY at @addr on @bus.
  349. *
  350. * Returns: 0 on success, -EIO on failure.
  351. */
  352. static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
  353. u32 *devices_in_package)
  354. {
  355. int phy_reg, reg_addr;
  356. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
  357. phy_reg = mdiobus_read(bus, addr, reg_addr);
  358. if (phy_reg < 0)
  359. return -EIO;
  360. *devices_in_package = (phy_reg & 0xffff) << 16;
  361. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
  362. phy_reg = mdiobus_read(bus, addr, reg_addr);
  363. if (phy_reg < 0)
  364. return -EIO;
  365. *devices_in_package |= (phy_reg & 0xffff);
  366. return 0;
  367. }
  368. /**
  369. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  370. * @bus: the target MII bus
  371. * @addr: PHY address on the MII bus
  372. * @phy_id: where to store the ID retrieved.
  373. * @c45_ids: where to store the c45 ID information.
  374. *
  375. * If the PHY devices-in-package appears to be valid, it and the
  376. * corresponding identifiers are stored in @c45_ids, zero is stored
  377. * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
  378. * zero on success.
  379. *
  380. */
  381. static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
  382. struct phy_c45_device_ids *c45_ids) {
  383. int phy_reg;
  384. int i, reg_addr;
  385. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  386. u32 *devs = &c45_ids->devices_in_package;
  387. /* Find first non-zero Devices In package. Device zero is reserved
  388. * for 802.3 c45 complied PHYs, so don't probe it at first.
  389. */
  390. for (i = 1; i < num_ids && *devs == 0; i++) {
  391. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
  392. if (phy_reg < 0)
  393. return -EIO;
  394. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  395. /* If mostly Fs, there is no device there,
  396. * then let's continue to probe more, as some
  397. * 10G PHYs have zero Devices In package,
  398. * e.g. Cortina CS4315/CS4340 PHY.
  399. */
  400. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
  401. if (phy_reg < 0)
  402. return -EIO;
  403. /* no device there, let's get out of here */
  404. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  405. *phy_id = 0xffffffff;
  406. return 0;
  407. } else {
  408. break;
  409. }
  410. }
  411. }
  412. /* Now probe Device Identifiers for each device present. */
  413. for (i = 1; i < num_ids; i++) {
  414. if (!(c45_ids->devices_in_package & (1 << i)))
  415. continue;
  416. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
  417. phy_reg = mdiobus_read(bus, addr, reg_addr);
  418. if (phy_reg < 0)
  419. return -EIO;
  420. c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
  421. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
  422. phy_reg = mdiobus_read(bus, addr, reg_addr);
  423. if (phy_reg < 0)
  424. return -EIO;
  425. c45_ids->device_ids[i] |= (phy_reg & 0xffff);
  426. }
  427. *phy_id = 0;
  428. return 0;
  429. }
  430. /**
  431. * get_phy_id - reads the specified addr for its ID.
  432. * @bus: the target MII bus
  433. * @addr: PHY address on the MII bus
  434. * @phy_id: where to store the ID retrieved.
  435. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  436. * @c45_ids: where to store the c45 ID information.
  437. *
  438. * Description: In the case of a 802.3-c22 PHY, reads the ID registers
  439. * of the PHY at @addr on the @bus, stores it in @phy_id and returns
  440. * zero on success.
  441. *
  442. * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
  443. * its return value is in turn returned.
  444. *
  445. */
  446. static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
  447. bool is_c45, struct phy_c45_device_ids *c45_ids)
  448. {
  449. int phy_reg;
  450. if (is_c45)
  451. return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
  452. /* Grab the bits from PHYIR1, and put them in the upper half */
  453. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  454. if (phy_reg < 0)
  455. return -EIO;
  456. *phy_id = (phy_reg & 0xffff) << 16;
  457. /* Grab the bits from PHYIR2, and put them in the lower half */
  458. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  459. if (phy_reg < 0)
  460. return -EIO;
  461. *phy_id |= (phy_reg & 0xffff);
  462. return 0;
  463. }
  464. /**
  465. * get_phy_device - reads the specified PHY device and returns its @phy_device
  466. * struct
  467. * @bus: the target MII bus
  468. * @addr: PHY address on the MII bus
  469. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  470. *
  471. * Description: Reads the ID registers of the PHY at @addr on the
  472. * @bus, then allocates and returns the phy_device to represent it.
  473. */
  474. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  475. {
  476. struct phy_c45_device_ids c45_ids = {0};
  477. u32 phy_id = 0;
  478. int r;
  479. r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
  480. if (r)
  481. return ERR_PTR(r);
  482. /* If the phy_id is mostly Fs, there is no device there */
  483. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  484. return ERR_PTR(-ENODEV);
  485. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  486. }
  487. EXPORT_SYMBOL(get_phy_device);
  488. static ssize_t
  489. phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  490. {
  491. struct phy_device *phydev = to_phy_device(dev);
  492. return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
  493. }
  494. static DEVICE_ATTR_RO(phy_id);
  495. static ssize_t
  496. phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
  497. {
  498. struct phy_device *phydev = to_phy_device(dev);
  499. const char *mode = NULL;
  500. if (phy_is_internal(phydev))
  501. mode = "internal";
  502. else
  503. mode = phy_modes(phydev->interface);
  504. return sprintf(buf, "%s\n", mode);
  505. }
  506. static DEVICE_ATTR_RO(phy_interface);
  507. static ssize_t
  508. phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
  509. char *buf)
  510. {
  511. struct phy_device *phydev = to_phy_device(dev);
  512. return sprintf(buf, "%d\n", phydev->has_fixups);
  513. }
  514. static DEVICE_ATTR_RO(phy_has_fixups);
  515. static struct attribute *phy_dev_attrs[] = {
  516. &dev_attr_phy_id.attr,
  517. &dev_attr_phy_interface.attr,
  518. &dev_attr_phy_has_fixups.attr,
  519. NULL,
  520. };
  521. ATTRIBUTE_GROUPS(phy_dev);
  522. /**
  523. * phy_device_register - Register the phy device on the MDIO bus
  524. * @phydev: phy_device structure to be added to the MDIO bus
  525. */
  526. int phy_device_register(struct phy_device *phydev)
  527. {
  528. int err;
  529. err = mdiobus_register_device(&phydev->mdio);
  530. if (err)
  531. return err;
  532. /* Run all of the fixups for this PHY */
  533. err = phy_scan_fixups(phydev);
  534. if (err) {
  535. pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
  536. goto out;
  537. }
  538. phydev->mdio.dev.groups = phy_dev_groups;
  539. err = device_add(&phydev->mdio.dev);
  540. if (err) {
  541. pr_err("PHY %d failed to add\n", phydev->mdio.addr);
  542. goto out;
  543. }
  544. return 0;
  545. out:
  546. mdiobus_unregister_device(&phydev->mdio);
  547. return err;
  548. }
  549. EXPORT_SYMBOL(phy_device_register);
  550. /**
  551. * phy_device_remove - Remove a previously registered phy device from the MDIO bus
  552. * @phydev: phy_device structure to remove
  553. *
  554. * This doesn't free the phy_device itself, it merely reverses the effects
  555. * of phy_device_register(). Use phy_device_free() to free the device
  556. * after calling this function.
  557. */
  558. void phy_device_remove(struct phy_device *phydev)
  559. {
  560. device_del(&phydev->mdio.dev);
  561. mdiobus_unregister_device(&phydev->mdio);
  562. }
  563. EXPORT_SYMBOL(phy_device_remove);
  564. /**
  565. * phy_find_first - finds the first PHY device on the bus
  566. * @bus: the target MII bus
  567. */
  568. struct phy_device *phy_find_first(struct mii_bus *bus)
  569. {
  570. struct phy_device *phydev;
  571. int addr;
  572. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  573. phydev = mdiobus_get_phy(bus, addr);
  574. if (phydev)
  575. return phydev;
  576. }
  577. return NULL;
  578. }
  579. EXPORT_SYMBOL(phy_find_first);
  580. /**
  581. * phy_prepare_link - prepares the PHY layer to monitor link status
  582. * @phydev: target phy_device struct
  583. * @handler: callback function for link status change notifications
  584. *
  585. * Description: Tells the PHY infrastructure to handle the
  586. * gory details on monitoring link status (whether through
  587. * polling or an interrupt), and to call back to the
  588. * connected device driver when the link status changes.
  589. * If you want to monitor your own link state, don't call
  590. * this function.
  591. */
  592. static void phy_prepare_link(struct phy_device *phydev,
  593. void (*handler)(struct net_device *))
  594. {
  595. phydev->adjust_link = handler;
  596. }
  597. /**
  598. * phy_connect_direct - connect an ethernet device to a specific phy_device
  599. * @dev: the network device to connect
  600. * @phydev: the pointer to the phy device
  601. * @handler: callback function for state change notifications
  602. * @interface: PHY device's interface
  603. */
  604. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  605. void (*handler)(struct net_device *),
  606. phy_interface_t interface)
  607. {
  608. int rc;
  609. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  610. if (rc)
  611. return rc;
  612. phy_prepare_link(phydev, handler);
  613. phy_start_machine(phydev);
  614. if (phydev->irq > 0)
  615. phy_start_interrupts(phydev);
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(phy_connect_direct);
  619. /**
  620. * phy_connect - connect an ethernet device to a PHY device
  621. * @dev: the network device to connect
  622. * @bus_id: the id string of the PHY device to connect
  623. * @handler: callback function for state change notifications
  624. * @interface: PHY device's interface
  625. *
  626. * Description: Convenience function for connecting ethernet
  627. * devices to PHY devices. The default behavior is for
  628. * the PHY infrastructure to handle everything, and only notify
  629. * the connected driver when the link status changes. If you
  630. * don't want, or can't use the provided functionality, you may
  631. * choose to call only the subset of functions which provide
  632. * the desired functionality.
  633. */
  634. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  635. void (*handler)(struct net_device *),
  636. phy_interface_t interface)
  637. {
  638. struct phy_device *phydev;
  639. struct device *d;
  640. int rc;
  641. /* Search the list of PHY devices on the mdio bus for the
  642. * PHY with the requested name
  643. */
  644. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  645. if (!d) {
  646. pr_err("PHY %s not found\n", bus_id);
  647. return ERR_PTR(-ENODEV);
  648. }
  649. phydev = to_phy_device(d);
  650. rc = phy_connect_direct(dev, phydev, handler, interface);
  651. put_device(d);
  652. if (rc)
  653. return ERR_PTR(rc);
  654. return phydev;
  655. }
  656. EXPORT_SYMBOL(phy_connect);
  657. /**
  658. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  659. * device
  660. * @phydev: target phy_device struct
  661. */
  662. void phy_disconnect(struct phy_device *phydev)
  663. {
  664. if (phydev->irq > 0)
  665. phy_stop_interrupts(phydev);
  666. phy_stop_machine(phydev);
  667. phydev->adjust_link = NULL;
  668. phy_detach(phydev);
  669. }
  670. EXPORT_SYMBOL(phy_disconnect);
  671. /**
  672. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  673. * @phydev: The PHY device to poll
  674. *
  675. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  676. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  677. * register must be polled until the BMCR_RESET bit clears.
  678. *
  679. * Furthermore, any attempts to write to PHY registers may have no effect
  680. * or even generate MDIO bus errors until this is complete.
  681. *
  682. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  683. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  684. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  685. * effort to support such broken PHYs, this function is separate from the
  686. * standard phy_init_hw() which will zero all the other bits in the BMCR
  687. * and reapply all driver-specific and board-specific fixups.
  688. */
  689. static int phy_poll_reset(struct phy_device *phydev)
  690. {
  691. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  692. unsigned int retries = 12;
  693. int ret;
  694. do {
  695. msleep(50);
  696. ret = phy_read(phydev, MII_BMCR);
  697. if (ret < 0)
  698. return ret;
  699. } while (ret & BMCR_RESET && --retries);
  700. if (ret & BMCR_RESET)
  701. return -ETIMEDOUT;
  702. /* Some chips (smsc911x) may still need up to another 1ms after the
  703. * BMCR_RESET bit is cleared before they are usable.
  704. */
  705. msleep(1);
  706. return 0;
  707. }
  708. int phy_init_hw(struct phy_device *phydev)
  709. {
  710. int ret = 0;
  711. if (!phydev->drv || !phydev->drv->config_init)
  712. return 0;
  713. if (phydev->drv->soft_reset)
  714. ret = phydev->drv->soft_reset(phydev);
  715. else
  716. ret = genphy_soft_reset(phydev);
  717. if (ret < 0)
  718. return ret;
  719. ret = phy_scan_fixups(phydev);
  720. if (ret < 0)
  721. return ret;
  722. return phydev->drv->config_init(phydev);
  723. }
  724. EXPORT_SYMBOL(phy_init_hw);
  725. void phy_attached_info(struct phy_device *phydev)
  726. {
  727. phy_attached_print(phydev, NULL);
  728. }
  729. EXPORT_SYMBOL(phy_attached_info);
  730. #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)"
  731. void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
  732. {
  733. if (!fmt) {
  734. dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
  735. phydev->drv->name, phydev_name(phydev),
  736. phydev->irq);
  737. } else {
  738. va_list ap;
  739. dev_info(&phydev->mdio.dev, ATTACHED_FMT,
  740. phydev->drv->name, phydev_name(phydev),
  741. phydev->irq);
  742. va_start(ap, fmt);
  743. vprintk(fmt, ap);
  744. va_end(ap);
  745. }
  746. }
  747. EXPORT_SYMBOL(phy_attached_print);
  748. /**
  749. * phy_attach_direct - attach a network device to a given PHY device pointer
  750. * @dev: network device to attach
  751. * @phydev: Pointer to phy_device to attach
  752. * @flags: PHY device's dev_flags
  753. * @interface: PHY device's interface
  754. *
  755. * Description: Called by drivers to attach to a particular PHY
  756. * device. The phy_device is found, and properly hooked up
  757. * to the phy_driver. If no driver is attached, then a
  758. * generic driver is used. The phy_device is given a ptr to
  759. * the attaching device, and given a callback for link status
  760. * change. The phy_device is returned to the attaching driver.
  761. * This function takes a reference on the phy device.
  762. */
  763. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  764. u32 flags, phy_interface_t interface)
  765. {
  766. struct module *ndev_owner = dev->dev.parent->driver->owner;
  767. struct mii_bus *bus = phydev->mdio.bus;
  768. struct device *d = &phydev->mdio.dev;
  769. int err;
  770. /* For Ethernet device drivers that register their own MDIO bus, we
  771. * will have bus->owner match ndev_mod, so we do not want to increment
  772. * our own module->refcnt here, otherwise we would not be able to
  773. * unload later on.
  774. */
  775. if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
  776. dev_err(&dev->dev, "failed to get the bus module\n");
  777. return -EIO;
  778. }
  779. get_device(d);
  780. /* Assume that if there is no driver, that it doesn't
  781. * exist, and we should use the genphy driver.
  782. */
  783. if (!d->driver) {
  784. if (phydev->is_c45)
  785. d->driver =
  786. &genphy_driver[GENPHY_DRV_10G].mdiodrv.driver;
  787. else
  788. d->driver =
  789. &genphy_driver[GENPHY_DRV_1G].mdiodrv.driver;
  790. err = d->driver->probe(d);
  791. if (err >= 0)
  792. err = device_bind_driver(d);
  793. if (err)
  794. goto error;
  795. }
  796. if (phydev->attached_dev) {
  797. dev_err(&dev->dev, "PHY already attached\n");
  798. err = -EBUSY;
  799. goto error;
  800. }
  801. phydev->attached_dev = dev;
  802. dev->phydev = phydev;
  803. phydev->dev_flags = flags;
  804. phydev->interface = interface;
  805. phydev->state = PHY_READY;
  806. /* Initial carrier state is off as the phy is about to be
  807. * (re)initialized.
  808. */
  809. netif_carrier_off(phydev->attached_dev);
  810. /* Do initial configuration here, now that
  811. * we have certain key parameters
  812. * (dev_flags and interface)
  813. */
  814. err = phy_init_hw(phydev);
  815. if (err)
  816. goto error;
  817. phy_resume(phydev);
  818. phy_led_triggers_register(phydev);
  819. return err;
  820. error:
  821. phy_detach(phydev);
  822. put_device(d);
  823. if (ndev_owner != bus->owner)
  824. module_put(bus->owner);
  825. return err;
  826. }
  827. EXPORT_SYMBOL(phy_attach_direct);
  828. /**
  829. * phy_attach - attach a network device to a particular PHY device
  830. * @dev: network device to attach
  831. * @bus_id: Bus ID of PHY device to attach
  832. * @interface: PHY device's interface
  833. *
  834. * Description: Same as phy_attach_direct() except that a PHY bus_id
  835. * string is passed instead of a pointer to a struct phy_device.
  836. */
  837. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  838. phy_interface_t interface)
  839. {
  840. struct bus_type *bus = &mdio_bus_type;
  841. struct phy_device *phydev;
  842. struct device *d;
  843. int rc;
  844. /* Search the list of PHY devices on the mdio bus for the
  845. * PHY with the requested name
  846. */
  847. d = bus_find_device_by_name(bus, NULL, bus_id);
  848. if (!d) {
  849. pr_err("PHY %s not found\n", bus_id);
  850. return ERR_PTR(-ENODEV);
  851. }
  852. phydev = to_phy_device(d);
  853. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  854. put_device(d);
  855. if (rc)
  856. return ERR_PTR(rc);
  857. return phydev;
  858. }
  859. EXPORT_SYMBOL(phy_attach);
  860. /**
  861. * phy_detach - detach a PHY device from its network device
  862. * @phydev: target phy_device struct
  863. *
  864. * This detaches the phy device from its network device and the phy
  865. * driver, and drops the reference count taken in phy_attach_direct().
  866. */
  867. void phy_detach(struct phy_device *phydev)
  868. {
  869. struct net_device *dev = phydev->attached_dev;
  870. struct module *ndev_owner = dev->dev.parent->driver->owner;
  871. struct mii_bus *bus;
  872. int i;
  873. phydev->attached_dev->phydev = NULL;
  874. phydev->attached_dev = NULL;
  875. phy_suspend(phydev);
  876. phy_led_triggers_unregister(phydev);
  877. /* If the device had no specific driver before (i.e. - it
  878. * was using the generic driver), we unbind the device
  879. * from the generic driver so that there's a chance a
  880. * real driver could be loaded
  881. */
  882. for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
  883. if (phydev->mdio.dev.driver ==
  884. &genphy_driver[i].mdiodrv.driver) {
  885. device_release_driver(&phydev->mdio.dev);
  886. break;
  887. }
  888. }
  889. /*
  890. * The phydev might go away on the put_device() below, so avoid
  891. * a use-after-free bug by reading the underlying bus first.
  892. */
  893. bus = phydev->mdio.bus;
  894. put_device(&phydev->mdio.dev);
  895. if (ndev_owner != bus->owner)
  896. module_put(bus->owner);
  897. }
  898. EXPORT_SYMBOL(phy_detach);
  899. int phy_suspend(struct phy_device *phydev)
  900. {
  901. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  902. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  903. int ret = 0;
  904. /* If the device has WOL enabled, we cannot suspend the PHY */
  905. phy_ethtool_get_wol(phydev, &wol);
  906. if (wol.wolopts)
  907. return -EBUSY;
  908. if (phydrv->suspend)
  909. ret = phydrv->suspend(phydev);
  910. if (ret)
  911. return ret;
  912. phydev->suspended = true;
  913. return ret;
  914. }
  915. EXPORT_SYMBOL(phy_suspend);
  916. int phy_resume(struct phy_device *phydev)
  917. {
  918. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  919. int ret = 0;
  920. if (phydrv->resume)
  921. ret = phydrv->resume(phydev);
  922. if (ret)
  923. return ret;
  924. phydev->suspended = false;
  925. return ret;
  926. }
  927. EXPORT_SYMBOL(phy_resume);
  928. /* Generic PHY support and helper functions */
  929. /**
  930. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  931. * @phydev: target phy_device struct
  932. *
  933. * Description: Writes MII_ADVERTISE with the appropriate values,
  934. * after sanitizing the values to make sure we only advertise
  935. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  936. * hasn't changed, and > 0 if it has changed.
  937. */
  938. static int genphy_config_advert(struct phy_device *phydev)
  939. {
  940. u32 advertise;
  941. int oldadv, adv, bmsr;
  942. int err, changed = 0;
  943. /* Only allow advertising what this PHY supports */
  944. phydev->advertising &= phydev->supported;
  945. advertise = phydev->advertising;
  946. /* Setup standard advertisement */
  947. adv = phy_read(phydev, MII_ADVERTISE);
  948. if (adv < 0)
  949. return adv;
  950. oldadv = adv;
  951. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  952. ADVERTISE_PAUSE_ASYM);
  953. adv |= ethtool_adv_to_mii_adv_t(advertise);
  954. if (adv != oldadv) {
  955. err = phy_write(phydev, MII_ADVERTISE, adv);
  956. if (err < 0)
  957. return err;
  958. changed = 1;
  959. }
  960. bmsr = phy_read(phydev, MII_BMSR);
  961. if (bmsr < 0)
  962. return bmsr;
  963. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  964. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  965. * logical 1.
  966. */
  967. if (!(bmsr & BMSR_ESTATEN))
  968. return changed;
  969. /* Configure gigabit if it's supported */
  970. adv = phy_read(phydev, MII_CTRL1000);
  971. if (adv < 0)
  972. return adv;
  973. oldadv = adv;
  974. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  975. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  976. SUPPORTED_1000baseT_Full)) {
  977. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  978. }
  979. if (adv != oldadv)
  980. changed = 1;
  981. err = phy_write(phydev, MII_CTRL1000, adv);
  982. if (err < 0)
  983. return err;
  984. return changed;
  985. }
  986. /**
  987. * genphy_config_eee_advert - disable unwanted eee mode advertisement
  988. * @phydev: target phy_device struct
  989. *
  990. * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
  991. * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
  992. * changed, and 1 if it has changed.
  993. */
  994. static int genphy_config_eee_advert(struct phy_device *phydev)
  995. {
  996. int broken = phydev->eee_broken_modes;
  997. int old_adv, adv;
  998. /* Nothing to disable */
  999. if (!broken)
  1000. return 0;
  1001. /* If the following call fails, we assume that EEE is not
  1002. * supported by the phy. If we read 0, EEE is not advertised
  1003. * In both case, we don't need to continue
  1004. */
  1005. adv = phy_read_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN);
  1006. if (adv <= 0)
  1007. return 0;
  1008. old_adv = adv;
  1009. adv &= ~broken;
  1010. /* Advertising remains unchanged with the broken mask */
  1011. if (old_adv == adv)
  1012. return 0;
  1013. phy_write_mmd_indirect(phydev, MDIO_AN_EEE_ADV, MDIO_MMD_AN, adv);
  1014. return 1;
  1015. }
  1016. /**
  1017. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  1018. * @phydev: target phy_device struct
  1019. *
  1020. * Description: Configures MII_BMCR to force speed/duplex
  1021. * to the values in phydev. Assumes that the values are valid.
  1022. * Please see phy_sanitize_settings().
  1023. */
  1024. int genphy_setup_forced(struct phy_device *phydev)
  1025. {
  1026. int ctl = phy_read(phydev, MII_BMCR);
  1027. ctl &= BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN;
  1028. phydev->pause = 0;
  1029. phydev->asym_pause = 0;
  1030. if (SPEED_1000 == phydev->speed)
  1031. ctl |= BMCR_SPEED1000;
  1032. else if (SPEED_100 == phydev->speed)
  1033. ctl |= BMCR_SPEED100;
  1034. if (DUPLEX_FULL == phydev->duplex)
  1035. ctl |= BMCR_FULLDPLX;
  1036. return phy_write(phydev, MII_BMCR, ctl);
  1037. }
  1038. EXPORT_SYMBOL(genphy_setup_forced);
  1039. /**
  1040. * genphy_restart_aneg - Enable and Restart Autonegotiation
  1041. * @phydev: target phy_device struct
  1042. */
  1043. int genphy_restart_aneg(struct phy_device *phydev)
  1044. {
  1045. int ctl = phy_read(phydev, MII_BMCR);
  1046. if (ctl < 0)
  1047. return ctl;
  1048. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  1049. /* Don't isolate the PHY if we're negotiating */
  1050. ctl &= ~BMCR_ISOLATE;
  1051. return phy_write(phydev, MII_BMCR, ctl);
  1052. }
  1053. EXPORT_SYMBOL(genphy_restart_aneg);
  1054. /**
  1055. * genphy_config_aneg - restart auto-negotiation or write BMCR
  1056. * @phydev: target phy_device struct
  1057. *
  1058. * Description: If auto-negotiation is enabled, we configure the
  1059. * advertising, and then restart auto-negotiation. If it is not
  1060. * enabled, then we write the BMCR.
  1061. */
  1062. int genphy_config_aneg(struct phy_device *phydev)
  1063. {
  1064. int err, changed;
  1065. changed = genphy_config_eee_advert(phydev);
  1066. if (AUTONEG_ENABLE != phydev->autoneg)
  1067. return genphy_setup_forced(phydev);
  1068. err = genphy_config_advert(phydev);
  1069. if (err < 0) /* error */
  1070. return err;
  1071. changed |= err;
  1072. if (changed == 0) {
  1073. /* Advertisement hasn't changed, but maybe aneg was never on to
  1074. * begin with? Or maybe phy was isolated?
  1075. */
  1076. int ctl = phy_read(phydev, MII_BMCR);
  1077. if (ctl < 0)
  1078. return ctl;
  1079. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  1080. changed = 1; /* do restart aneg */
  1081. }
  1082. /* Only restart aneg if we are advertising something different
  1083. * than we were before.
  1084. */
  1085. if (changed > 0)
  1086. return genphy_restart_aneg(phydev);
  1087. return 0;
  1088. }
  1089. EXPORT_SYMBOL(genphy_config_aneg);
  1090. /**
  1091. * genphy_aneg_done - return auto-negotiation status
  1092. * @phydev: target phy_device struct
  1093. *
  1094. * Description: Reads the status register and returns 0 either if
  1095. * auto-negotiation is incomplete, or if there was an error.
  1096. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  1097. */
  1098. int genphy_aneg_done(struct phy_device *phydev)
  1099. {
  1100. int retval = phy_read(phydev, MII_BMSR);
  1101. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  1102. }
  1103. EXPORT_SYMBOL(genphy_aneg_done);
  1104. static int gen10g_config_aneg(struct phy_device *phydev)
  1105. {
  1106. return 0;
  1107. }
  1108. /**
  1109. * genphy_update_link - update link status in @phydev
  1110. * @phydev: target phy_device struct
  1111. *
  1112. * Description: Update the value in phydev->link to reflect the
  1113. * current link value. In order to do this, we need to read
  1114. * the status register twice, keeping the second value.
  1115. */
  1116. int genphy_update_link(struct phy_device *phydev)
  1117. {
  1118. int status;
  1119. /* Do a fake read */
  1120. status = phy_read(phydev, MII_BMSR);
  1121. if (status < 0)
  1122. return status;
  1123. /* Read link and autonegotiation status */
  1124. status = phy_read(phydev, MII_BMSR);
  1125. if (status < 0)
  1126. return status;
  1127. if ((status & BMSR_LSTATUS) == 0)
  1128. phydev->link = 0;
  1129. else
  1130. phydev->link = 1;
  1131. return 0;
  1132. }
  1133. EXPORT_SYMBOL(genphy_update_link);
  1134. /**
  1135. * genphy_read_status - check the link status and update current link state
  1136. * @phydev: target phy_device struct
  1137. *
  1138. * Description: Check the link, then figure out the current state
  1139. * by comparing what we advertise with what the link partner
  1140. * advertises. Start by checking the gigabit possibilities,
  1141. * then move on to 10/100.
  1142. */
  1143. int genphy_read_status(struct phy_device *phydev)
  1144. {
  1145. int adv;
  1146. int err;
  1147. int lpa;
  1148. int lpagb = 0;
  1149. int common_adv;
  1150. int common_adv_gb = 0;
  1151. /* Update the link, but return if there was an error */
  1152. err = genphy_update_link(phydev);
  1153. if (err)
  1154. return err;
  1155. phydev->lp_advertising = 0;
  1156. if (AUTONEG_ENABLE == phydev->autoneg) {
  1157. if (phydev->supported & (SUPPORTED_1000baseT_Half
  1158. | SUPPORTED_1000baseT_Full)) {
  1159. lpagb = phy_read(phydev, MII_STAT1000);
  1160. if (lpagb < 0)
  1161. return lpagb;
  1162. adv = phy_read(phydev, MII_CTRL1000);
  1163. if (adv < 0)
  1164. return adv;
  1165. phydev->lp_advertising =
  1166. mii_stat1000_to_ethtool_lpa_t(lpagb);
  1167. common_adv_gb = lpagb & adv << 2;
  1168. }
  1169. lpa = phy_read(phydev, MII_LPA);
  1170. if (lpa < 0)
  1171. return lpa;
  1172. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  1173. adv = phy_read(phydev, MII_ADVERTISE);
  1174. if (adv < 0)
  1175. return adv;
  1176. common_adv = lpa & adv;
  1177. phydev->speed = SPEED_10;
  1178. phydev->duplex = DUPLEX_HALF;
  1179. phydev->pause = 0;
  1180. phydev->asym_pause = 0;
  1181. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  1182. phydev->speed = SPEED_1000;
  1183. if (common_adv_gb & LPA_1000FULL)
  1184. phydev->duplex = DUPLEX_FULL;
  1185. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  1186. phydev->speed = SPEED_100;
  1187. if (common_adv & LPA_100FULL)
  1188. phydev->duplex = DUPLEX_FULL;
  1189. } else
  1190. if (common_adv & LPA_10FULL)
  1191. phydev->duplex = DUPLEX_FULL;
  1192. if (phydev->duplex == DUPLEX_FULL) {
  1193. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  1194. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  1195. }
  1196. } else {
  1197. int bmcr = phy_read(phydev, MII_BMCR);
  1198. if (bmcr < 0)
  1199. return bmcr;
  1200. if (bmcr & BMCR_FULLDPLX)
  1201. phydev->duplex = DUPLEX_FULL;
  1202. else
  1203. phydev->duplex = DUPLEX_HALF;
  1204. if (bmcr & BMCR_SPEED1000)
  1205. phydev->speed = SPEED_1000;
  1206. else if (bmcr & BMCR_SPEED100)
  1207. phydev->speed = SPEED_100;
  1208. else
  1209. phydev->speed = SPEED_10;
  1210. phydev->pause = 0;
  1211. phydev->asym_pause = 0;
  1212. }
  1213. return 0;
  1214. }
  1215. EXPORT_SYMBOL(genphy_read_status);
  1216. static int gen10g_read_status(struct phy_device *phydev)
  1217. {
  1218. int devad, reg;
  1219. u32 mmd_mask = phydev->c45_ids.devices_in_package;
  1220. phydev->link = 1;
  1221. /* For now just lie and say it's 10G all the time */
  1222. phydev->speed = SPEED_10000;
  1223. phydev->duplex = DUPLEX_FULL;
  1224. for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
  1225. if (!(mmd_mask & 1))
  1226. continue;
  1227. /* Read twice because link state is latched and a
  1228. * read moves the current state into the register
  1229. */
  1230. phy_read_mmd(phydev, devad, MDIO_STAT1);
  1231. reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
  1232. if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
  1233. phydev->link = 0;
  1234. }
  1235. return 0;
  1236. }
  1237. /**
  1238. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  1239. * @phydev: target phy_device struct
  1240. *
  1241. * Description: Perform a software PHY reset using the standard
  1242. * BMCR_RESET bit and poll for the reset bit to be cleared.
  1243. *
  1244. * Returns: 0 on success, < 0 on failure
  1245. */
  1246. int genphy_soft_reset(struct phy_device *phydev)
  1247. {
  1248. int ret;
  1249. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  1250. if (ret < 0)
  1251. return ret;
  1252. return phy_poll_reset(phydev);
  1253. }
  1254. EXPORT_SYMBOL(genphy_soft_reset);
  1255. int genphy_config_init(struct phy_device *phydev)
  1256. {
  1257. int val;
  1258. u32 features;
  1259. features = (SUPPORTED_TP | SUPPORTED_MII
  1260. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  1261. SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1262. /* Do we support autonegotiation? */
  1263. val = phy_read(phydev, MII_BMSR);
  1264. if (val < 0)
  1265. return val;
  1266. if (val & BMSR_ANEGCAPABLE)
  1267. features |= SUPPORTED_Autoneg;
  1268. if (val & BMSR_100FULL)
  1269. features |= SUPPORTED_100baseT_Full;
  1270. if (val & BMSR_100HALF)
  1271. features |= SUPPORTED_100baseT_Half;
  1272. if (val & BMSR_10FULL)
  1273. features |= SUPPORTED_10baseT_Full;
  1274. if (val & BMSR_10HALF)
  1275. features |= SUPPORTED_10baseT_Half;
  1276. if (val & BMSR_ESTATEN) {
  1277. val = phy_read(phydev, MII_ESTATUS);
  1278. if (val < 0)
  1279. return val;
  1280. if (val & ESTATUS_1000_TFULL)
  1281. features |= SUPPORTED_1000baseT_Full;
  1282. if (val & ESTATUS_1000_THALF)
  1283. features |= SUPPORTED_1000baseT_Half;
  1284. }
  1285. phydev->supported &= features;
  1286. phydev->advertising &= features;
  1287. return 0;
  1288. }
  1289. static int gen10g_soft_reset(struct phy_device *phydev)
  1290. {
  1291. /* Do nothing for now */
  1292. return 0;
  1293. }
  1294. EXPORT_SYMBOL(genphy_config_init);
  1295. static int gen10g_config_init(struct phy_device *phydev)
  1296. {
  1297. /* Temporarily just say we support everything */
  1298. phydev->supported = SUPPORTED_10000baseT_Full;
  1299. phydev->advertising = SUPPORTED_10000baseT_Full;
  1300. return 0;
  1301. }
  1302. int genphy_suspend(struct phy_device *phydev)
  1303. {
  1304. int value;
  1305. mutex_lock(&phydev->lock);
  1306. value = phy_read(phydev, MII_BMCR);
  1307. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  1308. mutex_unlock(&phydev->lock);
  1309. return 0;
  1310. }
  1311. EXPORT_SYMBOL(genphy_suspend);
  1312. static int gen10g_suspend(struct phy_device *phydev)
  1313. {
  1314. return 0;
  1315. }
  1316. int genphy_resume(struct phy_device *phydev)
  1317. {
  1318. int value;
  1319. mutex_lock(&phydev->lock);
  1320. value = phy_read(phydev, MII_BMCR);
  1321. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  1322. mutex_unlock(&phydev->lock);
  1323. return 0;
  1324. }
  1325. EXPORT_SYMBOL(genphy_resume);
  1326. static int gen10g_resume(struct phy_device *phydev)
  1327. {
  1328. return 0;
  1329. }
  1330. static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  1331. {
  1332. /* The default values for phydev->supported are provided by the PHY
  1333. * driver "features" member, we want to reset to sane defaults first
  1334. * before supporting higher speeds.
  1335. */
  1336. phydev->supported &= PHY_DEFAULT_FEATURES;
  1337. switch (max_speed) {
  1338. default:
  1339. return -ENOTSUPP;
  1340. case SPEED_1000:
  1341. phydev->supported |= PHY_1000BT_FEATURES;
  1342. /* fall through */
  1343. case SPEED_100:
  1344. phydev->supported |= PHY_100BT_FEATURES;
  1345. /* fall through */
  1346. case SPEED_10:
  1347. phydev->supported |= PHY_10BT_FEATURES;
  1348. }
  1349. return 0;
  1350. }
  1351. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  1352. {
  1353. int err;
  1354. err = __set_phy_supported(phydev, max_speed);
  1355. if (err)
  1356. return err;
  1357. phydev->advertising = phydev->supported;
  1358. return 0;
  1359. }
  1360. EXPORT_SYMBOL(phy_set_max_speed);
  1361. static void of_set_phy_supported(struct phy_device *phydev)
  1362. {
  1363. struct device_node *node = phydev->mdio.dev.of_node;
  1364. u32 max_speed;
  1365. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1366. return;
  1367. if (!node)
  1368. return;
  1369. if (!of_property_read_u32(node, "max-speed", &max_speed))
  1370. __set_phy_supported(phydev, max_speed);
  1371. }
  1372. static void of_set_phy_eee_broken(struct phy_device *phydev)
  1373. {
  1374. struct device_node *node = phydev->mdio.dev.of_node;
  1375. u32 broken = 0;
  1376. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1377. return;
  1378. if (!node)
  1379. return;
  1380. if (of_property_read_bool(node, "eee-broken-100tx"))
  1381. broken |= MDIO_EEE_100TX;
  1382. if (of_property_read_bool(node, "eee-broken-1000t"))
  1383. broken |= MDIO_EEE_1000T;
  1384. if (of_property_read_bool(node, "eee-broken-10gt"))
  1385. broken |= MDIO_EEE_10GT;
  1386. if (of_property_read_bool(node, "eee-broken-1000kx"))
  1387. broken |= MDIO_EEE_1000KX;
  1388. if (of_property_read_bool(node, "eee-broken-10gkx4"))
  1389. broken |= MDIO_EEE_10GKX4;
  1390. if (of_property_read_bool(node, "eee-broken-10gkr"))
  1391. broken |= MDIO_EEE_10GKR;
  1392. phydev->eee_broken_modes = broken;
  1393. }
  1394. /**
  1395. * phy_probe - probe and init a PHY device
  1396. * @dev: device to probe and init
  1397. *
  1398. * Description: Take care of setting up the phy_device structure,
  1399. * set the state to READY (the driver's init function should
  1400. * set it to STARTING if needed).
  1401. */
  1402. static int phy_probe(struct device *dev)
  1403. {
  1404. struct phy_device *phydev = to_phy_device(dev);
  1405. struct device_driver *drv = phydev->mdio.dev.driver;
  1406. struct phy_driver *phydrv = to_phy_driver(drv);
  1407. int err = 0;
  1408. phydev->drv = phydrv;
  1409. /* Disable the interrupt if the PHY doesn't support it
  1410. * but the interrupt is still a valid one
  1411. */
  1412. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  1413. phy_interrupt_is_valid(phydev))
  1414. phydev->irq = PHY_POLL;
  1415. if (phydrv->flags & PHY_IS_INTERNAL)
  1416. phydev->is_internal = true;
  1417. mutex_lock(&phydev->lock);
  1418. /* Start out supporting everything. Eventually,
  1419. * a controller will attach, and may modify one
  1420. * or both of these values
  1421. */
  1422. phydev->supported = phydrv->features;
  1423. of_set_phy_supported(phydev);
  1424. phydev->advertising = phydev->supported;
  1425. /* Get the EEE modes we want to prohibit. We will ask
  1426. * the PHY stop advertising these mode later on
  1427. */
  1428. of_set_phy_eee_broken(phydev);
  1429. /* The Pause Frame bits indicate that the PHY can support passing
  1430. * pause frames. During autonegotiation, the PHYs will determine if
  1431. * they should allow pause frames to pass. The MAC driver should then
  1432. * use that result to determine whether to enable flow control via
  1433. * pause frames.
  1434. *
  1435. * Normally, PHY drivers should not set the Pause bits, and instead
  1436. * allow phylib to do that. However, there may be some situations
  1437. * (e.g. hardware erratum) where the driver wants to set only one
  1438. * of these bits.
  1439. */
  1440. if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
  1441. phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1442. phydev->supported |= phydrv->features &
  1443. (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1444. } else {
  1445. phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  1446. }
  1447. /* Set the state to READY by default */
  1448. phydev->state = PHY_READY;
  1449. if (phydev->drv->probe)
  1450. err = phydev->drv->probe(phydev);
  1451. mutex_unlock(&phydev->lock);
  1452. return err;
  1453. }
  1454. static int phy_remove(struct device *dev)
  1455. {
  1456. struct phy_device *phydev = to_phy_device(dev);
  1457. mutex_lock(&phydev->lock);
  1458. phydev->state = PHY_DOWN;
  1459. mutex_unlock(&phydev->lock);
  1460. if (phydev->drv->remove)
  1461. phydev->drv->remove(phydev);
  1462. phydev->drv = NULL;
  1463. return 0;
  1464. }
  1465. /**
  1466. * phy_driver_register - register a phy_driver with the PHY layer
  1467. * @new_driver: new phy_driver to register
  1468. * @owner: module owning this PHY
  1469. */
  1470. int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
  1471. {
  1472. int retval;
  1473. new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
  1474. new_driver->mdiodrv.driver.name = new_driver->name;
  1475. new_driver->mdiodrv.driver.bus = &mdio_bus_type;
  1476. new_driver->mdiodrv.driver.probe = phy_probe;
  1477. new_driver->mdiodrv.driver.remove = phy_remove;
  1478. new_driver->mdiodrv.driver.owner = owner;
  1479. retval = driver_register(&new_driver->mdiodrv.driver);
  1480. if (retval) {
  1481. pr_err("%s: Error %d in registering driver\n",
  1482. new_driver->name, retval);
  1483. return retval;
  1484. }
  1485. pr_debug("%s: Registered new driver\n", new_driver->name);
  1486. return 0;
  1487. }
  1488. EXPORT_SYMBOL(phy_driver_register);
  1489. int phy_drivers_register(struct phy_driver *new_driver, int n,
  1490. struct module *owner)
  1491. {
  1492. int i, ret = 0;
  1493. for (i = 0; i < n; i++) {
  1494. ret = phy_driver_register(new_driver + i, owner);
  1495. if (ret) {
  1496. while (i-- > 0)
  1497. phy_driver_unregister(new_driver + i);
  1498. break;
  1499. }
  1500. }
  1501. return ret;
  1502. }
  1503. EXPORT_SYMBOL(phy_drivers_register);
  1504. void phy_driver_unregister(struct phy_driver *drv)
  1505. {
  1506. driver_unregister(&drv->mdiodrv.driver);
  1507. }
  1508. EXPORT_SYMBOL(phy_driver_unregister);
  1509. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1510. {
  1511. int i;
  1512. for (i = 0; i < n; i++)
  1513. phy_driver_unregister(drv + i);
  1514. }
  1515. EXPORT_SYMBOL(phy_drivers_unregister);
  1516. static struct phy_driver genphy_driver[] = {
  1517. {
  1518. .phy_id = 0xffffffff,
  1519. .phy_id_mask = 0xffffffff,
  1520. .name = "Generic PHY",
  1521. .soft_reset = genphy_soft_reset,
  1522. .config_init = genphy_config_init,
  1523. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  1524. SUPPORTED_AUI | SUPPORTED_FIBRE |
  1525. SUPPORTED_BNC,
  1526. .config_aneg = genphy_config_aneg,
  1527. .aneg_done = genphy_aneg_done,
  1528. .read_status = genphy_read_status,
  1529. .suspend = genphy_suspend,
  1530. .resume = genphy_resume,
  1531. }, {
  1532. .phy_id = 0xffffffff,
  1533. .phy_id_mask = 0xffffffff,
  1534. .name = "Generic 10G PHY",
  1535. .soft_reset = gen10g_soft_reset,
  1536. .config_init = gen10g_config_init,
  1537. .features = 0,
  1538. .config_aneg = gen10g_config_aneg,
  1539. .read_status = gen10g_read_status,
  1540. .suspend = gen10g_suspend,
  1541. .resume = gen10g_resume,
  1542. } };
  1543. static int __init phy_init(void)
  1544. {
  1545. int rc;
  1546. rc = mdio_bus_init();
  1547. if (rc)
  1548. return rc;
  1549. rc = phy_drivers_register(genphy_driver,
  1550. ARRAY_SIZE(genphy_driver), THIS_MODULE);
  1551. if (rc)
  1552. mdio_bus_exit();
  1553. return rc;
  1554. }
  1555. static void __exit phy_exit(void)
  1556. {
  1557. phy_drivers_unregister(genphy_driver,
  1558. ARRAY_SIZE(genphy_driver));
  1559. mdio_bus_exit();
  1560. }
  1561. subsys_initcall(phy_init);
  1562. module_exit(phy_exit);