phy_device.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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. bool using_genphy = false;
  770. int err;
  771. /* For Ethernet device drivers that register their own MDIO bus, we
  772. * will have bus->owner match ndev_mod, so we do not want to increment
  773. * our own module->refcnt here, otherwise we would not be able to
  774. * unload later on.
  775. */
  776. if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
  777. dev_err(&dev->dev, "failed to get the bus module\n");
  778. return -EIO;
  779. }
  780. get_device(d);
  781. /* Assume that if there is no driver, that it doesn't
  782. * exist, and we should use the genphy driver.
  783. */
  784. if (!d->driver) {
  785. if (phydev->is_c45)
  786. d->driver =
  787. &genphy_driver[GENPHY_DRV_10G].mdiodrv.driver;
  788. else
  789. d->driver =
  790. &genphy_driver[GENPHY_DRV_1G].mdiodrv.driver;
  791. using_genphy = true;
  792. }
  793. if (!try_module_get(d->driver->owner)) {
  794. dev_err(&dev->dev, "failed to get the device driver module\n");
  795. err = -EIO;
  796. goto error_put_device;
  797. }
  798. if (using_genphy) {
  799. err = d->driver->probe(d);
  800. if (err >= 0)
  801. err = device_bind_driver(d);
  802. if (err)
  803. goto error_module_put;
  804. }
  805. if (phydev->attached_dev) {
  806. dev_err(&dev->dev, "PHY already attached\n");
  807. err = -EBUSY;
  808. goto error;
  809. }
  810. phydev->attached_dev = dev;
  811. dev->phydev = phydev;
  812. phydev->dev_flags = flags;
  813. phydev->interface = interface;
  814. phydev->state = PHY_READY;
  815. /* Initial carrier state is off as the phy is about to be
  816. * (re)initialized.
  817. */
  818. netif_carrier_off(phydev->attached_dev);
  819. /* Do initial configuration here, now that
  820. * we have certain key parameters
  821. * (dev_flags and interface)
  822. */
  823. err = phy_init_hw(phydev);
  824. if (err)
  825. goto error;
  826. phy_resume(phydev);
  827. phy_led_triggers_register(phydev);
  828. return err;
  829. error:
  830. /* phy_detach() does all of the cleanup below */
  831. phy_detach(phydev);
  832. return err;
  833. error_module_put:
  834. module_put(d->driver->owner);
  835. error_put_device:
  836. put_device(d);
  837. if (ndev_owner != bus->owner)
  838. module_put(bus->owner);
  839. return err;
  840. }
  841. EXPORT_SYMBOL(phy_attach_direct);
  842. /**
  843. * phy_attach - attach a network device to a particular PHY device
  844. * @dev: network device to attach
  845. * @bus_id: Bus ID of PHY device to attach
  846. * @interface: PHY device's interface
  847. *
  848. * Description: Same as phy_attach_direct() except that a PHY bus_id
  849. * string is passed instead of a pointer to a struct phy_device.
  850. */
  851. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  852. phy_interface_t interface)
  853. {
  854. struct bus_type *bus = &mdio_bus_type;
  855. struct phy_device *phydev;
  856. struct device *d;
  857. int rc;
  858. /* Search the list of PHY devices on the mdio bus for the
  859. * PHY with the requested name
  860. */
  861. d = bus_find_device_by_name(bus, NULL, bus_id);
  862. if (!d) {
  863. pr_err("PHY %s not found\n", bus_id);
  864. return ERR_PTR(-ENODEV);
  865. }
  866. phydev = to_phy_device(d);
  867. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  868. put_device(d);
  869. if (rc)
  870. return ERR_PTR(rc);
  871. return phydev;
  872. }
  873. EXPORT_SYMBOL(phy_attach);
  874. /**
  875. * phy_detach - detach a PHY device from its network device
  876. * @phydev: target phy_device struct
  877. *
  878. * This detaches the phy device from its network device and the phy
  879. * driver, and drops the reference count taken in phy_attach_direct().
  880. */
  881. void phy_detach(struct phy_device *phydev)
  882. {
  883. struct net_device *dev = phydev->attached_dev;
  884. struct module *ndev_owner = dev->dev.parent->driver->owner;
  885. struct mii_bus *bus;
  886. int i;
  887. phydev->attached_dev->phydev = NULL;
  888. phydev->attached_dev = NULL;
  889. phy_suspend(phydev);
  890. phy_led_triggers_unregister(phydev);
  891. module_put(phydev->mdio.dev.driver->owner);
  892. /* If the device had no specific driver before (i.e. - it
  893. * was using the generic driver), we unbind the device
  894. * from the generic driver so that there's a chance a
  895. * real driver could be loaded
  896. */
  897. for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
  898. if (phydev->mdio.dev.driver ==
  899. &genphy_driver[i].mdiodrv.driver) {
  900. device_release_driver(&phydev->mdio.dev);
  901. break;
  902. }
  903. }
  904. /*
  905. * The phydev might go away on the put_device() below, so avoid
  906. * a use-after-free bug by reading the underlying bus first.
  907. */
  908. bus = phydev->mdio.bus;
  909. put_device(&phydev->mdio.dev);
  910. if (ndev_owner != bus->owner)
  911. module_put(bus->owner);
  912. }
  913. EXPORT_SYMBOL(phy_detach);
  914. int phy_suspend(struct phy_device *phydev)
  915. {
  916. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  917. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  918. int ret = 0;
  919. /* If the device has WOL enabled, we cannot suspend the PHY */
  920. phy_ethtool_get_wol(phydev, &wol);
  921. if (wol.wolopts)
  922. return -EBUSY;
  923. if (phydev->drv && phydrv->suspend)
  924. ret = phydrv->suspend(phydev);
  925. if (ret)
  926. return ret;
  927. phydev->suspended = true;
  928. return ret;
  929. }
  930. EXPORT_SYMBOL(phy_suspend);
  931. int phy_resume(struct phy_device *phydev)
  932. {
  933. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  934. int ret = 0;
  935. if (phydev->drv && phydrv->resume)
  936. ret = phydrv->resume(phydev);
  937. if (ret)
  938. return ret;
  939. phydev->suspended = false;
  940. return ret;
  941. }
  942. EXPORT_SYMBOL(phy_resume);
  943. /* Generic PHY support and helper functions */
  944. /**
  945. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  946. * @phydev: target phy_device struct
  947. *
  948. * Description: Writes MII_ADVERTISE with the appropriate values,
  949. * after sanitizing the values to make sure we only advertise
  950. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  951. * hasn't changed, and > 0 if it has changed.
  952. */
  953. static int genphy_config_advert(struct phy_device *phydev)
  954. {
  955. u32 advertise;
  956. int oldadv, adv, bmsr;
  957. int err, changed = 0;
  958. /* Only allow advertising what this PHY supports */
  959. phydev->advertising &= phydev->supported;
  960. advertise = phydev->advertising;
  961. /* Setup standard advertisement */
  962. adv = phy_read(phydev, MII_ADVERTISE);
  963. if (adv < 0)
  964. return adv;
  965. oldadv = adv;
  966. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  967. ADVERTISE_PAUSE_ASYM);
  968. adv |= ethtool_adv_to_mii_adv_t(advertise);
  969. if (adv != oldadv) {
  970. err = phy_write(phydev, MII_ADVERTISE, adv);
  971. if (err < 0)
  972. return err;
  973. changed = 1;
  974. }
  975. bmsr = phy_read(phydev, MII_BMSR);
  976. if (bmsr < 0)
  977. return bmsr;
  978. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  979. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  980. * logical 1.
  981. */
  982. if (!(bmsr & BMSR_ESTATEN))
  983. return changed;
  984. /* Configure gigabit if it's supported */
  985. adv = phy_read(phydev, MII_CTRL1000);
  986. if (adv < 0)
  987. return adv;
  988. oldadv = adv;
  989. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  990. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  991. SUPPORTED_1000baseT_Full)) {
  992. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  993. }
  994. if (adv != oldadv)
  995. changed = 1;
  996. err = phy_write(phydev, MII_CTRL1000, adv);
  997. if (err < 0)
  998. return err;
  999. return changed;
  1000. }
  1001. /**
  1002. * genphy_config_eee_advert - disable unwanted eee mode advertisement
  1003. * @phydev: target phy_device struct
  1004. *
  1005. * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
  1006. * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
  1007. * changed, and 1 if it has changed.
  1008. */
  1009. static int genphy_config_eee_advert(struct phy_device *phydev)
  1010. {
  1011. int broken = phydev->eee_broken_modes;
  1012. int old_adv, adv;
  1013. /* Nothing to disable */
  1014. if (!broken)
  1015. return 0;
  1016. /* If the following call fails, we assume that EEE is not
  1017. * supported by the phy. If we read 0, EEE is not advertised
  1018. * In both case, we don't need to continue
  1019. */
  1020. adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1021. if (adv <= 0)
  1022. return 0;
  1023. old_adv = adv;
  1024. adv &= ~broken;
  1025. /* Advertising remains unchanged with the broken mask */
  1026. if (old_adv == adv)
  1027. return 0;
  1028. phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1029. return 1;
  1030. }
  1031. /**
  1032. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  1033. * @phydev: target phy_device struct
  1034. *
  1035. * Description: Configures MII_BMCR to force speed/duplex
  1036. * to the values in phydev. Assumes that the values are valid.
  1037. * Please see phy_sanitize_settings().
  1038. */
  1039. int genphy_setup_forced(struct phy_device *phydev)
  1040. {
  1041. int ctl = phy_read(phydev, MII_BMCR);
  1042. ctl &= BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN;
  1043. phydev->pause = 0;
  1044. phydev->asym_pause = 0;
  1045. if (SPEED_1000 == phydev->speed)
  1046. ctl |= BMCR_SPEED1000;
  1047. else if (SPEED_100 == phydev->speed)
  1048. ctl |= BMCR_SPEED100;
  1049. if (DUPLEX_FULL == phydev->duplex)
  1050. ctl |= BMCR_FULLDPLX;
  1051. return phy_write(phydev, MII_BMCR, ctl);
  1052. }
  1053. EXPORT_SYMBOL(genphy_setup_forced);
  1054. /**
  1055. * genphy_restart_aneg - Enable and Restart Autonegotiation
  1056. * @phydev: target phy_device struct
  1057. */
  1058. int genphy_restart_aneg(struct phy_device *phydev)
  1059. {
  1060. int ctl = phy_read(phydev, MII_BMCR);
  1061. if (ctl < 0)
  1062. return ctl;
  1063. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  1064. /* Don't isolate the PHY if we're negotiating */
  1065. ctl &= ~BMCR_ISOLATE;
  1066. return phy_write(phydev, MII_BMCR, ctl);
  1067. }
  1068. EXPORT_SYMBOL(genphy_restart_aneg);
  1069. /**
  1070. * genphy_config_aneg - restart auto-negotiation or write BMCR
  1071. * @phydev: target phy_device struct
  1072. *
  1073. * Description: If auto-negotiation is enabled, we configure the
  1074. * advertising, and then restart auto-negotiation. If it is not
  1075. * enabled, then we write the BMCR.
  1076. */
  1077. int genphy_config_aneg(struct phy_device *phydev)
  1078. {
  1079. int err, changed;
  1080. changed = genphy_config_eee_advert(phydev);
  1081. if (AUTONEG_ENABLE != phydev->autoneg)
  1082. return genphy_setup_forced(phydev);
  1083. err = genphy_config_advert(phydev);
  1084. if (err < 0) /* error */
  1085. return err;
  1086. changed |= err;
  1087. if (changed == 0) {
  1088. /* Advertisement hasn't changed, but maybe aneg was never on to
  1089. * begin with? Or maybe phy was isolated?
  1090. */
  1091. int ctl = phy_read(phydev, MII_BMCR);
  1092. if (ctl < 0)
  1093. return ctl;
  1094. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  1095. changed = 1; /* do restart aneg */
  1096. }
  1097. /* Only restart aneg if we are advertising something different
  1098. * than we were before.
  1099. */
  1100. if (changed > 0)
  1101. return genphy_restart_aneg(phydev);
  1102. return 0;
  1103. }
  1104. EXPORT_SYMBOL(genphy_config_aneg);
  1105. /**
  1106. * genphy_aneg_done - return auto-negotiation status
  1107. * @phydev: target phy_device struct
  1108. *
  1109. * Description: Reads the status register and returns 0 either if
  1110. * auto-negotiation is incomplete, or if there was an error.
  1111. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  1112. */
  1113. int genphy_aneg_done(struct phy_device *phydev)
  1114. {
  1115. int retval = phy_read(phydev, MII_BMSR);
  1116. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  1117. }
  1118. EXPORT_SYMBOL(genphy_aneg_done);
  1119. static int gen10g_config_aneg(struct phy_device *phydev)
  1120. {
  1121. return 0;
  1122. }
  1123. /**
  1124. * genphy_update_link - update link status in @phydev
  1125. * @phydev: target phy_device struct
  1126. *
  1127. * Description: Update the value in phydev->link to reflect the
  1128. * current link value. In order to do this, we need to read
  1129. * the status register twice, keeping the second value.
  1130. */
  1131. int genphy_update_link(struct phy_device *phydev)
  1132. {
  1133. int status;
  1134. /* Do a fake read */
  1135. status = phy_read(phydev, MII_BMSR);
  1136. if (status < 0)
  1137. return status;
  1138. /* Read link and autonegotiation status */
  1139. status = phy_read(phydev, MII_BMSR);
  1140. if (status < 0)
  1141. return status;
  1142. if ((status & BMSR_LSTATUS) == 0)
  1143. phydev->link = 0;
  1144. else
  1145. phydev->link = 1;
  1146. return 0;
  1147. }
  1148. EXPORT_SYMBOL(genphy_update_link);
  1149. /**
  1150. * genphy_read_status - check the link status and update current link state
  1151. * @phydev: target phy_device struct
  1152. *
  1153. * Description: Check the link, then figure out the current state
  1154. * by comparing what we advertise with what the link partner
  1155. * advertises. Start by checking the gigabit possibilities,
  1156. * then move on to 10/100.
  1157. */
  1158. int genphy_read_status(struct phy_device *phydev)
  1159. {
  1160. int adv;
  1161. int err;
  1162. int lpa;
  1163. int lpagb = 0;
  1164. int common_adv;
  1165. int common_adv_gb = 0;
  1166. /* Update the link, but return if there was an error */
  1167. err = genphy_update_link(phydev);
  1168. if (err)
  1169. return err;
  1170. phydev->lp_advertising = 0;
  1171. if (AUTONEG_ENABLE == phydev->autoneg) {
  1172. if (phydev->supported & (SUPPORTED_1000baseT_Half
  1173. | SUPPORTED_1000baseT_Full)) {
  1174. lpagb = phy_read(phydev, MII_STAT1000);
  1175. if (lpagb < 0)
  1176. return lpagb;
  1177. adv = phy_read(phydev, MII_CTRL1000);
  1178. if (adv < 0)
  1179. return adv;
  1180. phydev->lp_advertising =
  1181. mii_stat1000_to_ethtool_lpa_t(lpagb);
  1182. common_adv_gb = lpagb & adv << 2;
  1183. }
  1184. lpa = phy_read(phydev, MII_LPA);
  1185. if (lpa < 0)
  1186. return lpa;
  1187. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  1188. adv = phy_read(phydev, MII_ADVERTISE);
  1189. if (adv < 0)
  1190. return adv;
  1191. common_adv = lpa & adv;
  1192. phydev->speed = SPEED_10;
  1193. phydev->duplex = DUPLEX_HALF;
  1194. phydev->pause = 0;
  1195. phydev->asym_pause = 0;
  1196. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  1197. phydev->speed = SPEED_1000;
  1198. if (common_adv_gb & LPA_1000FULL)
  1199. phydev->duplex = DUPLEX_FULL;
  1200. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  1201. phydev->speed = SPEED_100;
  1202. if (common_adv & LPA_100FULL)
  1203. phydev->duplex = DUPLEX_FULL;
  1204. } else
  1205. if (common_adv & LPA_10FULL)
  1206. phydev->duplex = DUPLEX_FULL;
  1207. if (phydev->duplex == DUPLEX_FULL) {
  1208. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  1209. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  1210. }
  1211. } else {
  1212. int bmcr = phy_read(phydev, MII_BMCR);
  1213. if (bmcr < 0)
  1214. return bmcr;
  1215. if (bmcr & BMCR_FULLDPLX)
  1216. phydev->duplex = DUPLEX_FULL;
  1217. else
  1218. phydev->duplex = DUPLEX_HALF;
  1219. if (bmcr & BMCR_SPEED1000)
  1220. phydev->speed = SPEED_1000;
  1221. else if (bmcr & BMCR_SPEED100)
  1222. phydev->speed = SPEED_100;
  1223. else
  1224. phydev->speed = SPEED_10;
  1225. phydev->pause = 0;
  1226. phydev->asym_pause = 0;
  1227. }
  1228. return 0;
  1229. }
  1230. EXPORT_SYMBOL(genphy_read_status);
  1231. static int gen10g_read_status(struct phy_device *phydev)
  1232. {
  1233. int devad, reg;
  1234. u32 mmd_mask = phydev->c45_ids.devices_in_package;
  1235. phydev->link = 1;
  1236. /* For now just lie and say it's 10G all the time */
  1237. phydev->speed = SPEED_10000;
  1238. phydev->duplex = DUPLEX_FULL;
  1239. for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
  1240. if (!(mmd_mask & 1))
  1241. continue;
  1242. /* Read twice because link state is latched and a
  1243. * read moves the current state into the register
  1244. */
  1245. phy_read_mmd(phydev, devad, MDIO_STAT1);
  1246. reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
  1247. if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
  1248. phydev->link = 0;
  1249. }
  1250. return 0;
  1251. }
  1252. /**
  1253. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  1254. * @phydev: target phy_device struct
  1255. *
  1256. * Description: Perform a software PHY reset using the standard
  1257. * BMCR_RESET bit and poll for the reset bit to be cleared.
  1258. *
  1259. * Returns: 0 on success, < 0 on failure
  1260. */
  1261. int genphy_soft_reset(struct phy_device *phydev)
  1262. {
  1263. int ret;
  1264. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  1265. if (ret < 0)
  1266. return ret;
  1267. return phy_poll_reset(phydev);
  1268. }
  1269. EXPORT_SYMBOL(genphy_soft_reset);
  1270. int genphy_config_init(struct phy_device *phydev)
  1271. {
  1272. int val;
  1273. u32 features;
  1274. features = (SUPPORTED_TP | SUPPORTED_MII
  1275. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  1276. SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1277. /* Do we support autonegotiation? */
  1278. val = phy_read(phydev, MII_BMSR);
  1279. if (val < 0)
  1280. return val;
  1281. if (val & BMSR_ANEGCAPABLE)
  1282. features |= SUPPORTED_Autoneg;
  1283. if (val & BMSR_100FULL)
  1284. features |= SUPPORTED_100baseT_Full;
  1285. if (val & BMSR_100HALF)
  1286. features |= SUPPORTED_100baseT_Half;
  1287. if (val & BMSR_10FULL)
  1288. features |= SUPPORTED_10baseT_Full;
  1289. if (val & BMSR_10HALF)
  1290. features |= SUPPORTED_10baseT_Half;
  1291. if (val & BMSR_ESTATEN) {
  1292. val = phy_read(phydev, MII_ESTATUS);
  1293. if (val < 0)
  1294. return val;
  1295. if (val & ESTATUS_1000_TFULL)
  1296. features |= SUPPORTED_1000baseT_Full;
  1297. if (val & ESTATUS_1000_THALF)
  1298. features |= SUPPORTED_1000baseT_Half;
  1299. }
  1300. phydev->supported &= features;
  1301. phydev->advertising &= features;
  1302. return 0;
  1303. }
  1304. static int gen10g_soft_reset(struct phy_device *phydev)
  1305. {
  1306. /* Do nothing for now */
  1307. return 0;
  1308. }
  1309. EXPORT_SYMBOL(genphy_config_init);
  1310. static int gen10g_config_init(struct phy_device *phydev)
  1311. {
  1312. /* Temporarily just say we support everything */
  1313. phydev->supported = SUPPORTED_10000baseT_Full;
  1314. phydev->advertising = SUPPORTED_10000baseT_Full;
  1315. return 0;
  1316. }
  1317. int genphy_suspend(struct phy_device *phydev)
  1318. {
  1319. int value;
  1320. mutex_lock(&phydev->lock);
  1321. value = phy_read(phydev, MII_BMCR);
  1322. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  1323. mutex_unlock(&phydev->lock);
  1324. return 0;
  1325. }
  1326. EXPORT_SYMBOL(genphy_suspend);
  1327. static int gen10g_suspend(struct phy_device *phydev)
  1328. {
  1329. return 0;
  1330. }
  1331. int genphy_resume(struct phy_device *phydev)
  1332. {
  1333. int value;
  1334. mutex_lock(&phydev->lock);
  1335. value = phy_read(phydev, MII_BMCR);
  1336. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  1337. mutex_unlock(&phydev->lock);
  1338. return 0;
  1339. }
  1340. EXPORT_SYMBOL(genphy_resume);
  1341. static int gen10g_resume(struct phy_device *phydev)
  1342. {
  1343. return 0;
  1344. }
  1345. static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  1346. {
  1347. /* The default values for phydev->supported are provided by the PHY
  1348. * driver "features" member, we want to reset to sane defaults first
  1349. * before supporting higher speeds.
  1350. */
  1351. phydev->supported &= PHY_DEFAULT_FEATURES;
  1352. switch (max_speed) {
  1353. default:
  1354. return -ENOTSUPP;
  1355. case SPEED_1000:
  1356. phydev->supported |= PHY_1000BT_FEATURES;
  1357. /* fall through */
  1358. case SPEED_100:
  1359. phydev->supported |= PHY_100BT_FEATURES;
  1360. /* fall through */
  1361. case SPEED_10:
  1362. phydev->supported |= PHY_10BT_FEATURES;
  1363. }
  1364. return 0;
  1365. }
  1366. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  1367. {
  1368. int err;
  1369. err = __set_phy_supported(phydev, max_speed);
  1370. if (err)
  1371. return err;
  1372. phydev->advertising = phydev->supported;
  1373. return 0;
  1374. }
  1375. EXPORT_SYMBOL(phy_set_max_speed);
  1376. static void of_set_phy_supported(struct phy_device *phydev)
  1377. {
  1378. struct device_node *node = phydev->mdio.dev.of_node;
  1379. u32 max_speed;
  1380. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1381. return;
  1382. if (!node)
  1383. return;
  1384. if (!of_property_read_u32(node, "max-speed", &max_speed))
  1385. __set_phy_supported(phydev, max_speed);
  1386. }
  1387. static void of_set_phy_eee_broken(struct phy_device *phydev)
  1388. {
  1389. struct device_node *node = phydev->mdio.dev.of_node;
  1390. u32 broken = 0;
  1391. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1392. return;
  1393. if (!node)
  1394. return;
  1395. if (of_property_read_bool(node, "eee-broken-100tx"))
  1396. broken |= MDIO_EEE_100TX;
  1397. if (of_property_read_bool(node, "eee-broken-1000t"))
  1398. broken |= MDIO_EEE_1000T;
  1399. if (of_property_read_bool(node, "eee-broken-10gt"))
  1400. broken |= MDIO_EEE_10GT;
  1401. if (of_property_read_bool(node, "eee-broken-1000kx"))
  1402. broken |= MDIO_EEE_1000KX;
  1403. if (of_property_read_bool(node, "eee-broken-10gkx4"))
  1404. broken |= MDIO_EEE_10GKX4;
  1405. if (of_property_read_bool(node, "eee-broken-10gkr"))
  1406. broken |= MDIO_EEE_10GKR;
  1407. phydev->eee_broken_modes = broken;
  1408. }
  1409. /**
  1410. * phy_probe - probe and init a PHY device
  1411. * @dev: device to probe and init
  1412. *
  1413. * Description: Take care of setting up the phy_device structure,
  1414. * set the state to READY (the driver's init function should
  1415. * set it to STARTING if needed).
  1416. */
  1417. static int phy_probe(struct device *dev)
  1418. {
  1419. struct phy_device *phydev = to_phy_device(dev);
  1420. struct device_driver *drv = phydev->mdio.dev.driver;
  1421. struct phy_driver *phydrv = to_phy_driver(drv);
  1422. int err = 0;
  1423. phydev->drv = phydrv;
  1424. /* Disable the interrupt if the PHY doesn't support it
  1425. * but the interrupt is still a valid one
  1426. */
  1427. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  1428. phy_interrupt_is_valid(phydev))
  1429. phydev->irq = PHY_POLL;
  1430. if (phydrv->flags & PHY_IS_INTERNAL)
  1431. phydev->is_internal = true;
  1432. mutex_lock(&phydev->lock);
  1433. /* Start out supporting everything. Eventually,
  1434. * a controller will attach, and may modify one
  1435. * or both of these values
  1436. */
  1437. phydev->supported = phydrv->features;
  1438. of_set_phy_supported(phydev);
  1439. phydev->advertising = phydev->supported;
  1440. /* Get the EEE modes we want to prohibit. We will ask
  1441. * the PHY stop advertising these mode later on
  1442. */
  1443. of_set_phy_eee_broken(phydev);
  1444. /* The Pause Frame bits indicate that the PHY can support passing
  1445. * pause frames. During autonegotiation, the PHYs will determine if
  1446. * they should allow pause frames to pass. The MAC driver should then
  1447. * use that result to determine whether to enable flow control via
  1448. * pause frames.
  1449. *
  1450. * Normally, PHY drivers should not set the Pause bits, and instead
  1451. * allow phylib to do that. However, there may be some situations
  1452. * (e.g. hardware erratum) where the driver wants to set only one
  1453. * of these bits.
  1454. */
  1455. if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
  1456. phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1457. phydev->supported |= phydrv->features &
  1458. (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1459. } else {
  1460. phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  1461. }
  1462. /* Set the state to READY by default */
  1463. phydev->state = PHY_READY;
  1464. if (phydev->drv->probe)
  1465. err = phydev->drv->probe(phydev);
  1466. mutex_unlock(&phydev->lock);
  1467. return err;
  1468. }
  1469. static int phy_remove(struct device *dev)
  1470. {
  1471. struct phy_device *phydev = to_phy_device(dev);
  1472. cancel_delayed_work_sync(&phydev->state_queue);
  1473. mutex_lock(&phydev->lock);
  1474. phydev->state = PHY_DOWN;
  1475. mutex_unlock(&phydev->lock);
  1476. if (phydev->drv && phydev->drv->remove)
  1477. phydev->drv->remove(phydev);
  1478. phydev->drv = NULL;
  1479. return 0;
  1480. }
  1481. /**
  1482. * phy_driver_register - register a phy_driver with the PHY layer
  1483. * @new_driver: new phy_driver to register
  1484. * @owner: module owning this PHY
  1485. */
  1486. int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
  1487. {
  1488. int retval;
  1489. new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
  1490. new_driver->mdiodrv.driver.name = new_driver->name;
  1491. new_driver->mdiodrv.driver.bus = &mdio_bus_type;
  1492. new_driver->mdiodrv.driver.probe = phy_probe;
  1493. new_driver->mdiodrv.driver.remove = phy_remove;
  1494. new_driver->mdiodrv.driver.owner = owner;
  1495. retval = driver_register(&new_driver->mdiodrv.driver);
  1496. if (retval) {
  1497. pr_err("%s: Error %d in registering driver\n",
  1498. new_driver->name, retval);
  1499. return retval;
  1500. }
  1501. pr_debug("%s: Registered new driver\n", new_driver->name);
  1502. return 0;
  1503. }
  1504. EXPORT_SYMBOL(phy_driver_register);
  1505. int phy_drivers_register(struct phy_driver *new_driver, int n,
  1506. struct module *owner)
  1507. {
  1508. int i, ret = 0;
  1509. for (i = 0; i < n; i++) {
  1510. ret = phy_driver_register(new_driver + i, owner);
  1511. if (ret) {
  1512. while (i-- > 0)
  1513. phy_driver_unregister(new_driver + i);
  1514. break;
  1515. }
  1516. }
  1517. return ret;
  1518. }
  1519. EXPORT_SYMBOL(phy_drivers_register);
  1520. void phy_driver_unregister(struct phy_driver *drv)
  1521. {
  1522. driver_unregister(&drv->mdiodrv.driver);
  1523. }
  1524. EXPORT_SYMBOL(phy_driver_unregister);
  1525. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1526. {
  1527. int i;
  1528. for (i = 0; i < n; i++)
  1529. phy_driver_unregister(drv + i);
  1530. }
  1531. EXPORT_SYMBOL(phy_drivers_unregister);
  1532. static struct phy_driver genphy_driver[] = {
  1533. {
  1534. .phy_id = 0xffffffff,
  1535. .phy_id_mask = 0xffffffff,
  1536. .name = "Generic PHY",
  1537. .soft_reset = genphy_no_soft_reset,
  1538. .config_init = genphy_config_init,
  1539. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  1540. SUPPORTED_AUI | SUPPORTED_FIBRE |
  1541. SUPPORTED_BNC,
  1542. .config_aneg = genphy_config_aneg,
  1543. .aneg_done = genphy_aneg_done,
  1544. .read_status = genphy_read_status,
  1545. .suspend = genphy_suspend,
  1546. .resume = genphy_resume,
  1547. }, {
  1548. .phy_id = 0xffffffff,
  1549. .phy_id_mask = 0xffffffff,
  1550. .name = "Generic 10G PHY",
  1551. .soft_reset = gen10g_soft_reset,
  1552. .config_init = gen10g_config_init,
  1553. .features = 0,
  1554. .config_aneg = gen10g_config_aneg,
  1555. .read_status = gen10g_read_status,
  1556. .suspend = gen10g_suspend,
  1557. .resume = gen10g_resume,
  1558. } };
  1559. static int __init phy_init(void)
  1560. {
  1561. int rc;
  1562. rc = mdio_bus_init();
  1563. if (rc)
  1564. return rc;
  1565. rc = phy_drivers_register(genphy_driver,
  1566. ARRAY_SIZE(genphy_driver), THIS_MODULE);
  1567. if (rc)
  1568. mdio_bus_exit();
  1569. return rc;
  1570. }
  1571. static void __exit phy_exit(void)
  1572. {
  1573. phy_drivers_unregister(genphy_driver,
  1574. ARRAY_SIZE(genphy_driver));
  1575. mdio_bus_exit();
  1576. }
  1577. subsys_initcall(phy_init);
  1578. module_exit(phy_exit);