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
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.
10 Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
11 Cc: Joe Hershberger <joe.hershberger@gmail.com>
13 diff --git a/Makefile b/Makefile
14 index dc04179..6ee9a3c 100644
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
27 index 0000000..31719d8
29 +++ b/drivers/net/switch/Makefile
32 +# Copyright (C) 2000-2011 Wolfgang Denk, DENX Software Engineering, wd@denx.de
33 +# Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
35 +# SPDX-License-Identifier: GPL-2.0+
38 +include $(TOPDIR)/config.mk
40 +LIB := $(obj)libswitch.o
42 +COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
45 +SRCS := $(COBJS:.o=.c)
46 +OBJS := $(addprefix $(obj),$(COBJS))
50 +$(LIB): $(obj).depend $(OBJS)
51 + $(call cmd_link_o_target, $(OBJS))
53 +#########################################################################
55 +# defines $(obj).depend target
56 +include $(SRCTREE)/rules.mk
58 +sinclude $(obj).depend
60 +#########################################################################
61 diff --git a/drivers/net/switch/switch.c b/drivers/net/switch/switch.c
63 index 0000000..0e1d6b7
65 +++ b/drivers/net/switch/switch.c
68 + * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
70 + * SPDX-License-Identifier: GPL-2.0+
78 +static struct list_head switch_drivers;
79 +static struct list_head switch_devices;
81 +void switch_init(void)
83 + INIT_LIST_HEAD(&switch_drivers);
84 + INIT_LIST_HEAD(&switch_devices);
86 + board_switch_init();
89 +void switch_driver_register(struct switch_driver *drv)
91 + INIT_LIST_HEAD(&drv->list);
92 + list_add_tail(&drv->list, &switch_drivers);
95 +int switch_device_register(struct switch_device *dev)
97 + struct switch_driver *drv;
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)) {
104 + INIT_LIST_HEAD(&dev->list);
105 + list_add_tail(&dev->list, &switch_devices);
114 +struct switch_device *switch_connect(struct mii_dev *bus)
116 + struct switch_device *sw;
119 + list_for_each_entry(sw, &switch_devices, list) {
122 + err = sw->drv->probe(sw);
129 diff --git a/include/switch.h b/include/switch.h
131 index 0000000..4a7ae63
133 +++ b/include/switch.h
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.
139 + * Copyright (C) 2011-2013 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
145 +#include <linux/list.h>
147 +#define SWITCH_NAME_SIZE 32
149 +struct switch_device;
152 +struct switch_driver {
153 + struct list_head list;
155 + /* Switch device name */
156 + const char name[SWITCH_NAME_SIZE];
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.
163 + int (*probe) (struct switch_device *dev);
166 + * Called to initialize the switch chip.
168 + void (*setup) (struct switch_device *dev);
171 +struct switch_device {
172 + struct list_head list;
173 + struct switch_driver *drv;
175 + /* MII bus the switch chip is connected to */
176 + struct mii_dev *bus;
178 + /* Switch device name */
179 + const char name[SWITCH_NAME_SIZE];
181 + /* Bitmask for board specific setup of used switch ports */
184 + /* Number of switch port that is connected to host CPU */
189 + * Board specific switch initialization.
191 + * Called from switch_init to register the board specific switch_device
194 +extern int board_switch_init(void);
196 +/* Initialize switch subsystem */
197 +#ifdef CONFIG_SWITCH_MULTI
198 +extern void switch_init(void);
200 +static inline void switch_init(void)
205 +/* Register a switch driver */
206 +extern void switch_driver_register(struct switch_driver *drv);
208 +/* Register a switch device */
209 +extern int switch_device_register(struct switch_device *dev);
212 + * Probe the available switch chips and connect the found one
213 + * with the given MII bus
215 +#ifdef CONFIG_SWITCH_MULTI
216 +extern struct switch_device *switch_connect(struct mii_dev *bus);
218 +static inline struct switch_device *switch_connect(struct mii_dev *bus)
225 + * Setup the given switch device
227 +static inline void switch_setup(struct switch_device *dev)
229 + if (dev->drv->setup)
230 + dev->drv->setup(dev);
233 +/* Init functions for supported Switch drivers */
235 +#endif /* __SWITCH_H */
237 diff --git a/net/eth.c b/net/eth.c
238 index c96e767..03ecc1c 100644
247 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
249 @@ -287,6 +288,8 @@ int eth_initialize(bd_t *bis)