mdio_bus.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.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_gpio.h>
  26. #include <linux/of_mdio.h>
  27. #include <linux/phy.h>
  28. #include <linux/reset.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/string.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/unistd.h>
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/mdio.h>
  37. #include "mdio-boardinfo.h"
  38. static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
  39. {
  40. /* Deassert the optional reset signal */
  41. mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
  42. "reset", GPIOD_OUT_LOW);
  43. if (IS_ERR(mdiodev->reset_gpio))
  44. return PTR_ERR(mdiodev->reset_gpio);
  45. if (mdiodev->reset_gpio)
  46. gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
  47. return 0;
  48. }
  49. static int mdiobus_register_reset(struct mdio_device *mdiodev)
  50. {
  51. struct reset_control *reset;
  52. reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
  53. if (IS_ERR(reset))
  54. return PTR_ERR(reset);
  55. mdiodev->reset_ctrl = reset;
  56. return 0;
  57. }
  58. int mdiobus_register_device(struct mdio_device *mdiodev)
  59. {
  60. int err;
  61. if (mdiodev->bus->mdio_map[mdiodev->addr])
  62. return -EBUSY;
  63. if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
  64. err = mdiobus_register_gpiod(mdiodev);
  65. if (err)
  66. return err;
  67. err = mdiobus_register_reset(mdiodev);
  68. if (err)
  69. return err;
  70. /* Assert the reset signal */
  71. mdio_device_reset(mdiodev, 1);
  72. }
  73. mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL(mdiobus_register_device);
  77. int mdiobus_unregister_device(struct mdio_device *mdiodev)
  78. {
  79. if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
  80. return -EINVAL;
  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. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
  87. {
  88. struct mdio_device *mdiodev;
  89. if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
  90. return NULL;
  91. mdiodev = bus->mdio_map[addr];
  92. if (!mdiodev)
  93. return NULL;
  94. if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
  95. return NULL;
  96. return container_of(mdiodev, struct phy_device, mdio);
  97. }
  98. EXPORT_SYMBOL(mdiobus_get_phy);
  99. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
  100. {
  101. return bus->mdio_map[addr];
  102. }
  103. EXPORT_SYMBOL(mdiobus_is_registered_device);
  104. /**
  105. * mdiobus_alloc_size - allocate a mii_bus structure
  106. * @size: extra amount of memory to allocate for private storage.
  107. * If non-zero, then bus->priv is points to that memory.
  108. *
  109. * Description: called by a bus driver to allocate an mii_bus
  110. * structure to fill in.
  111. */
  112. struct mii_bus *mdiobus_alloc_size(size_t size)
  113. {
  114. struct mii_bus *bus;
  115. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  116. size_t alloc_size;
  117. int i;
  118. /* If we alloc extra space, it should be aligned */
  119. if (size)
  120. alloc_size = aligned_size + size;
  121. else
  122. alloc_size = sizeof(*bus);
  123. bus = kzalloc(alloc_size, GFP_KERNEL);
  124. if (!bus)
  125. return NULL;
  126. bus->state = MDIOBUS_ALLOCATED;
  127. if (size)
  128. bus->priv = (void *)bus + aligned_size;
  129. /* Initialise the interrupts to polling and 64-bit seqcounts */
  130. for (i = 0; i < PHY_MAX_ADDR; i++) {
  131. bus->irq[i] = PHY_POLL;
  132. u64_stats_init(&bus->stats[i].syncp);
  133. }
  134. return bus;
  135. }
  136. EXPORT_SYMBOL(mdiobus_alloc_size);
  137. /**
  138. * mdiobus_release - mii_bus device release callback
  139. * @d: the target struct device that contains the mii_bus
  140. *
  141. * Description: called when the last reference to an mii_bus is
  142. * dropped, to free the underlying memory.
  143. */
  144. static void mdiobus_release(struct device *d)
  145. {
  146. struct mii_bus *bus = to_mii_bus(d);
  147. WARN(bus->state != MDIOBUS_RELEASED &&
  148. /* for compatibility with error handling in drivers */
  149. bus->state != MDIOBUS_ALLOCATED,
  150. "%s: not in RELEASED or ALLOCATED state\n",
  151. bus->id);
  152. kfree(bus);
  153. }
  154. struct mdio_bus_stat_attr {
  155. int addr;
  156. unsigned int field_offset;
  157. };
  158. static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
  159. {
  160. const char *p = (const char *)s + offset;
  161. unsigned int start;
  162. u64 val = 0;
  163. do {
  164. start = u64_stats_fetch_begin(&s->syncp);
  165. val = u64_stats_read((const u64_stats_t *)p);
  166. } while (u64_stats_fetch_retry(&s->syncp, start));
  167. return val;
  168. }
  169. static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
  170. {
  171. unsigned int i;
  172. u64 val = 0;
  173. for (i = 0; i < PHY_MAX_ADDR; i++)
  174. val += mdio_bus_get_stat(&bus->stats[i], offset);
  175. return val;
  176. }
  177. static ssize_t mdio_bus_stat_field_show(struct device *dev,
  178. struct device_attribute *attr,
  179. char *buf)
  180. {
  181. struct mii_bus *bus = to_mii_bus(dev);
  182. struct mdio_bus_stat_attr *sattr;
  183. struct dev_ext_attribute *eattr;
  184. u64 val;
  185. eattr = container_of(attr, struct dev_ext_attribute, attr);
  186. sattr = eattr->var;
  187. if (sattr->addr < 0)
  188. val = mdio_bus_get_global_stat(bus, sattr->field_offset);
  189. else
  190. val = mdio_bus_get_stat(&bus->stats[sattr->addr],
  191. sattr->field_offset);
  192. return sysfs_emit(buf, "%llu\n", val);
  193. }
  194. static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
  195. struct device_attribute *attr,
  196. char *buf)
  197. {
  198. struct mdio_device *mdiodev = to_mdio_device(dev);
  199. struct mii_bus *bus = mdiodev->bus;
  200. struct mdio_bus_stat_attr *sattr;
  201. struct dev_ext_attribute *eattr;
  202. int addr = mdiodev->addr;
  203. u64 val;
  204. eattr = container_of(attr, struct dev_ext_attribute, attr);
  205. sattr = eattr->var;
  206. val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
  207. return sysfs_emit(buf, "%llu\n", val);
  208. }
  209. #define MDIO_BUS_STATS_ATTR_DECL(field, file) \
  210. static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \
  211. .attr = { .attr = { .name = file, .mode = 0444 }, \
  212. .show = mdio_bus_stat_field_show, \
  213. }, \
  214. .var = &((struct mdio_bus_stat_attr) { \
  215. -1, offsetof(struct mdio_bus_stats, field) \
  216. }), \
  217. }; \
  218. static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \
  219. .attr = { .attr = { .name = file, .mode = 0444 }, \
  220. .show = mdio_bus_device_stat_field_show, \
  221. }, \
  222. .var = &((struct mdio_bus_stat_attr) { \
  223. -1, offsetof(struct mdio_bus_stats, field) \
  224. }), \
  225. };
  226. #define MDIO_BUS_STATS_ATTR(field) \
  227. MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
  228. MDIO_BUS_STATS_ATTR(transfers);
  229. MDIO_BUS_STATS_ATTR(errors);
  230. MDIO_BUS_STATS_ATTR(writes);
  231. MDIO_BUS_STATS_ATTR(reads);
  232. #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \
  233. static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
  234. .attr = { .attr = { .name = file, .mode = 0444 }, \
  235. .show = mdio_bus_stat_field_show, \
  236. }, \
  237. .var = &((struct mdio_bus_stat_attr) { \
  238. addr, offsetof(struct mdio_bus_stats, field) \
  239. }), \
  240. }
  241. #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \
  242. MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \
  243. __stringify(field) "_" __stringify(addr))
  244. #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \
  245. MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \
  246. MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \
  247. MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \
  248. MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \
  249. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
  250. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
  251. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
  252. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
  253. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
  254. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
  255. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
  256. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
  257. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
  258. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
  259. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
  260. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
  261. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
  262. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
  263. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
  264. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
  265. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
  266. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
  267. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
  268. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
  269. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
  270. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
  271. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
  272. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
  273. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
  274. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
  275. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
  276. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
  277. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
  278. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
  279. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
  280. MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
  281. #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \
  282. &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \
  283. &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \
  284. &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \
  285. &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \
  286. static struct attribute *mdio_bus_statistics_attrs[] = {
  287. &dev_attr_mdio_bus_transfers.attr.attr,
  288. &dev_attr_mdio_bus_errors.attr.attr,
  289. &dev_attr_mdio_bus_writes.attr.attr,
  290. &dev_attr_mdio_bus_reads.attr.attr,
  291. MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
  292. MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
  293. MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
  294. MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
  295. MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
  296. MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
  297. MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
  298. MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
  299. MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
  300. MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
  301. MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
  302. MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
  303. MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
  304. MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
  305. MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
  306. MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
  307. MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
  308. MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
  309. MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
  310. MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
  311. MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
  312. MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
  313. MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
  314. MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
  315. MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
  316. MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
  317. MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
  318. MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
  319. MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
  320. MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
  321. MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
  322. MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
  323. NULL,
  324. };
  325. static const struct attribute_group mdio_bus_statistics_group = {
  326. .name = "statistics",
  327. .attrs = mdio_bus_statistics_attrs,
  328. };
  329. static const struct attribute_group *mdio_bus_groups[] = {
  330. &mdio_bus_statistics_group,
  331. NULL,
  332. };
  333. static struct class mdio_bus_class = {
  334. .name = "mdio_bus",
  335. .dev_release = mdiobus_release,
  336. .dev_groups = mdio_bus_groups,
  337. };
  338. /**
  339. * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
  340. * @mdio_name: The name of a mdiobus.
  341. *
  342. * Returns a reference to the mii_bus, or NULL if none found. The
  343. * embedded struct device will have its reference count incremented,
  344. * and this must be put_deviced'ed once the bus is finished with.
  345. */
  346. struct mii_bus *mdio_find_bus(const char *mdio_name)
  347. {
  348. struct device *d;
  349. d = class_find_device_by_name(&mdio_bus_class, mdio_name);
  350. return d ? to_mii_bus(d) : NULL;
  351. }
  352. EXPORT_SYMBOL(mdio_find_bus);
  353. #if IS_ENABLED(CONFIG_OF_MDIO)
  354. /**
  355. * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
  356. * @mdio_bus_np: Pointer to the mii_bus.
  357. *
  358. * Returns a reference to the mii_bus, or NULL if none found. The
  359. * embedded struct device will have its reference count incremented,
  360. * and this must be put once the bus is finished with.
  361. *
  362. * Because the association of a device_node and mii_bus is made via
  363. * of_mdiobus_register(), the mii_bus cannot be found before it is
  364. * registered with of_mdiobus_register().
  365. *
  366. */
  367. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  368. {
  369. struct device *d;
  370. if (!mdio_bus_np)
  371. return NULL;
  372. d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
  373. return d ? to_mii_bus(d) : NULL;
  374. }
  375. EXPORT_SYMBOL(of_mdio_find_bus);
  376. /* Walk the list of subnodes of a mdio bus and look for a node that
  377. * matches the mdio device's address with its 'reg' property. If
  378. * found, set the of_node pointer for the mdio device. This allows
  379. * auto-probed phy devices to be supplied with information passed in
  380. * via DT.
  381. */
  382. static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
  383. struct mdio_device *mdiodev)
  384. {
  385. struct device *dev = &mdiodev->dev;
  386. struct device_node *child;
  387. if (dev->of_node || !bus->dev.of_node)
  388. return;
  389. for_each_available_child_of_node(bus->dev.of_node, child) {
  390. int addr;
  391. addr = of_mdio_parse_addr(dev, child);
  392. if (addr < 0)
  393. continue;
  394. if (addr == mdiodev->addr) {
  395. device_set_node(dev, of_fwnode_handle(child));
  396. /* The refcount on "child" is passed to the mdio
  397. * device. Do _not_ use of_node_put(child) here.
  398. */
  399. return;
  400. }
  401. }
  402. }
  403. #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
  404. static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
  405. struct mdio_device *mdiodev)
  406. {
  407. }
  408. #endif
  409. /**
  410. * mdiobus_create_device - create a full MDIO device given
  411. * a mdio_board_info structure
  412. * @bus: MDIO bus to create the devices on
  413. * @bi: mdio_board_info structure describing the devices
  414. *
  415. * Returns 0 on success or < 0 on error.
  416. */
  417. static int mdiobus_create_device(struct mii_bus *bus,
  418. struct mdio_board_info *bi)
  419. {
  420. struct mdio_device *mdiodev;
  421. int ret = 0;
  422. mdiodev = mdio_device_create(bus, bi->mdio_addr);
  423. if (IS_ERR(mdiodev))
  424. return -ENODEV;
  425. strncpy(mdiodev->modalias, bi->modalias,
  426. sizeof(mdiodev->modalias));
  427. mdiodev->bus_match = mdio_device_bus_match;
  428. mdiodev->dev.platform_data = (void *)bi->platform_data;
  429. ret = mdio_device_register(mdiodev);
  430. if (ret)
  431. mdio_device_free(mdiodev);
  432. return ret;
  433. }
  434. /**
  435. * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  436. * @bus: target mii_bus
  437. * @owner: module containing bus accessor functions
  438. *
  439. * Description: Called by a bus driver to bring up all the PHYs
  440. * on a given bus, and attach them to the bus. Drivers should use
  441. * mdiobus_register() rather than __mdiobus_register() unless they
  442. * need to pass a specific owner module. MDIO devices which are not
  443. * PHYs will not be brought up by this function. They are expected
  444. * to be explicitly listed in DT and instantiated by of_mdiobus_register().
  445. *
  446. * Returns 0 on success or < 0 on error.
  447. */
  448. int __mdiobus_register(struct mii_bus *bus, struct module *owner)
  449. {
  450. struct mdio_device *mdiodev;
  451. int i, err;
  452. struct gpio_desc *gpiod;
  453. if (NULL == bus || NULL == bus->name ||
  454. NULL == bus->read || NULL == bus->write)
  455. return -EINVAL;
  456. if (bus->parent && bus->parent->of_node)
  457. bus->parent->of_node->fwnode.flags |=
  458. FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
  459. WARN(bus->state != MDIOBUS_ALLOCATED &&
  460. bus->state != MDIOBUS_UNREGISTERED,
  461. "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
  462. bus->owner = owner;
  463. bus->dev.parent = bus->parent;
  464. bus->dev.class = &mdio_bus_class;
  465. bus->dev.groups = NULL;
  466. dev_set_name(&bus->dev, "%s", bus->id);
  467. /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
  468. * the device in mdiobus_free()
  469. *
  470. * State will be updated later in this function in case of success
  471. */
  472. bus->state = MDIOBUS_UNREGISTERED;
  473. err = device_register(&bus->dev);
  474. if (err) {
  475. pr_err("mii_bus %s failed to register\n", bus->id);
  476. return -EINVAL;
  477. }
  478. mutex_init(&bus->mdio_lock);
  479. mutex_init(&bus->shared_lock);
  480. /* assert bus level PHY GPIO reset */
  481. gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
  482. if (IS_ERR(gpiod)) {
  483. err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
  484. "mii_bus %s couldn't get reset GPIO\n",
  485. bus->id);
  486. device_del(&bus->dev);
  487. return err;
  488. } else if (gpiod) {
  489. bus->reset_gpiod = gpiod;
  490. fsleep(bus->reset_delay_us);
  491. gpiod_set_value_cansleep(gpiod, 0);
  492. if (bus->reset_post_delay_us > 0)
  493. fsleep(bus->reset_post_delay_us);
  494. }
  495. if (bus->reset) {
  496. err = bus->reset(bus);
  497. if (err)
  498. goto error_reset_gpiod;
  499. }
  500. for (i = 0; i < PHY_MAX_ADDR; i++) {
  501. if ((bus->phy_mask & BIT(i)) == 0) {
  502. struct phy_device *phydev;
  503. phydev = mdiobus_scan(bus, i);
  504. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
  505. err = PTR_ERR(phydev);
  506. goto error;
  507. }
  508. }
  509. }
  510. mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
  511. bus->state = MDIOBUS_REGISTERED;
  512. dev_dbg(&bus->dev, "probed\n");
  513. return 0;
  514. error:
  515. while (--i >= 0) {
  516. mdiodev = bus->mdio_map[i];
  517. if (!mdiodev)
  518. continue;
  519. mdiodev->device_remove(mdiodev);
  520. mdiodev->device_free(mdiodev);
  521. }
  522. error_reset_gpiod:
  523. /* Put PHYs in RESET to save power */
  524. if (bus->reset_gpiod)
  525. gpiod_set_value_cansleep(bus->reset_gpiod, 1);
  526. device_del(&bus->dev);
  527. return err;
  528. }
  529. EXPORT_SYMBOL(__mdiobus_register);
  530. void mdiobus_unregister(struct mii_bus *bus)
  531. {
  532. struct mdio_device *mdiodev;
  533. int i;
  534. if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
  535. return;
  536. bus->state = MDIOBUS_UNREGISTERED;
  537. for (i = 0; i < PHY_MAX_ADDR; i++) {
  538. mdiodev = bus->mdio_map[i];
  539. if (!mdiodev)
  540. continue;
  541. if (mdiodev->reset_gpio)
  542. gpiod_put(mdiodev->reset_gpio);
  543. mdiodev->device_remove(mdiodev);
  544. mdiodev->device_free(mdiodev);
  545. }
  546. /* Put PHYs in RESET to save power */
  547. if (bus->reset_gpiod)
  548. gpiod_set_value_cansleep(bus->reset_gpiod, 1);
  549. device_del(&bus->dev);
  550. }
  551. EXPORT_SYMBOL(mdiobus_unregister);
  552. /**
  553. * mdiobus_free - free a struct mii_bus
  554. * @bus: mii_bus to free
  555. *
  556. * This function releases the reference to the underlying device
  557. * object in the mii_bus. If this is the last reference, the mii_bus
  558. * will be freed.
  559. */
  560. void mdiobus_free(struct mii_bus *bus)
  561. {
  562. /* For compatibility with error handling in drivers. */
  563. if (bus->state == MDIOBUS_ALLOCATED) {
  564. kfree(bus);
  565. return;
  566. }
  567. WARN(bus->state != MDIOBUS_UNREGISTERED,
  568. "%s: not in UNREGISTERED state\n", bus->id);
  569. bus->state = MDIOBUS_RELEASED;
  570. put_device(&bus->dev);
  571. }
  572. EXPORT_SYMBOL(mdiobus_free);
  573. /**
  574. * mdiobus_scan - scan a bus for MDIO devices.
  575. * @bus: mii_bus to scan
  576. * @addr: address on bus to scan
  577. *
  578. * This function scans the MDIO bus, looking for devices which can be
  579. * identified using a vendor/product ID in registers 2 and 3. Not all
  580. * MDIO devices have such registers, but PHY devices typically
  581. * do. Hence this function assumes anything found is a PHY, or can be
  582. * treated as a PHY. Other MDIO devices, such as switches, will
  583. * probably not be found during the scan.
  584. */
  585. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  586. {
  587. struct phy_device *phydev = ERR_PTR(-ENODEV);
  588. int err;
  589. switch (bus->probe_capabilities) {
  590. case MDIOBUS_NO_CAP:
  591. case MDIOBUS_C22:
  592. phydev = get_phy_device(bus, addr, false);
  593. break;
  594. case MDIOBUS_C45:
  595. phydev = get_phy_device(bus, addr, true);
  596. break;
  597. case MDIOBUS_C22_C45:
  598. phydev = get_phy_device(bus, addr, false);
  599. if (IS_ERR(phydev))
  600. phydev = get_phy_device(bus, addr, true);
  601. break;
  602. }
  603. if (IS_ERR(phydev))
  604. return phydev;
  605. /*
  606. * For DT, see if the auto-probed phy has a correspoding child
  607. * in the bus node, and set the of_node pointer in this case.
  608. */
  609. of_mdiobus_link_mdiodev(bus, &phydev->mdio);
  610. err = phy_device_register(phydev);
  611. if (err) {
  612. phy_device_free(phydev);
  613. return ERR_PTR(-ENODEV);
  614. }
  615. return phydev;
  616. }
  617. EXPORT_SYMBOL(mdiobus_scan);
  618. static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
  619. {
  620. preempt_disable();
  621. u64_stats_update_begin(&stats->syncp);
  622. u64_stats_inc(&stats->transfers);
  623. if (ret < 0) {
  624. u64_stats_inc(&stats->errors);
  625. goto out;
  626. }
  627. if (op)
  628. u64_stats_inc(&stats->reads);
  629. else
  630. u64_stats_inc(&stats->writes);
  631. out:
  632. u64_stats_update_end(&stats->syncp);
  633. preempt_enable();
  634. }
  635. /**
  636. * __mdiobus_read - Unlocked version of the mdiobus_read function
  637. * @bus: the mii_bus struct
  638. * @addr: the phy address
  639. * @regnum: register number to read
  640. *
  641. * Read a MDIO bus register. Caller must hold the mdio bus lock.
  642. *
  643. * NOTE: MUST NOT be called from interrupt context.
  644. */
  645. int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  646. {
  647. int retval;
  648. lockdep_assert_held_once(&bus->mdio_lock);
  649. retval = bus->read(bus, addr, regnum);
  650. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  651. mdiobus_stats_acct(&bus->stats[addr], true, retval);
  652. return retval;
  653. }
  654. EXPORT_SYMBOL(__mdiobus_read);
  655. /**
  656. * __mdiobus_write - Unlocked version of the mdiobus_write function
  657. * @bus: the mii_bus struct
  658. * @addr: the phy address
  659. * @regnum: register number to write
  660. * @val: value to write to @regnum
  661. *
  662. * Write a MDIO bus register. Caller must hold the mdio bus lock.
  663. *
  664. * NOTE: MUST NOT be called from interrupt context.
  665. */
  666. int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  667. {
  668. int err;
  669. lockdep_assert_held_once(&bus->mdio_lock);
  670. err = bus->write(bus, addr, regnum, val);
  671. trace_mdio_access(bus, 0, addr, regnum, val, err);
  672. mdiobus_stats_acct(&bus->stats[addr], false, err);
  673. return err;
  674. }
  675. EXPORT_SYMBOL(__mdiobus_write);
  676. /**
  677. * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
  678. * @bus: the mii_bus struct
  679. * @addr: the phy address
  680. * @regnum: register number to modify
  681. * @mask: bit mask of bits to clear
  682. * @set: bit mask of bits to set
  683. *
  684. * Read, modify, and if any change, write the register value back to the
  685. * device. Any error returns a negative number.
  686. *
  687. * NOTE: MUST NOT be called from interrupt context.
  688. */
  689. int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
  690. u16 mask, u16 set)
  691. {
  692. int new, ret;
  693. ret = __mdiobus_read(bus, addr, regnum);
  694. if (ret < 0)
  695. return ret;
  696. new = (ret & ~mask) | set;
  697. if (new == ret)
  698. return 0;
  699. ret = __mdiobus_write(bus, addr, regnum, new);
  700. return ret < 0 ? ret : 1;
  701. }
  702. EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
  703. /**
  704. * mdiobus_read_nested - Nested version of the mdiobus_read function
  705. * @bus: the mii_bus struct
  706. * @addr: the phy address
  707. * @regnum: register number to read
  708. *
  709. * In case of nested MDIO bus access avoid lockdep false positives by
  710. * using mutex_lock_nested().
  711. *
  712. * NOTE: MUST NOT be called from interrupt context,
  713. * because the bus read/write functions may wait for an interrupt
  714. * to conclude the operation.
  715. */
  716. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
  717. {
  718. int retval;
  719. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  720. retval = __mdiobus_read(bus, addr, regnum);
  721. mutex_unlock(&bus->mdio_lock);
  722. return retval;
  723. }
  724. EXPORT_SYMBOL(mdiobus_read_nested);
  725. /**
  726. * mdiobus_read - Convenience function for reading a given MII mgmt register
  727. * @bus: the mii_bus struct
  728. * @addr: the phy address
  729. * @regnum: register number to read
  730. *
  731. * NOTE: MUST NOT be called from interrupt context,
  732. * because the bus read/write functions may wait for an interrupt
  733. * to conclude the operation.
  734. */
  735. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  736. {
  737. int retval;
  738. mutex_lock(&bus->mdio_lock);
  739. retval = __mdiobus_read(bus, addr, regnum);
  740. mutex_unlock(&bus->mdio_lock);
  741. return retval;
  742. }
  743. EXPORT_SYMBOL(mdiobus_read);
  744. /**
  745. * mdiobus_write_nested - Nested version of the mdiobus_write function
  746. * @bus: the mii_bus struct
  747. * @addr: the phy address
  748. * @regnum: register number to write
  749. * @val: value to write to @regnum
  750. *
  751. * In case of nested MDIO bus access avoid lockdep false positives by
  752. * using mutex_lock_nested().
  753. *
  754. * NOTE: MUST NOT be called from interrupt context,
  755. * because the bus read/write functions may wait for an interrupt
  756. * to conclude the operation.
  757. */
  758. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  759. {
  760. int err;
  761. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  762. err = __mdiobus_write(bus, addr, regnum, val);
  763. mutex_unlock(&bus->mdio_lock);
  764. return err;
  765. }
  766. EXPORT_SYMBOL(mdiobus_write_nested);
  767. /**
  768. * mdiobus_write - Convenience function for writing a given MII mgmt register
  769. * @bus: the mii_bus struct
  770. * @addr: the phy address
  771. * @regnum: register number to write
  772. * @val: value to write to @regnum
  773. *
  774. * NOTE: MUST NOT be called from interrupt context,
  775. * because the bus read/write functions may wait for an interrupt
  776. * to conclude the operation.
  777. */
  778. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  779. {
  780. int err;
  781. mutex_lock(&bus->mdio_lock);
  782. err = __mdiobus_write(bus, addr, regnum, val);
  783. mutex_unlock(&bus->mdio_lock);
  784. return err;
  785. }
  786. EXPORT_SYMBOL(mdiobus_write);
  787. /**
  788. * mdiobus_modify - Convenience function for modifying a given mdio device
  789. * register
  790. * @bus: the mii_bus struct
  791. * @addr: the phy address
  792. * @regnum: register number to write
  793. * @mask: bit mask of bits to clear
  794. * @set: bit mask of bits to set
  795. */
  796. int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
  797. {
  798. int err;
  799. mutex_lock(&bus->mdio_lock);
  800. err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
  801. mutex_unlock(&bus->mdio_lock);
  802. return err < 0 ? err : 0;
  803. }
  804. EXPORT_SYMBOL_GPL(mdiobus_modify);
  805. /**
  806. * mdiobus_modify_changed - Convenience function for modifying a given mdio
  807. * device register and returning if it changed
  808. * @bus: the mii_bus struct
  809. * @addr: the phy address
  810. * @regnum: register number to write
  811. * @mask: bit mask of bits to clear
  812. * @set: bit mask of bits to set
  813. */
  814. int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
  815. u16 mask, u16 set)
  816. {
  817. int err;
  818. mutex_lock(&bus->mdio_lock);
  819. err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
  820. mutex_unlock(&bus->mdio_lock);
  821. return err;
  822. }
  823. EXPORT_SYMBOL_GPL(mdiobus_modify_changed);
  824. /**
  825. * mdio_bus_match - determine if given MDIO driver supports the given
  826. * MDIO device
  827. * @dev: target MDIO device
  828. * @drv: given MDIO driver
  829. *
  830. * Description: Given a MDIO device, and a MDIO driver, return 1 if
  831. * the driver supports the device. Otherwise, return 0. This may
  832. * require calling the devices own match function, since different classes
  833. * of MDIO devices have different match criteria.
  834. */
  835. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  836. {
  837. struct mdio_driver *mdiodrv = to_mdio_driver(drv);
  838. struct mdio_device *mdio = to_mdio_device(dev);
  839. /* Both the driver and device must type-match */
  840. if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) !=
  841. !(mdio->flags & MDIO_DEVICE_FLAG_PHY))
  842. return 0;
  843. if (of_driver_match_device(dev, drv))
  844. return 1;
  845. if (mdio->bus_match)
  846. return mdio->bus_match(dev, drv);
  847. return 0;
  848. }
  849. static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
  850. {
  851. int rc;
  852. /* Some devices have extra OF data and an OF-style MODALIAS */
  853. rc = of_device_uevent_modalias(dev, env);
  854. if (rc != -ENODEV)
  855. return rc;
  856. return 0;
  857. }
  858. static struct attribute *mdio_bus_device_statistics_attrs[] = {
  859. &dev_attr_mdio_bus_device_transfers.attr.attr,
  860. &dev_attr_mdio_bus_device_errors.attr.attr,
  861. &dev_attr_mdio_bus_device_writes.attr.attr,
  862. &dev_attr_mdio_bus_device_reads.attr.attr,
  863. NULL,
  864. };
  865. static const struct attribute_group mdio_bus_device_statistics_group = {
  866. .name = "statistics",
  867. .attrs = mdio_bus_device_statistics_attrs,
  868. };
  869. static const struct attribute_group *mdio_bus_dev_groups[] = {
  870. &mdio_bus_device_statistics_group,
  871. NULL,
  872. };
  873. struct bus_type mdio_bus_type = {
  874. .name = "mdio_bus",
  875. .dev_groups = mdio_bus_dev_groups,
  876. .match = mdio_bus_match,
  877. .uevent = mdio_uevent,
  878. };
  879. EXPORT_SYMBOL(mdio_bus_type);
  880. int __init mdio_bus_init(void)
  881. {
  882. int ret;
  883. ret = class_register(&mdio_bus_class);
  884. if (!ret) {
  885. ret = bus_register(&mdio_bus_type);
  886. if (ret)
  887. class_unregister(&mdio_bus_class);
  888. }
  889. return ret;
  890. }
  891. #if IS_ENABLED(CONFIG_PHYLIB)
  892. void mdio_bus_exit(void)
  893. {
  894. class_unregister(&mdio_bus_class);
  895. bus_unregister(&mdio_bus_type);
  896. }
  897. EXPORT_SYMBOL_GPL(mdio_bus_exit);
  898. #else
  899. module_init(mdio_bus_init);
  900. /* no module_exit, intentional */
  901. MODULE_LICENSE("GPL");
  902. MODULE_DESCRIPTION("MDIO bus/device layer");
  903. #endif