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