mdio_bus.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* MDIO Bus interface
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/ethtool.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/micrel_phy.h>
  20. #include <linux/mii.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/phy.h>
  27. #include <linux/reset.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/slab.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/string.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/unistd.h>
  34. #define CREATE_TRACE_POINTS
  35. #include <trace/events/mdio.h>
  36. #include "mdio-boardinfo.h"
  37. static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
  38. {
  39. /* Deassert the optional reset signal */
  40. mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
  41. "reset", GPIOD_OUT_LOW);
  42. if (IS_ERR(mdiodev->reset_gpio))
  43. return PTR_ERR(mdiodev->reset_gpio);
  44. if (mdiodev->reset_gpio)
  45. gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
  46. return 0;
  47. }
  48. static int mdiobus_register_reset(struct mdio_device *mdiodev)
  49. {
  50. struct reset_control *reset;
  51. reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
  52. if (IS_ERR(reset))
  53. return PTR_ERR(reset);
  54. mdiodev->reset_ctrl = reset;
  55. return 0;
  56. }
  57. int mdiobus_register_device(struct mdio_device *mdiodev)
  58. {
  59. int err;
  60. if (mdiodev->bus->mdio_map[mdiodev->addr])
  61. return -EBUSY;
  62. if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
  63. err = mdiobus_register_gpiod(mdiodev);
  64. if (err)
  65. return err;
  66. err = mdiobus_register_reset(mdiodev);
  67. if (err)
  68. return err;
  69. /* Assert the reset signal */
  70. mdio_device_reset(mdiodev, 1);
  71. }
  72. mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(mdiobus_register_device);
  76. int mdiobus_unregister_device(struct mdio_device *mdiodev)
  77. {
  78. if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
  79. return -EINVAL;
  80. gpiod_put(mdiodev->reset_gpio);
  81. reset_control_put(mdiodev->reset_ctrl);
  82. mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(mdiobus_unregister_device);
  86. static struct mdio_device *mdiobus_find_device(struct mii_bus *bus, int addr)
  87. {
  88. bool addr_valid = addr >= 0 && addr < ARRAY_SIZE(bus->mdio_map);
  89. if (WARN_ONCE(!addr_valid, "addr %d out of range\n", addr))
  90. return NULL;
  91. return bus->mdio_map[addr];
  92. }
  93. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
  94. {
  95. struct mdio_device *mdiodev;
  96. mdiodev = mdiobus_find_device(bus, addr);
  97. if (!mdiodev)
  98. return NULL;
  99. if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
  100. return NULL;
  101. return container_of(mdiodev, struct phy_device, mdio);
  102. }
  103. EXPORT_SYMBOL(mdiobus_get_phy);
  104. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
  105. {
  106. return mdiobus_find_device(bus, addr) != NULL;
  107. }
  108. EXPORT_SYMBOL(mdiobus_is_registered_device);
  109. /**
  110. * mdiobus_alloc_size - allocate a mii_bus structure
  111. * @size: extra amount of memory to allocate for private storage.
  112. * If non-zero, then bus->priv is points to that memory.
  113. *
  114. * Description: called by a bus driver to allocate an mii_bus
  115. * structure to fill in.
  116. */
  117. struct mii_bus *mdiobus_alloc_size(size_t size)
  118. {
  119. struct mii_bus *bus;
  120. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  121. size_t alloc_size;
  122. int i;
  123. /* If we alloc extra space, it should be aligned */
  124. if (size)
  125. alloc_size = aligned_size + size;
  126. else
  127. alloc_size = sizeof(*bus);
  128. bus = kzalloc(alloc_size, GFP_KERNEL);
  129. if (!bus)
  130. return NULL;
  131. bus->state = MDIOBUS_ALLOCATED;
  132. if (size)
  133. bus->priv = (void *)bus + aligned_size;
  134. /* Initialise the interrupts to polling and 64-bit seqcounts */
  135. for (i = 0; i < PHY_MAX_ADDR; i++) {
  136. bus->irq[i] = PHY_POLL;
  137. u64_stats_init(&bus->stats[i].syncp);
  138. }
  139. return bus;
  140. }
  141. EXPORT_SYMBOL(mdiobus_alloc_size);
  142. /**
  143. * mdiobus_release - mii_bus device release callback
  144. * @d: the target struct device that contains the mii_bus
  145. *
  146. * Description: called when the last reference to an mii_bus is
  147. * dropped, to free the underlying memory.
  148. */
  149. static void mdiobus_release(struct device *d)
  150. {
  151. struct mii_bus *bus = to_mii_bus(d);
  152. WARN(bus->state != MDIOBUS_RELEASED &&
  153. /* for compatibility with error handling in drivers */
  154. bus->state != MDIOBUS_ALLOCATED,
  155. "%s: not in RELEASED or ALLOCATED state\n",
  156. bus->id);
  157. if (bus->state == MDIOBUS_RELEASED)
  158. fwnode_handle_put(dev_fwnode(d));
  159. kfree(bus);
  160. }
  161. struct mdio_bus_stat_attr {
  162. int addr;
  163. unsigned int field_offset;
  164. };
  165. static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
  166. {
  167. const char *p = (const char *)s + offset;
  168. unsigned int start;
  169. u64 val = 0;
  170. do {
  171. start = u64_stats_fetch_begin(&s->syncp);
  172. val = u64_stats_read((const u64_stats_t *)p);
  173. } while (u64_stats_fetch_retry(&s->syncp, start));
  174. return val;
  175. }
  176. static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
  177. {
  178. unsigned int i;
  179. u64 val = 0;
  180. for (i = 0; i < PHY_MAX_ADDR; i++)
  181. val += mdio_bus_get_stat(&bus->stats[i], offset);
  182. return val;
  183. }
  184. static ssize_t mdio_bus_stat_field_show(struct device *dev,
  185. struct device_attribute *attr,
  186. char *buf)
  187. {
  188. struct mii_bus *bus = to_mii_bus(dev);
  189. struct mdio_bus_stat_attr *sattr;
  190. struct dev_ext_attribute *eattr;
  191. u64 val;
  192. eattr = container_of(attr, struct dev_ext_attribute, attr);
  193. sattr = eattr->var;
  194. if (sattr->addr < 0)
  195. val = mdio_bus_get_global_stat(bus, sattr->field_offset);
  196. else
  197. val = mdio_bus_get_stat(&bus->stats[sattr->addr],
  198. sattr->field_offset);
  199. return sysfs_emit(buf, "%llu\n", val);
  200. }
  201. static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
  202. struct device_attribute *attr,
  203. char *buf)
  204. {
  205. struct mdio_device *mdiodev = to_mdio_device(dev);
  206. struct mii_bus *bus = mdiodev->bus;
  207. struct mdio_bus_stat_attr *sattr;
  208. struct dev_ext_attribute *eattr;
  209. int addr = mdiodev->addr;
  210. u64 val;
  211. eattr = container_of(attr, struct dev_ext_attribute, attr);
  212. sattr = eattr->var;
  213. val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
  214. return sysfs_emit(buf, "%llu\n", val);
  215. }
  216. #define MDIO_BUS_STATS_ATTR_DECL(field, file) \
  217. static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \
  218. .attr = { .attr = { .name = file, .mode = 0444 }, \
  219. .show = mdio_bus_stat_field_show, \
  220. }, \
  221. .var = &((struct mdio_bus_stat_attr) { \
  222. -1, offsetof(struct mdio_bus_stats, field) \
  223. }), \
  224. }; \
  225. static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \
  226. .attr = { .attr = { .name = file, .mode = 0444 }, \
  227. .show = mdio_bus_device_stat_field_show, \
  228. }, \
  229. .var = &((struct mdio_bus_stat_attr) { \
  230. -1, offsetof(struct mdio_bus_stats, field) \
  231. }), \
  232. };
  233. #define MDIO_BUS_STATS_ATTR(field) \
  234. MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
  235. MDIO_BUS_STATS_ATTR(transfers);
  236. MDIO_BUS_STATS_ATTR(errors);
  237. MDIO_BUS_STATS_ATTR(writes);
  238. MDIO_BUS_STATS_ATTR(reads);
  239. #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \
  240. static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
  241. .attr = { .attr = { .name = file, .mode = 0444 }, \
  242. .show = mdio_bus_stat_field_show, \
  243. }, \
  244. .var = &((struct mdio_bus_stat_attr) { \
  245. addr, offsetof(struct mdio_bus_stats, field) \
  246. }), \
  247. }
  248. #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \
  249. MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \
  250. __stringify(field) "_" __stringify(addr))
  251. #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \
  252. MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \
  253. MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \
  254. MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \
  255. MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \
  256. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
  257. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
  258. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
  259. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
  260. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
  261. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
  262. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
  263. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
  264. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
  265. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
  266. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
  267. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
  268. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
  269. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
  270. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
  271. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
  272. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
  273. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
  274. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
  275. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
  276. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
  277. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
  278. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
  279. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
  280. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
  281. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
  282. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
  283. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
  284. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
  285. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
  286. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
  287. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
  288. #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \
  289. &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \
  290. &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \
  291. &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \
  292. &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \
  293. static struct attribute *mdio_bus_statistics_attrs[] = {
  294. &dev_attr_mdio_bus_transfers.attr.attr,
  295. &dev_attr_mdio_bus_errors.attr.attr,
  296. &dev_attr_mdio_bus_writes.attr.attr,
  297. &dev_attr_mdio_bus_reads.attr.attr,
  298. MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
  299. MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
  300. MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
  301. MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
  302. MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
  303. MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
  304. MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
  305. MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
  306. MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
  307. MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
  308. MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
  309. MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
  310. MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
  311. MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
  312. MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
  313. MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
  314. MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
  315. MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
  316. MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
  317. MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
  318. MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
  319. MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
  320. MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
  321. MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
  322. MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
  323. MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
  324. MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
  325. MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
  326. MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
  327. MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
  328. MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
  329. MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
  330. NULL,
  331. };
  332. static const struct attribute_group mdio_bus_statistics_group = {
  333. .name = "statistics",
  334. .attrs = mdio_bus_statistics_attrs,
  335. };
  336. static const struct attribute_group *mdio_bus_groups[] = {
  337. &mdio_bus_statistics_group,
  338. NULL,
  339. };
  340. static struct class mdio_bus_class = {
  341. .name = "mdio_bus",
  342. .dev_release = mdiobus_release,
  343. .dev_groups = mdio_bus_groups,
  344. };
  345. /**
  346. * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
  347. * @mdio_name: The name of a mdiobus.
  348. *
  349. * Returns a reference to the mii_bus, or NULL if none found. The
  350. * embedded struct device will have its reference count incremented,
  351. * and this must be put_deviced'ed once the bus is finished with.
  352. */
  353. struct mii_bus *mdio_find_bus(const char *mdio_name)
  354. {
  355. struct device *d;
  356. d = class_find_device_by_name(&mdio_bus_class, mdio_name);
  357. return d ? to_mii_bus(d) : NULL;
  358. }
  359. EXPORT_SYMBOL(mdio_find_bus);
  360. #if IS_ENABLED(CONFIG_OF_MDIO)
  361. /**
  362. * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
  363. * @mdio_bus_np: Pointer to the mii_bus.
  364. *
  365. * Returns a reference to the mii_bus, or NULL if none found. The
  366. * embedded struct device will have its reference count incremented,
  367. * and this must be put once the bus is finished with.
  368. *
  369. * Because the association of a device_node and mii_bus is made via
  370. * of_mdiobus_register(), the mii_bus cannot be found before it is
  371. * registered with of_mdiobus_register().
  372. *
  373. */
  374. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  375. {
  376. struct device *d;
  377. if (!mdio_bus_np)
  378. return NULL;
  379. d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
  380. return d ? to_mii_bus(d) : NULL;
  381. }
  382. EXPORT_SYMBOL(of_mdio_find_bus);
  383. /* Walk the list of subnodes of a mdio bus and look for a node that
  384. * matches the mdio device's address with its 'reg' property. If
  385. * found, set the of_node pointer for the mdio device. This allows
  386. * auto-probed phy devices to be supplied with information passed in
  387. * via DT.
  388. * If a PHY package is found, PHY is searched also there.
  389. */
  390. static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev,
  391. struct device_node *np)
  392. {
  393. struct device_node *child;
  394. for_each_available_child_of_node(np, child) {
  395. int addr;
  396. if (of_node_name_eq(child, "ethernet-phy-package")) {
  397. /* Validate PHY package reg presence */
  398. if (!of_property_present(child, "reg")) {
  399. of_node_put(child);
  400. return -EINVAL;
  401. }
  402. if (!of_mdiobus_find_phy(dev, mdiodev, child)) {
  403. /* The refcount for the PHY package will be
  404. * incremented later when PHY join the Package.
  405. */
  406. of_node_put(child);
  407. return 0;
  408. }
  409. continue;
  410. }
  411. addr = of_mdio_parse_addr(dev, child);
  412. if (addr < 0)
  413. continue;
  414. if (addr == mdiodev->addr) {
  415. device_set_node(dev, of_fwnode_handle(child));
  416. /* The refcount on "child" is passed to the mdio
  417. * device. Do _not_ use of_node_put(child) here.
  418. */
  419. return 0;
  420. }
  421. }
  422. return -ENODEV;
  423. }
  424. static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
  425. struct mdio_device *mdiodev)
  426. {
  427. struct device *dev = &mdiodev->dev;
  428. if (dev->of_node || !bus->dev.of_node)
  429. return;
  430. of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node);
  431. }
  432. #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
  433. static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
  434. struct mdio_device *mdiodev)
  435. {
  436. }
  437. #endif
  438. /**
  439. * mdiobus_create_device - create a full MDIO device given
  440. * a mdio_board_info structure
  441. * @bus: MDIO bus to create the devices on
  442. * @bi: mdio_board_info structure describing the devices
  443. *
  444. * Returns 0 on success or < 0 on error.
  445. */
  446. static int mdiobus_create_device(struct mii_bus *bus,
  447. struct mdio_board_info *bi)
  448. {
  449. struct mdio_device *mdiodev;
  450. int ret = 0;
  451. mdiodev = mdio_device_create(bus, bi->mdio_addr);
  452. if (IS_ERR(mdiodev))
  453. return -ENODEV;
  454. strscpy(mdiodev->modalias, bi->modalias,
  455. sizeof(mdiodev->modalias));
  456. mdiodev->bus_match = mdio_device_bus_match;
  457. mdiodev->dev.platform_data = (void *)bi->platform_data;
  458. ret = mdio_device_register(mdiodev);
  459. if (ret)
  460. mdio_device_free(mdiodev);
  461. return ret;
  462. }
  463. static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45)
  464. {
  465. struct phy_device *phydev = ERR_PTR(-ENODEV);
  466. int err;
  467. phydev = get_phy_device(bus, addr, c45);
  468. if (IS_ERR(phydev))
  469. return phydev;
  470. /* For DT, see if the auto-probed phy has a corresponding child
  471. * in the bus node, and set the of_node pointer in this case.
  472. */
  473. of_mdiobus_link_mdiodev(bus, &phydev->mdio);
  474. err = phy_device_register(phydev);
  475. if (err) {
  476. phy_device_free(phydev);
  477. return ERR_PTR(-ENODEV);
  478. }
  479. return phydev;
  480. }
  481. /**
  482. * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
  483. * @bus: mii_bus to scan
  484. * @addr: address on bus to scan
  485. *
  486. * This function scans one address on the MDIO bus, looking for
  487. * devices which can be identified using a vendor/product ID in
  488. * registers 2 and 3. Not all MDIO devices have such registers, but
  489. * PHY devices typically do. Hence this function assumes anything
  490. * found is a PHY, or can be treated as a PHY. Other MDIO devices,
  491. * such as switches, will probably not be found during the scan.
  492. */
  493. struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr)
  494. {
  495. return mdiobus_scan(bus, addr, false);
  496. }
  497. EXPORT_SYMBOL(mdiobus_scan_c22);
  498. /**
  499. * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
  500. * @bus: mii_bus to scan
  501. * @addr: address on bus to scan
  502. *
  503. * This function scans one address on the MDIO bus, looking for
  504. * devices which can be identified using a vendor/product ID in
  505. * registers 2 and 3. Not all MDIO devices have such registers, but
  506. * PHY devices typically do. Hence this function assumes anything
  507. * found is a PHY, or can be treated as a PHY. Other MDIO devices,
  508. * such as switches, will probably not be found during the scan.
  509. */
  510. static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr)
  511. {
  512. return mdiobus_scan(bus, addr, true);
  513. }
  514. static int mdiobus_scan_bus_c22(struct mii_bus *bus)
  515. {
  516. int i;
  517. for (i = 0; i < PHY_MAX_ADDR; i++) {
  518. if ((bus->phy_mask & BIT(i)) == 0) {
  519. struct phy_device *phydev;
  520. phydev = mdiobus_scan_c22(bus, i);
  521. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
  522. return PTR_ERR(phydev);
  523. }
  524. }
  525. return 0;
  526. }
  527. static int mdiobus_scan_bus_c45(struct mii_bus *bus)
  528. {
  529. int i;
  530. for (i = 0; i < PHY_MAX_ADDR; i++) {
  531. if ((bus->phy_mask & BIT(i)) == 0) {
  532. struct phy_device *phydev;
  533. /* Don't scan C45 if we already have a C22 device */
  534. if (bus->mdio_map[i])
  535. continue;
  536. phydev = mdiobus_scan_c45(bus, i);
  537. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV))
  538. return PTR_ERR(phydev);
  539. }
  540. }
  541. return 0;
  542. }
  543. /* There are some C22 PHYs which do bad things when where is a C45
  544. * transaction on the bus, like accepting a read themselves, and
  545. * stomping over the true devices reply, to performing a write to
  546. * themselves which was intended for another device. Now that C22
  547. * devices have been found, see if any of them are bad for C45, and if we
  548. * should skip the C45 scan.
  549. */
  550. static bool mdiobus_prevent_c45_scan(struct mii_bus *bus)
  551. {
  552. int i;
  553. for (i = 0; i < PHY_MAX_ADDR; i++) {
  554. struct phy_device *phydev;
  555. u32 oui;
  556. phydev = mdiobus_get_phy(bus, i);
  557. if (!phydev)
  558. continue;
  559. oui = phydev->phy_id >> 10;
  560. if (oui == MICREL_OUI)
  561. return true;
  562. }
  563. return false;
  564. }
  565. /**
  566. * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  567. * @bus: target mii_bus
  568. * @owner: module containing bus accessor functions
  569. *
  570. * Description: Called by a bus driver to bring up all the PHYs
  571. * on a given bus, and attach them to the bus. Drivers should use
  572. * mdiobus_register() rather than __mdiobus_register() unless they
  573. * need to pass a specific owner module. MDIO devices which are not
  574. * PHYs will not be brought up by this function. They are expected
  575. * to be explicitly listed in DT and instantiated by of_mdiobus_register().
  576. *
  577. * Returns 0 on success or < 0 on error.
  578. */
  579. int __mdiobus_register(struct mii_bus *bus, struct module *owner)
  580. {
  581. struct mdio_device *mdiodev;
  582. struct gpio_desc *gpiod;
  583. bool prevent_c45_scan;
  584. int i, err;
  585. if (!bus || !bus->name)
  586. return -EINVAL;
  587. /* An access method always needs both read and write operations */
  588. if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45)
  589. return -EINVAL;
  590. /* At least one method is mandatory */
  591. if (!bus->read && !bus->read_c45)
  592. return -EINVAL;
  593. if (bus->parent && bus->parent->of_node)
  594. bus->parent->of_node->fwnode.flags |=
  595. FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
  596. WARN(bus->state != MDIOBUS_ALLOCATED &&
  597. bus->state != MDIOBUS_UNREGISTERED,
  598. "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
  599. bus->owner = owner;
  600. bus->dev.parent = bus->parent;
  601. bus->dev.class = &mdio_bus_class;
  602. bus->dev.groups = NULL;
  603. dev_set_name(&bus->dev, "%s", bus->id);
  604. /* If the bus state is allocated, we're registering a fresh bus
  605. * that may have a fwnode associated with it. Grab a reference
  606. * to the fwnode. This will be dropped when the bus is released.
  607. * If the bus was set to unregistered, it means that the bus was
  608. * previously registered, and we've already grabbed a reference.
  609. */
  610. if (bus->state == MDIOBUS_ALLOCATED)
  611. fwnode_handle_get(dev_fwnode(&bus->dev));
  612. /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
  613. * the device in mdiobus_free()
  614. *
  615. * State will be updated later in this function in case of success
  616. */
  617. bus->state = MDIOBUS_UNREGISTERED;
  618. err = device_register(&bus->dev);
  619. if (err) {
  620. pr_err("mii_bus %s failed to register\n", bus->id);
  621. return -EINVAL;
  622. }
  623. mutex_init(&bus->mdio_lock);
  624. mutex_init(&bus->shared_lock);
  625. /* assert bus level PHY GPIO reset */
  626. gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
  627. if (IS_ERR(gpiod)) {
  628. err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
  629. "mii_bus %s couldn't get reset GPIO\n",
  630. bus->id);
  631. device_del(&bus->dev);
  632. return err;
  633. } else if (gpiod) {
  634. bus->reset_gpiod = gpiod;
  635. fsleep(bus->reset_delay_us);
  636. gpiod_set_value_cansleep(gpiod, 0);
  637. if (bus->reset_post_delay_us > 0)
  638. fsleep(bus->reset_post_delay_us);
  639. }
  640. if (bus->reset) {
  641. err = bus->reset(bus);
  642. if (err)
  643. goto error_reset_gpiod;
  644. }
  645. if (bus->read) {
  646. err = mdiobus_scan_bus_c22(bus);
  647. if (err)
  648. goto error;
  649. }
  650. prevent_c45_scan = mdiobus_prevent_c45_scan(bus);
  651. if (!prevent_c45_scan && bus->read_c45) {
  652. err = mdiobus_scan_bus_c45(bus);
  653. if (err)
  654. goto error;
  655. }
  656. mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
  657. bus->state = MDIOBUS_REGISTERED;
  658. dev_dbg(&bus->dev, "probed\n");
  659. return 0;
  660. error:
  661. for (i = 0; i < PHY_MAX_ADDR; i++) {
  662. mdiodev = bus->mdio_map[i];
  663. if (!mdiodev)
  664. continue;
  665. mdiodev->device_remove(mdiodev);
  666. mdiodev->device_free(mdiodev);
  667. }
  668. error_reset_gpiod:
  669. /* Put PHYs in RESET to save power */
  670. if (bus->reset_gpiod)
  671. gpiod_set_value_cansleep(bus->reset_gpiod, 1);
  672. device_del(&bus->dev);
  673. return err;
  674. }
  675. EXPORT_SYMBOL(__mdiobus_register);
  676. void mdiobus_unregister(struct mii_bus *bus)
  677. {
  678. struct mdio_device *mdiodev;
  679. int i;
  680. if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
  681. return;
  682. bus->state = MDIOBUS_UNREGISTERED;
  683. for (i = 0; i < PHY_MAX_ADDR; i++) {
  684. mdiodev = bus->mdio_map[i];
  685. if (!mdiodev)
  686. continue;
  687. mdiodev->device_remove(mdiodev);
  688. mdiodev->device_free(mdiodev);
  689. }
  690. /* Put PHYs in RESET to save power */
  691. if (bus->reset_gpiod)
  692. gpiod_set_value_cansleep(bus->reset_gpiod, 1);
  693. device_del(&bus->dev);
  694. }
  695. EXPORT_SYMBOL(mdiobus_unregister);
  696. /**
  697. * mdiobus_free - free a struct mii_bus
  698. * @bus: mii_bus to free
  699. *
  700. * This function releases the reference to the underlying device
  701. * object in the mii_bus. If this is the last reference, the mii_bus
  702. * will be freed.
  703. */
  704. void mdiobus_free(struct mii_bus *bus)
  705. {
  706. /* For compatibility with error handling in drivers. */
  707. if (bus->state == MDIOBUS_ALLOCATED) {
  708. kfree(bus);
  709. return;
  710. }
  711. WARN(bus->state != MDIOBUS_UNREGISTERED,
  712. "%s: not in UNREGISTERED state\n", bus->id);
  713. bus->state = MDIOBUS_RELEASED;
  714. put_device(&bus->dev);
  715. }
  716. EXPORT_SYMBOL(mdiobus_free);
  717. static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
  718. {
  719. preempt_disable();
  720. u64_stats_update_begin(&stats->syncp);
  721. u64_stats_inc(&stats->transfers);
  722. if (ret < 0) {
  723. u64_stats_inc(&stats->errors);
  724. goto out;
  725. }
  726. if (op)
  727. u64_stats_inc(&stats->reads);
  728. else
  729. u64_stats_inc(&stats->writes);
  730. out:
  731. u64_stats_update_end(&stats->syncp);
  732. preempt_enable();
  733. }
  734. /**
  735. * __mdiobus_read - Unlocked version of the mdiobus_read function
  736. * @bus: the mii_bus struct
  737. * @addr: the phy address
  738. * @regnum: register number to read
  739. *
  740. * Read a MDIO bus register. Caller must hold the mdio bus lock.
  741. *
  742. * NOTE: MUST NOT be called from interrupt context.
  743. */
  744. int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  745. {
  746. int retval;
  747. lockdep_assert_held_once(&bus->mdio_lock);
  748. if (addr >= PHY_MAX_ADDR)
  749. return -ENXIO;
  750. if (bus->read)
  751. retval = bus->read(bus, addr, regnum);
  752. else
  753. retval = -EOPNOTSUPP;
  754. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  755. mdiobus_stats_acct(&bus->stats[addr], true, retval);
  756. return retval;
  757. }
  758. EXPORT_SYMBOL(__mdiobus_read);
  759. /**
  760. * __mdiobus_write - Unlocked version of the mdiobus_write function
  761. * @bus: the mii_bus struct
  762. * @addr: the phy address
  763. * @regnum: register number to write
  764. * @val: value to write to @regnum
  765. *
  766. * Write a MDIO bus register. Caller must hold the mdio bus lock.
  767. *
  768. * NOTE: MUST NOT be called from interrupt context.
  769. */
  770. int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  771. {
  772. int err;
  773. lockdep_assert_held_once(&bus->mdio_lock);
  774. if (addr >= PHY_MAX_ADDR)
  775. return -ENXIO;
  776. if (bus->write)
  777. err = bus->write(bus, addr, regnum, val);
  778. else
  779. err = -EOPNOTSUPP;
  780. trace_mdio_access(bus, 0, addr, regnum, val, err);
  781. mdiobus_stats_acct(&bus->stats[addr], false, err);
  782. return err;
  783. }
  784. EXPORT_SYMBOL(__mdiobus_write);
  785. /**
  786. * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
  787. * @bus: the mii_bus struct
  788. * @addr: the phy address
  789. * @regnum: register number to modify
  790. * @mask: bit mask of bits to clear
  791. * @set: bit mask of bits to set
  792. *
  793. * Read, modify, and if any change, write the register value back to the
  794. * device. Any error returns a negative number.
  795. *
  796. * NOTE: MUST NOT be called from interrupt context.
  797. */
  798. int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
  799. u16 mask, u16 set)
  800. {
  801. int new, ret;
  802. ret = __mdiobus_read(bus, addr, regnum);
  803. if (ret < 0)
  804. return ret;
  805. new = (ret & ~mask) | set;
  806. if (new == ret)
  807. return 0;
  808. ret = __mdiobus_write(bus, addr, regnum, new);
  809. return ret < 0 ? ret : 1;
  810. }
  811. EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
  812. /**
  813. * __mdiobus_c45_read - Unlocked version of the mdiobus_c45_read function
  814. * @bus: the mii_bus struct
  815. * @addr: the phy address
  816. * @devad: device address to read
  817. * @regnum: register number to read
  818. *
  819. * Read a MDIO bus register. Caller must hold the mdio bus lock.
  820. *
  821. * NOTE: MUST NOT be called from interrupt context.
  822. */
  823. int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum)
  824. {
  825. int retval;
  826. lockdep_assert_held_once(&bus->mdio_lock);
  827. if (addr >= PHY_MAX_ADDR)
  828. return -ENXIO;
  829. if (bus->read_c45)
  830. retval = bus->read_c45(bus, addr, devad, regnum);
  831. else
  832. retval = -EOPNOTSUPP;
  833. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  834. mdiobus_stats_acct(&bus->stats[addr], true, retval);
  835. return retval;
  836. }
  837. EXPORT_SYMBOL(__mdiobus_c45_read);
  838. /**
  839. * __mdiobus_c45_write - Unlocked version of the mdiobus_write function
  840. * @bus: the mii_bus struct
  841. * @addr: the phy address
  842. * @devad: device address to read
  843. * @regnum: register number to write
  844. * @val: value to write to @regnum
  845. *
  846. * Write a MDIO bus register. Caller must hold the mdio bus lock.
  847. *
  848. * NOTE: MUST NOT be called from interrupt context.
  849. */
  850. int __mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum,
  851. u16 val)
  852. {
  853. int err;
  854. lockdep_assert_held_once(&bus->mdio_lock);
  855. if (addr >= PHY_MAX_ADDR)
  856. return -ENXIO;
  857. if (bus->write_c45)
  858. err = bus->write_c45(bus, addr, devad, regnum, val);
  859. else
  860. err = -EOPNOTSUPP;
  861. trace_mdio_access(bus, 0, addr, regnum, val, err);
  862. mdiobus_stats_acct(&bus->stats[addr], false, err);
  863. return err;
  864. }
  865. EXPORT_SYMBOL(__mdiobus_c45_write);
  866. /**
  867. * __mdiobus_c45_modify_changed - Unlocked version of the mdiobus_modify function
  868. * @bus: the mii_bus struct
  869. * @addr: the phy address
  870. * @devad: device address to read
  871. * @regnum: register number to modify
  872. * @mask: bit mask of bits to clear
  873. * @set: bit mask of bits to set
  874. *
  875. * Read, modify, and if any change, write the register value back to the
  876. * device. Any error returns a negative number.
  877. *
  878. * NOTE: MUST NOT be called from interrupt context.
  879. */
  880. static int __mdiobus_c45_modify_changed(struct mii_bus *bus, int addr,
  881. int devad, u32 regnum, u16 mask,
  882. u16 set)
  883. {
  884. int new, ret;
  885. ret = __mdiobus_c45_read(bus, addr, devad, regnum);
  886. if (ret < 0)
  887. return ret;
  888. new = (ret & ~mask) | set;
  889. if (new == ret)
  890. return 0;
  891. ret = __mdiobus_c45_write(bus, addr, devad, regnum, new);
  892. return ret < 0 ? ret : 1;
  893. }
  894. /**
  895. * mdiobus_read_nested - Nested version of the mdiobus_read function
  896. * @bus: the mii_bus struct
  897. * @addr: the phy address
  898. * @regnum: register number to read
  899. *
  900. * In case of nested MDIO bus access avoid lockdep false positives by
  901. * using mutex_lock_nested().
  902. *
  903. * NOTE: MUST NOT be called from interrupt context,
  904. * because the bus read/write functions may wait for an interrupt
  905. * to conclude the operation.
  906. */
  907. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
  908. {
  909. int retval;
  910. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  911. retval = __mdiobus_read(bus, addr, regnum);
  912. mutex_unlock(&bus->mdio_lock);
  913. return retval;
  914. }
  915. EXPORT_SYMBOL(mdiobus_read_nested);
  916. /**
  917. * mdiobus_read - Convenience function for reading a given MII mgmt register
  918. * @bus: the mii_bus struct
  919. * @addr: the phy address
  920. * @regnum: register number to read
  921. *
  922. * NOTE: MUST NOT be called from interrupt context,
  923. * because the bus read/write functions may wait for an interrupt
  924. * to conclude the operation.
  925. */
  926. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  927. {
  928. int retval;
  929. mutex_lock(&bus->mdio_lock);
  930. retval = __mdiobus_read(bus, addr, regnum);
  931. mutex_unlock(&bus->mdio_lock);
  932. return retval;
  933. }
  934. EXPORT_SYMBOL(mdiobus_read);
  935. /**
  936. * mdiobus_c45_read - Convenience function for reading a given MII mgmt register
  937. * @bus: the mii_bus struct
  938. * @addr: the phy address
  939. * @devad: device address to read
  940. * @regnum: register number to read
  941. *
  942. * NOTE: MUST NOT be called from interrupt context,
  943. * because the bus read/write functions may wait for an interrupt
  944. * to conclude the operation.
  945. */
  946. int mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum)
  947. {
  948. int retval;
  949. mutex_lock(&bus->mdio_lock);
  950. retval = __mdiobus_c45_read(bus, addr, devad, regnum);
  951. mutex_unlock(&bus->mdio_lock);
  952. return retval;
  953. }
  954. EXPORT_SYMBOL(mdiobus_c45_read);
  955. /**
  956. * mdiobus_c45_read_nested - Nested version of the mdiobus_c45_read function
  957. * @bus: the mii_bus struct
  958. * @addr: the phy address
  959. * @devad: device address to read
  960. * @regnum: register number to read
  961. *
  962. * In case of nested MDIO bus access avoid lockdep false positives by
  963. * using mutex_lock_nested().
  964. *
  965. * NOTE: MUST NOT be called from interrupt context,
  966. * because the bus read/write functions may wait for an interrupt
  967. * to conclude the operation.
  968. */
  969. int mdiobus_c45_read_nested(struct mii_bus *bus, int addr, int devad,
  970. u32 regnum)
  971. {
  972. int retval;
  973. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  974. retval = __mdiobus_c45_read(bus, addr, devad, regnum);
  975. mutex_unlock(&bus->mdio_lock);
  976. return retval;
  977. }
  978. EXPORT_SYMBOL(mdiobus_c45_read_nested);
  979. /**
  980. * mdiobus_write_nested - Nested version of the mdiobus_write function
  981. * @bus: the mii_bus struct
  982. * @addr: the phy address
  983. * @regnum: register number to write
  984. * @val: value to write to @regnum
  985. *
  986. * In case of nested MDIO bus access avoid lockdep false positives by
  987. * using mutex_lock_nested().
  988. *
  989. * NOTE: MUST NOT be called from interrupt context,
  990. * because the bus read/write functions may wait for an interrupt
  991. * to conclude the operation.
  992. */
  993. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  994. {
  995. int err;
  996. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  997. err = __mdiobus_write(bus, addr, regnum, val);
  998. mutex_unlock(&bus->mdio_lock);
  999. return err;
  1000. }
  1001. EXPORT_SYMBOL(mdiobus_write_nested);
  1002. /**
  1003. * mdiobus_write - Convenience function for writing a given MII mgmt register
  1004. * @bus: the mii_bus struct
  1005. * @addr: the phy address
  1006. * @regnum: register number to write
  1007. * @val: value to write to @regnum
  1008. *
  1009. * NOTE: MUST NOT be called from interrupt context,
  1010. * because the bus read/write functions may wait for an interrupt
  1011. * to conclude the operation.
  1012. */
  1013. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  1014. {
  1015. int err;
  1016. mutex_lock(&bus->mdio_lock);
  1017. err = __mdiobus_write(bus, addr, regnum, val);
  1018. mutex_unlock(&bus->mdio_lock);
  1019. return err;
  1020. }
  1021. EXPORT_SYMBOL(mdiobus_write);
  1022. /**
  1023. * mdiobus_c45_write - Convenience function for writing a given MII mgmt register
  1024. * @bus: the mii_bus struct
  1025. * @addr: the phy address
  1026. * @devad: device address to read
  1027. * @regnum: register number to write
  1028. * @val: value to write to @regnum
  1029. *
  1030. * NOTE: MUST NOT be called from interrupt context,
  1031. * because the bus read/write functions may wait for an interrupt
  1032. * to conclude the operation.
  1033. */
  1034. int mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum,
  1035. u16 val)
  1036. {
  1037. int err;
  1038. mutex_lock(&bus->mdio_lock);
  1039. err = __mdiobus_c45_write(bus, addr, devad, regnum, val);
  1040. mutex_unlock(&bus->mdio_lock);
  1041. return err;
  1042. }
  1043. EXPORT_SYMBOL(mdiobus_c45_write);
  1044. /**
  1045. * mdiobus_c45_write_nested - Nested version of the mdiobus_c45_write function
  1046. * @bus: the mii_bus struct
  1047. * @addr: the phy address
  1048. * @devad: device address to read
  1049. * @regnum: register number to write
  1050. * @val: value to write to @regnum
  1051. *
  1052. * In case of nested MDIO bus access avoid lockdep false positives by
  1053. * using mutex_lock_nested().
  1054. *
  1055. * NOTE: MUST NOT be called from interrupt context,
  1056. * because the bus read/write functions may wait for an interrupt
  1057. * to conclude the operation.
  1058. */
  1059. int mdiobus_c45_write_nested(struct mii_bus *bus, int addr, int devad,
  1060. u32 regnum, u16 val)
  1061. {
  1062. int err;
  1063. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  1064. err = __mdiobus_c45_write(bus, addr, devad, regnum, val);
  1065. mutex_unlock(&bus->mdio_lock);
  1066. return err;
  1067. }
  1068. EXPORT_SYMBOL(mdiobus_c45_write_nested);
  1069. /*
  1070. * __mdiobus_modify - Convenience function for modifying a given mdio device
  1071. * register
  1072. * @bus: the mii_bus struct
  1073. * @addr: the phy address
  1074. * @regnum: register number to write
  1075. * @mask: bit mask of bits to clear
  1076. * @set: bit mask of bits to set
  1077. */
  1078. int __mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
  1079. u16 set)
  1080. {
  1081. int err;
  1082. err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
  1083. return err < 0 ? err : 0;
  1084. }
  1085. EXPORT_SYMBOL_GPL(__mdiobus_modify);
  1086. /**
  1087. * mdiobus_modify - Convenience function for modifying a given mdio device
  1088. * register
  1089. * @bus: the mii_bus struct
  1090. * @addr: the phy address
  1091. * @regnum: register number to write
  1092. * @mask: bit mask of bits to clear
  1093. * @set: bit mask of bits to set
  1094. */
  1095. int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
  1096. {
  1097. int err;
  1098. mutex_lock(&bus->mdio_lock);
  1099. err = __mdiobus_modify(bus, addr, regnum, mask, set);
  1100. mutex_unlock(&bus->mdio_lock);
  1101. return err;
  1102. }
  1103. EXPORT_SYMBOL_GPL(mdiobus_modify);
  1104. /**
  1105. * mdiobus_c45_modify - Convenience function for modifying a given mdio device
  1106. * register
  1107. * @bus: the mii_bus struct
  1108. * @addr: the phy address
  1109. * @devad: device address to read
  1110. * @regnum: register number to write
  1111. * @mask: bit mask of bits to clear
  1112. * @set: bit mask of bits to set
  1113. */
  1114. int mdiobus_c45_modify(struct mii_bus *bus, int addr, int devad, u32 regnum,
  1115. u16 mask, u16 set)
  1116. {
  1117. int err;
  1118. mutex_lock(&bus->mdio_lock);
  1119. err = __mdiobus_c45_modify_changed(bus, addr, devad, regnum,
  1120. mask, set);
  1121. mutex_unlock(&bus->mdio_lock);
  1122. return err < 0 ? err : 0;
  1123. }
  1124. EXPORT_SYMBOL_GPL(mdiobus_c45_modify);
  1125. /**
  1126. * mdiobus_modify_changed - Convenience function for modifying a given mdio
  1127. * device register and returning if it changed
  1128. * @bus: the mii_bus struct
  1129. * @addr: the phy address
  1130. * @regnum: register number to write
  1131. * @mask: bit mask of bits to clear
  1132. * @set: bit mask of bits to set
  1133. */
  1134. int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
  1135. u16 mask, u16 set)
  1136. {
  1137. int err;
  1138. mutex_lock(&bus->mdio_lock);
  1139. err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
  1140. mutex_unlock(&bus->mdio_lock);
  1141. return err;
  1142. }
  1143. EXPORT_SYMBOL_GPL(mdiobus_modify_changed);
  1144. /**
  1145. * mdiobus_c45_modify_changed - Convenience function for modifying a given mdio
  1146. * device register and returning if it changed
  1147. * @bus: the mii_bus struct
  1148. * @addr: the phy address
  1149. * @devad: device address to read
  1150. * @regnum: register number to write
  1151. * @mask: bit mask of bits to clear
  1152. * @set: bit mask of bits to set
  1153. */
  1154. int mdiobus_c45_modify_changed(struct mii_bus *bus, int addr, int devad,
  1155. u32 regnum, u16 mask, u16 set)
  1156. {
  1157. int err;
  1158. mutex_lock(&bus->mdio_lock);
  1159. err = __mdiobus_c45_modify_changed(bus, addr, devad, regnum, mask, set);
  1160. mutex_unlock(&bus->mdio_lock);
  1161. return err;
  1162. }
  1163. EXPORT_SYMBOL_GPL(mdiobus_c45_modify_changed);
  1164. /**
  1165. * mdio_bus_match - determine if given MDIO driver supports the given
  1166. * MDIO device
  1167. * @dev: target MDIO device
  1168. * @drv: given MDIO driver
  1169. *
  1170. * Description: Given a MDIO device, and a MDIO driver, return 1 if
  1171. * the driver supports the device. Otherwise, return 0. This may
  1172. * require calling the devices own match function, since different classes
  1173. * of MDIO devices have different match criteria.
  1174. */
  1175. static int mdio_bus_match(struct device *dev, const struct device_driver *drv)
  1176. {
  1177. const struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  1178. struct mdio_device *mdio = to_mdio_device(dev);
  1179. /* Both the driver and device must type-match */
  1180. if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) !=
  1181. !(mdio->flags & MDIO_DEVICE_FLAG_PHY))
  1182. return 0;
  1183. if (of_driver_match_device(dev, drv))
  1184. return 1;
  1185. if (mdio->bus_match)
  1186. return mdio->bus_match(dev, drv);
  1187. return 0;
  1188. }
  1189. static int mdio_uevent(const struct device *dev, struct kobj_uevent_env *env)
  1190. {
  1191. int rc;
  1192. /* Some devices have extra OF data and an OF-style MODALIAS */
  1193. rc = of_device_uevent_modalias(dev, env);
  1194. if (rc != -ENODEV)
  1195. return rc;
  1196. return 0;
  1197. }
  1198. static struct attribute *mdio_bus_device_statistics_attrs[] = {
  1199. &dev_attr_mdio_bus_device_transfers.attr.attr,
  1200. &dev_attr_mdio_bus_device_errors.attr.attr,
  1201. &dev_attr_mdio_bus_device_writes.attr.attr,
  1202. &dev_attr_mdio_bus_device_reads.attr.attr,
  1203. NULL,
  1204. };
  1205. static const struct attribute_group mdio_bus_device_statistics_group = {
  1206. .name = "statistics",
  1207. .attrs = mdio_bus_device_statistics_attrs,
  1208. };
  1209. static const struct attribute_group *mdio_bus_dev_groups[] = {
  1210. &mdio_bus_device_statistics_group,
  1211. NULL,
  1212. };
  1213. const struct bus_type mdio_bus_type = {
  1214. .name = "mdio_bus",
  1215. .dev_groups = mdio_bus_dev_groups,
  1216. .match = mdio_bus_match,
  1217. .uevent = mdio_uevent,
  1218. };
  1219. EXPORT_SYMBOL(mdio_bus_type);
  1220. int __init mdio_bus_init(void)
  1221. {
  1222. int ret;
  1223. ret = class_register(&mdio_bus_class);
  1224. if (!ret) {
  1225. ret = bus_register(&mdio_bus_type);
  1226. if (ret)
  1227. class_unregister(&mdio_bus_class);
  1228. }
  1229. return ret;
  1230. }
  1231. #if IS_ENABLED(CONFIG_PHYLIB)
  1232. void mdio_bus_exit(void)
  1233. {
  1234. class_unregister(&mdio_bus_class);
  1235. bus_unregister(&mdio_bus_type);
  1236. }
  1237. EXPORT_SYMBOL_GPL(mdio_bus_exit);
  1238. #else
  1239. module_init(mdio_bus_init);
  1240. /* no module_exit, intentional */
  1241. MODULE_LICENSE("GPL");
  1242. MODULE_DESCRIPTION("MDIO bus/device layer");
  1243. #endif