0021-phy-add-mdio-boardinfo.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. From b8d5957374dc0e6ec8687c6e1b154ea066d27a5b Mon Sep 17 00:00:00 2001
  2. From: Phil Sutter <phil@nwl.cc>
  3. Date: Wed, 14 May 2014 02:44:43 +0200
  4. Subject: [PATCH] phy: add mdio boardinfo
  5. ---
  6. drivers/net/Makefile | 2 +-
  7. drivers/net/phy/Kconfig | 4 +++
  8. drivers/net/phy/Makefile | 2 ++
  9. drivers/net/phy/mdio-boardinfo.c | 58 ++++++++++++++++++++++++++++++++++++++++
  10. drivers/net/phy/mdio-boardinfo.h | 22 +++++++++++++++
  11. drivers/net/phy/mdio_bus.c | 20 ++++++++++++++
  12. include/linux/phy.h | 18 +++++++++++++
  13. 7 files changed, 125 insertions(+), 1 deletion(-)
  14. create mode 100644 drivers/net/phy/mdio-boardinfo.c
  15. create mode 100644 drivers/net/phy/mdio-boardinfo.h
  16. diff --git a/drivers/net/Makefile b/drivers/net/Makefile
  17. index 3fef8a8..70b736b 100644
  18. --- a/drivers/net/Makefile
  19. +++ b/drivers/net/Makefile
  20. @@ -15,7 +15,7 @@ obj-$(CONFIG_MII) += mii.o
  21. obj-$(CONFIG_MDIO) += mdio.o
  22. obj-$(CONFIG_NET) += Space.o loopback.o
  23. obj-$(CONFIG_NETCONSOLE) += netconsole.o
  24. -obj-$(CONFIG_PHYLIB) += phy/
  25. +obj-y += phy/
  26. obj-$(CONFIG_RIONET) += rionet.o
  27. obj-$(CONFIG_NET_TEAM) += team/
  28. obj-$(CONFIG_TUN) += tun.o
  29. diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
  30. index 0414889..97ca8ec 100644
  31. --- a/drivers/net/phy/Kconfig
  32. +++ b/drivers/net/phy/Kconfig
  33. @@ -12,6 +12,10 @@ menuconfig PHYLIB
  34. if PHYLIB
  35. +config MDIO_BOARDINFO
  36. + bool
  37. + default y
  38. +
  39. config SWCONFIG
  40. tristate "Switch configuration API"
  41. ---help---
  42. diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
  43. index 3c76ff8..0c990a4 100644
  44. --- a/drivers/net/phy/Makefile
  45. +++ b/drivers/net/phy/Makefile
  46. @@ -2,6 +2,8 @@
  47. libphy-objs := phy.o phy_device.o mdio_bus.o
  48. +obj-$(CONFIG_MDIO_BOARDINFO) += mdio-boardinfo.o
  49. +
  50. obj-$(CONFIG_PHYLIB) += libphy.o
  51. obj-$(CONFIG_SWCONFIG) += swconfig.o
  52. obj-$(CONFIG_MARVELL_PHY) += marvell.o
  53. diff --git a/drivers/net/phy/mdio-boardinfo.c b/drivers/net/phy/mdio-boardinfo.c
  54. new file mode 100644
  55. index 0000000..9b8aaed
  56. --- /dev/null
  57. +++ b/drivers/net/phy/mdio-boardinfo.c
  58. @@ -0,0 +1,58 @@
  59. +/*
  60. + * mdio-boardinfo.c - collect pre-declarations of PHY devices
  61. + *
  62. + * This program is free software; you can redistribute it and/or modify it
  63. + * under the terms of the GNU General Public License as published by the
  64. + * Free Software Foundation; either version 2 of the License, or (at your
  65. + * option) any later version.
  66. + *
  67. + */
  68. +
  69. +#include <linux/kernel.h>
  70. +#include <linux/phy.h>
  71. +#include <linux/slab.h>
  72. +#include <linux/export.h>
  73. +#include <linux/mutex.h>
  74. +#include <linux/phy.h>
  75. +
  76. +#include "mdio-boardinfo.h"
  77. +
  78. +/*
  79. + * These symbols are exported ONLY FOR the mdio_bus component.
  80. + * No other users will be supported.
  81. + */
  82. +
  83. +LIST_HEAD(__mdio_board_list);
  84. +EXPORT_SYMBOL_GPL(__mdio_board_list);
  85. +
  86. +DEFINE_MUTEX(__mdio_board_lock);
  87. +EXPORT_SYMBOL_GPL(__mdio_board_lock);
  88. +
  89. +/**
  90. + * mdio_register_board_info - register PHY devices for a given board
  91. + * @info: array of chip descriptors
  92. + * @n: how many descriptors are provided
  93. + * Context: can sleep
  94. + *
  95. + * The board info passed can safely be __initdata ... but be careful of
  96. + * any embedded pointers (platform_data, etc), they're copied as-is.
  97. + */
  98. +int __init
  99. +mdiobus_register_board_info(struct mdio_board_info const *info, unsigned n)
  100. +{
  101. + struct mdio_board_entry *be;
  102. + int i;
  103. +
  104. + be = kzalloc(n * sizeof(*be), GFP_KERNEL);
  105. + if (!be)
  106. + return -ENOMEM;
  107. +
  108. + for (i = 0; i < n; i++, be++, info++) {
  109. + memcpy(&be->board_info, info, sizeof(*info));
  110. + mutex_lock(&__mdio_board_lock);
  111. + list_add_tail(&be->list, &__mdio_board_list);
  112. + mutex_unlock(&__mdio_board_lock);
  113. + }
  114. +
  115. + return 0;
  116. +}
  117. diff --git a/drivers/net/phy/mdio-boardinfo.h b/drivers/net/phy/mdio-boardinfo.h
  118. new file mode 100644
  119. index 0000000..28fbc0d
  120. --- /dev/null
  121. +++ b/drivers/net/phy/mdio-boardinfo.h
  122. @@ -0,0 +1,22 @@
  123. +/*
  124. + * mdio-boardinfo.h - boardinfo interface internal to the mdio_bus component
  125. + *
  126. + * This program is free software; you can redistribute it and/or modify it
  127. + * under the terms of the GNU General Public License as published by the
  128. + * Free Software Foundation; either version 2 of the License, or (at your
  129. + * option) any later version.
  130. + *
  131. + */
  132. +
  133. +#include <linux/mutex.h>
  134. +
  135. +struct mdio_board_entry {
  136. + struct list_head list;
  137. + struct mdio_board_info board_info;
  138. +};
  139. +
  140. +/* __mdio_board_lock protects __mdio_board_list
  141. + * only mdio_bus components are allowed to use these symbols.
  142. + */
  143. +extern struct mutex __mdio_board_lock;
  144. +extern struct list_head __mdio_board_list;
  145. diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c
  146. index 71e4900..50fbe35 100644
  147. --- a/drivers/net/phy/mdio_bus.c
  148. +++ b/drivers/net/phy/mdio_bus.c
  149. @@ -38,6 +38,8 @@
  150. #include <asm/irq.h>
  151. +#include "mdio-boardinfo.h"
  152. +
  153. /**
  154. * mdiobus_alloc_size - allocate a mii_bus structure
  155. * @size: extra amount of memory to allocate for private storage.
  156. @@ -224,15 +226,33 @@ void mdiobus_free(struct mii_bus *bus)
  157. }
  158. EXPORT_SYMBOL(mdiobus_free);
  159. +static void mdiobus_setup_phydev_from_boardinfo(struct mii_bus *bus,
  160. + struct phy_device *phydev,
  161. + struct mdio_board_info *bi)
  162. +{
  163. + if (strcmp(bus->id, bi->bus_id) ||
  164. + bi->phy_addr != phydev->addr)
  165. + return;
  166. +
  167. + phydev->dev.platform_data = (void *) bi->platform_data;
  168. +}
  169. +
  170. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  171. {
  172. struct phy_device *phydev;
  173. + struct mdio_board_entry *be;
  174. int err;
  175. phydev = get_phy_device(bus, addr, false);
  176. if (IS_ERR(phydev) || phydev == NULL)
  177. return phydev;
  178. + mutex_lock(&__mdio_board_lock);
  179. + list_for_each_entry(be, &__mdio_board_list, list)
  180. + mdiobus_setup_phydev_from_boardinfo(bus, phydev,
  181. + &be->board_info);
  182. + mutex_unlock(&__mdio_board_lock);
  183. +
  184. err = phy_device_register(phydev);
  185. if (err) {
  186. phy_device_free(phydev);
  187. diff --git a/include/linux/phy.h b/include/linux/phy.h
  188. index f1441b4..9dca415 100644
  189. --- a/include/linux/phy.h
  190. +++ b/include/linux/phy.h
  191. @@ -658,4 +658,22 @@ int __init mdio_bus_init(void);
  192. void mdio_bus_exit(void);
  193. extern struct bus_type mdio_bus_type;
  194. +
  195. +struct mdio_board_info {
  196. + const char *bus_id;
  197. + int phy_addr;
  198. +
  199. + const void *platform_data;
  200. +};
  201. +
  202. +#ifdef CONFIG_MDIO_BOARDINFO
  203. +int mdiobus_register_board_info(const struct mdio_board_info *info, unsigned n);
  204. +#else
  205. +static inline int
  206. +mdiobus_register_board_info(const struct mdio_board_info *info, unsigned n)
  207. +{
  208. + return 0;
  209. +}
  210. +#endif
  211. +
  212. #endif /* __PHY_H */
  213. --
  214. 1.8.5.3