uboot-lantiq: update to v2013.10
[openwrt/svn-archive/archive.git] / package / boot / uboot-lantiq / patches / 0009-net-switchlib-add-framework-for-ethernet-switch-driv.patch
1 From 0dff8c753c8929a478357abb38db0d1c1a60ec94 Mon Sep 17 00:00:00 2001
2 From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
3 Date: Wed, 29 Aug 2012 22:08:15 +0200
4 Subject: net: switchlib: add framework for ethernet switch drivers
5
6 Add a generic framework similar to phylib for ethernet switch
7 drivers and devices. This is useful to share the init and
8 setup code for switch devices across different boards.
9
10 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
11 Cc: Joe Hershberger <joe.hershberger@gmail.com>
12
13 diff --git a/Makefile b/Makefile
14 index dc04179..6ee9a3c 100644
15 --- a/Makefile
16 +++ b/Makefile
17 @@ -280,6 +280,7 @@ LIBS-y += drivers/mtd/ubi/libubi.o
18 LIBS-y += drivers/mtd/spi/libspi_flash.o
19 LIBS-y += drivers/net/libnet.o
20 LIBS-y += drivers/net/phy/libphy.o
21 +LIBS-y += drivers/net/switch/libswitch.o
22 LIBS-y += drivers/pci/libpci.o
23 LIBS-y += drivers/pcmcia/libpcmcia.o
24 LIBS-y += drivers/power/libpower.o \
25 diff --git a/drivers/net/switch/Makefile b/drivers/net/switch/Makefile
26 new file mode 100644
27 index 0000000..31719d8
28 --- /dev/null
29 +++ b/drivers/net/switch/Makefile
30 @@ -0,0 +1,30 @@
31 +#
32 +# Copyright (C) 2000-2011 Wolfgang Denk, DENX Software Engineering, wd@denx.de
33 +# Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
34 +#
35 +# SPDX-License-Identifier: GPL-2.0+
36 +#
37 +
38 +include $(TOPDIR)/config.mk
39 +
40 +LIB := $(obj)libswitch.o
41 +
42 +COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
43 +
44 +COBJS := $(COBJS-y)
45 +SRCS := $(COBJS:.o=.c)
46 +OBJS := $(addprefix $(obj),$(COBJS))
47 +
48 +all: $(LIB)
49 +
50 +$(LIB): $(obj).depend $(OBJS)
51 + $(call cmd_link_o_target, $(OBJS))
52 +
53 +#########################################################################
54 +
55 +# defines $(obj).depend target
56 +include $(SRCTREE)/rules.mk
57 +
58 +sinclude $(obj).depend
59 +
60 +#########################################################################
61 diff --git a/drivers/net/switch/switch.c b/drivers/net/switch/switch.c
62 new file mode 100644
63 index 0000000..0e1d6b7
64 --- /dev/null
65 +++ b/drivers/net/switch/switch.c
66 @@ -0,0 +1,62 @@
67 +/*
68 + * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
69 + *
70 + * SPDX-License-Identifier: GPL-2.0+
71 + */
72 +
73 +#include <common.h>
74 +#include <netdev.h>
75 +#include <miiphy.h>
76 +#include <switch.h>
77 +
78 +static struct list_head switch_drivers;
79 +static struct list_head switch_devices;
80 +
81 +void switch_init(void)
82 +{
83 + INIT_LIST_HEAD(&switch_drivers);
84 + INIT_LIST_HEAD(&switch_devices);
85 +
86 + board_switch_init();
87 +}
88 +
89 +void switch_driver_register(struct switch_driver *drv)
90 +{
91 + INIT_LIST_HEAD(&drv->list);
92 + list_add_tail(&drv->list, &switch_drivers);
93 +}
94 +
95 +int switch_device_register(struct switch_device *dev)
96 +{
97 + struct switch_driver *drv;
98 +
99 + /* Add switch device only, if an adequate driver is registered */
100 + list_for_each_entry(drv, &switch_drivers, list) {
101 + if (!strcmp(drv->name, dev->name)) {
102 + dev->drv = drv;
103 +
104 + INIT_LIST_HEAD(&dev->list);
105 + list_add_tail(&dev->list, &switch_devices);
106 +
107 + return 0;
108 + }
109 + }
110 +
111 + return -1;
112 +}
113 +
114 +struct switch_device *switch_connect(struct mii_dev *bus)
115 +{
116 + struct switch_device *sw;
117 + int err;
118 +
119 + list_for_each_entry(sw, &switch_devices, list) {
120 + sw->bus = bus;
121 +
122 + err = sw->drv->probe(sw);
123 + if (!err)
124 + return sw;
125 + }
126 +
127 + return NULL;
128 +}
129 diff --git a/include/switch.h b/include/switch.h
130 new file mode 100644
131 index 0000000..4a7ae63
132 --- /dev/null
133 +++ b/include/switch.h
134 @@ -0,0 +1,102 @@
135 +/*
136 + * This file is released under the terms of GPL v2 and any later version.
137 + * See the file COPYING in the root directory of the source tree for details.
138 + *
139 + * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
140 + */
141 +
142 +#ifndef __SWITCH_H
143 +#define __SWITCH_H
144 +
145 +#include <linux/list.h>
146 +
147 +#define SWITCH_NAME_SIZE 32
148 +
149 +struct switch_device;
150 +struct mii_dev;
151 +
152 +struct switch_driver {
153 + struct list_head list;
154 +
155 + /* Switch device name */
156 + const char name[SWITCH_NAME_SIZE];
157 +
158 + /*
159 + * Called to probe the switch chip. Must return 0 if the switch
160 + * chip matches the given switch device/driver combination. Otherwise
161 + * 1 must be returned.
162 + */
163 + int (*probe) (struct switch_device *dev);
164 +
165 + /*
166 + * Called to initialize the switch chip.
167 + */
168 + void (*setup) (struct switch_device *dev);
169 +};
170 +
171 +struct switch_device {
172 + struct list_head list;
173 + struct switch_driver *drv;
174 +
175 + /* MII bus the switch chip is connected to */
176 + struct mii_dev *bus;
177 +
178 + /* Switch device name */
179 + const char name[SWITCH_NAME_SIZE];
180 +
181 + /* Bitmask for board specific setup of used switch ports */
182 + u16 port_mask;
183 +
184 + /* Number of switch port that is connected to host CPU */
185 + u16 cpu_port;
186 +};
187 +
188 +/*
189 + * Board specific switch initialization.
190 + *
191 + * Called from switch_init to register the board specific switch_device
192 + * structure.
193 + */
194 +extern int board_switch_init(void);
195 +
196 +/* Initialize switch subsystem */
197 +#ifdef CONFIG_SWITCH_MULTI
198 +extern void switch_init(void);
199 +#else
200 +static inline void switch_init(void)
201 +{
202 +}
203 +#endif
204 +
205 +/* Register a switch driver */
206 +extern void switch_driver_register(struct switch_driver *drv);
207 +
208 +/* Register a switch device */
209 +extern int switch_device_register(struct switch_device *dev);
210 +
211 +/*
212 + * Probe the available switch chips and connect the found one
213 + * with the given MII bus
214 + */
215 +#ifdef CONFIG_SWITCH_MULTI
216 +extern struct switch_device *switch_connect(struct mii_dev *bus);
217 +#else
218 +static inline struct switch_device *switch_connect(struct mii_dev *bus)
219 +{
220 + return NULL;
221 +}
222 +#endif
223 +
224 +/*
225 + * Setup the given switch device
226 + */
227 +static inline void switch_setup(struct switch_device *dev)
228 +{
229 + if (dev->drv->setup)
230 + dev->drv->setup(dev);
231 +}
232 +
233 +/* Init functions for supported Switch drivers */
234 +
235 +#endif /* __SWITCH_H */
236 +
237 diff --git a/net/eth.c b/net/eth.c
238 index c96e767..03ecc1c 100644
239 --- a/net/eth.c
240 +++ b/net/eth.c
241 @@ -10,6 +10,7 @@
242 #include <net.h>
243 #include <miiphy.h>
244 #include <phy.h>
245 +#include <switch.h>
246
247 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
248 {
249 @@ -287,6 +288,8 @@ int eth_initialize(bd_t *bis)
250 phy_init();
251 #endif
252
253 + switch_init();
254 +
255 eth_env_init(bis);
256
257 /*
258 --
259 1.8.3.2
260