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