phy_device.c 41 KB

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