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