phy_device.c 50 KB

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