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
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>
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 \
24 +++ b/drivers/net/switch/Makefile
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.
30 +# Copyright (C) 2000-2011 Wolfgang Denk, DENX Software Engineering, wd@denx.de
31 +# Copyright (C) 2011-2012 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
34 +include $(TOPDIR)/config.mk
36 +LIB := $(obj)libswitch.o
38 +COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
41 +SRCS := $(COBJS:.o=.c)
42 +OBJS := $(addprefix $(obj),$(COBJS))
46 +$(LIB): $(obj).depend $(OBJS)
47 + $(call cmd_link_o_target, $(OBJS))
49 +#########################################################################
51 +# defines $(obj).depend target
52 +include $(SRCTREE)/rules.mk
54 +sinclude $(obj).depend
56 +#########################################################################
58 +++ b/drivers/net/switch/switch.c
61 + * Copyright (C) 2011-2012 Daniel Schwierzeck, daniel.schwierzeck@gmail.com
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.
72 +static struct list_head switch_drivers;
73 +static struct list_head switch_devices;
75 +void switch_init(void)
77 + INIT_LIST_HEAD(&switch_drivers);
78 + INIT_LIST_HEAD(&switch_devices);
80 + board_switch_init();
83 +void switch_driver_register(struct switch_driver *drv)
85 + INIT_LIST_HEAD(&drv->list);
86 + list_add_tail(&drv->list, &switch_drivers);
89 +int switch_device_register(struct switch_device *dev)
91 + struct switch_driver *drv;
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)) {
98 + INIT_LIST_HEAD(&dev->list);
99 + list_add_tail(&dev->list, &switch_devices);
108 +struct switch_device *switch_connect(struct mii_dev *bus)
110 + struct switch_device *sw;
113 + list_for_each_entry(sw, &switch_devices, list) {
116 + err = sw->drv->probe(sw);
124 +++ b/include/switch.h
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.
130 + * Copyright (C) 2011 Daniel Schwierzeck, daniel.schwierzeck@googlemail.com
136 +#include <linux/list.h>
138 +#define SWITCH_NAME_SIZE 32
140 +struct switch_device;
143 +struct switch_driver {
144 + struct list_head list;
146 + /* Switch device name */
147 + const char name[SWITCH_NAME_SIZE];
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.
154 + int (*probe) (struct switch_device *dev);
157 + * Called to initialize the switch chip.
159 + void (*setup) (struct switch_device *dev);
162 +struct switch_device {
163 + struct list_head list;
164 + struct switch_driver *drv;
166 + /* MII bus the switch chip is connected to */
167 + struct mii_dev *bus;
169 + /* Switch device name */
170 + const char name[SWITCH_NAME_SIZE];
172 + /* Bitmask for board specific setup of used switch ports */
175 + /* Number of switch port that is connected to host CPU */
180 + * Board specific switch initialization.
182 + * Called from switch_init to register the board specific switch_device
185 +extern int board_switch_init(void);
187 +/* Initialize switch subsystem */
188 +#ifdef CONFIG_SWITCH_MULTI
189 +extern void switch_init(void);
191 +static inline void switch_init(void)
196 +/* Register a switch driver */
197 +extern void switch_driver_register(struct switch_driver *drv);
199 +/* Register a switch device */
200 +extern int switch_device_register(struct switch_device *dev);
203 + * Probe the available switch chips and connect the found one
204 + * with the given MII bus
206 +extern struct switch_device *switch_connect(struct mii_dev *bus);
209 + * Setup the given switch device
211 +static inline void switch_setup(struct switch_device *dev)
213 + if (dev->drv->setup)
214 + dev->drv->setup(dev);
217 +/* Init functions for supported Switch drivers */
219 +#endif /* __SWITCH_H */
229 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
231 @@ -303,6 +304,8 @@ int eth_initialize(bd_t *bis)