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