0009-net-switchlib-add-framework-for-ethernet-switch-driv.patch 5.5 KB

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