1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019-2021 NXP
8 #include <dm/device_compat.h>
9 #include <dm/device-internal.h>
10 #include <dm/uclass-internal.h>
11 #include <linux/bitmap.h>
14 #define DSA_PORT_CHILD_DRV_NAME "dsa-port"
16 /* per-device internal state structure */
18 struct phy_device *cpu_port_fixed_phy;
19 struct udevice *master_dev;
27 int dsa_set_tagging(struct udevice *dev, ushort headroom, ushort tailroom)
29 struct dsa_priv *priv;
34 if (headroom + tailroom > DSA_MAX_OVR)
37 priv = dev_get_uclass_priv(dev);
40 priv->headroom = headroom;
42 priv->tailroom = tailroom;
47 /* returns the DSA master Ethernet device */
48 struct udevice *dsa_get_master(struct udevice *dev)
50 struct dsa_priv *priv;
55 priv = dev_get_uclass_priv(dev);
57 return priv->master_dev;
61 * Start the desired port, the CPU port and the master Eth interface.
62 * TODO: if cascaded we may need to _start ports in other switches too
64 static int dsa_port_start(struct udevice *pdev)
66 struct udevice *dev = dev_get_parent(pdev);
67 struct dsa_priv *priv = dev_get_uclass_priv(dev);
68 struct udevice *master = dsa_get_master(dev);
69 struct dsa_ops *ops = dsa_get_ops(dev);
72 if (ops->port_enable) {
73 struct dsa_port_pdata *port_pdata;
75 port_pdata = dev_get_parent_plat(pdev);
76 err = ops->port_enable(dev, port_pdata->index,
81 err = ops->port_enable(dev, priv->cpu_port,
82 priv->cpu_port_fixed_phy);
87 return eth_get_ops(master)->start(master);
90 /* Stop the desired port, the CPU port and the master Eth interface */
91 static void dsa_port_stop(struct udevice *pdev)
93 struct udevice *dev = dev_get_parent(pdev);
94 struct dsa_priv *priv = dev_get_uclass_priv(dev);
95 struct udevice *master = dsa_get_master(dev);
96 struct dsa_ops *ops = dsa_get_ops(dev);
98 if (ops->port_disable) {
99 struct dsa_port_pdata *port_pdata;
101 port_pdata = dev_get_parent_plat(pdev);
102 ops->port_disable(dev, port_pdata->index, port_pdata->phy);
103 ops->port_disable(dev, priv->cpu_port, NULL);
106 eth_get_ops(master)->stop(master);
110 * Insert a DSA tag and call master Ethernet send on the resulting packet
111 * We copy the frame to a stack buffer where we have reserved headroom and
112 * tailroom space. Headroom and tailroom are set to 0.
114 static int dsa_port_send(struct udevice *pdev, void *packet, int length)
116 struct udevice *dev = dev_get_parent(pdev);
117 struct dsa_priv *priv = dev_get_uclass_priv(dev);
118 int head = priv->headroom, tail = priv->tailroom;
119 struct udevice *master = dsa_get_master(dev);
120 struct dsa_ops *ops = dsa_get_ops(dev);
121 uchar dsa_packet_tmp[PKTSIZE_ALIGN];
122 struct dsa_port_pdata *port_pdata;
125 if (length + head + tail > PKTSIZE_ALIGN)
128 memset(dsa_packet_tmp, 0, head);
129 memset(dsa_packet_tmp + head + length, 0, tail);
130 memcpy(dsa_packet_tmp + head, packet, length);
131 length += head + tail;
132 /* copy back to preserve original buffer alignment */
133 memcpy(packet, dsa_packet_tmp, length);
135 port_pdata = dev_get_parent_plat(pdev);
136 err = ops->xmit(dev, port_pdata->index, packet, length);
140 return eth_get_ops(master)->send(master, packet, length);
143 /* Receive a frame from master Ethernet, process it and pass it on */
144 static int dsa_port_recv(struct udevice *pdev, int flags, uchar **packetp)
146 struct udevice *dev = dev_get_parent(pdev);
147 struct dsa_priv *priv = dev_get_uclass_priv(dev);
148 int head = priv->headroom, tail = priv->tailroom;
149 struct udevice *master = dsa_get_master(dev);
150 struct dsa_ops *ops = dsa_get_ops(dev);
151 struct dsa_port_pdata *port_pdata;
152 int length, port_index, err;
154 length = eth_get_ops(master)->recv(master, flags, packetp);
159 * If we receive frames from a different port or frames that DSA driver
160 * doesn't like we discard them here.
161 * In case of discard we return with no frame and expect to be called
162 * again instead of looping here, so upper layer can deal with timeouts.
164 port_pdata = dev_get_parent_plat(pdev);
165 err = ops->rcv(dev, &port_index, *packetp, length);
166 if (err || port_index != port_pdata->index || (length <= head + tail)) {
167 if (eth_get_ops(master)->free_pkt)
168 eth_get_ops(master)->free_pkt(master, *packetp, length);
173 * We move the pointer over headroom here to avoid a copy. If free_pkt
174 * gets called we move the pointer back before calling master free_pkt.
178 return length - head - tail;
181 static int dsa_port_free_pkt(struct udevice *pdev, uchar *packet, int length)
183 struct udevice *dev = dev_get_parent(pdev);
184 struct udevice *master = dsa_get_master(dev);
185 struct dsa_priv *priv;
187 priv = dev_get_uclass_priv(dev);
188 if (eth_get_ops(master)->free_pkt) {
189 /* return the original pointer and length to master Eth */
190 packet -= priv->headroom;
191 length += priv->headroom - priv->tailroom;
193 return eth_get_ops(master)->free_pkt(master, packet, length);
199 static int dsa_port_of_to_pdata(struct udevice *pdev)
201 struct dsa_port_pdata *port_pdata;
202 struct dsa_pdata *dsa_pdata;
203 struct eth_pdata *eth_pdata;
212 err = ofnode_read_u32(dev_ofnode(pdev), "reg", &index);
216 dev = dev_get_parent(pdev);
217 dsa_pdata = dev_get_uclass_plat(dev);
219 port_pdata = dev_get_parent_plat(pdev);
220 port_pdata->index = index;
222 label = ofnode_read_string(dev_ofnode(pdev), "label");
224 strncpy(port_pdata->name, label, DSA_PORT_NAME_LENGTH);
226 eth_pdata = dev_get_plat(pdev);
227 eth_pdata->priv_pdata = port_pdata;
229 dev_dbg(pdev, "port %d node %s\n", port_pdata->index,
230 ofnode_get_name(dev_ofnode(pdev)));
235 static const struct eth_ops dsa_port_ops = {
236 .start = dsa_port_start,
237 .send = dsa_port_send,
238 .recv = dsa_port_recv,
239 .stop = dsa_port_stop,
240 .free_pkt = dsa_port_free_pkt,
244 * Inherit port's hwaddr from the DSA master, unless the port already has a
245 * unique MAC address specified in the environment.
247 static void dsa_port_set_hwaddr(struct udevice *pdev, struct udevice *master)
249 struct eth_pdata *eth_pdata, *master_pdata;
250 unsigned char env_enetaddr[ARP_HLEN];
252 eth_env_get_enetaddr_by_index("eth", dev_seq(pdev), env_enetaddr);
253 if (!is_zero_ethaddr(env_enetaddr)) {
254 /* individual port mac addrs require master to be promisc */
255 struct eth_ops *eth_ops = eth_get_ops(master);
257 if (eth_ops->set_promisc)
258 eth_ops->set_promisc(master, 1);
263 master_pdata = dev_get_plat(master);
264 eth_pdata = dev_get_plat(pdev);
265 memcpy(eth_pdata->enetaddr, master_pdata->enetaddr, ARP_HLEN);
266 eth_env_set_enetaddr_by_index("eth", dev_seq(pdev),
267 master_pdata->enetaddr);
270 static int dsa_port_probe(struct udevice *pdev)
272 struct udevice *dev = dev_get_parent(pdev);
273 struct dsa_port_pdata *port_pdata;
274 struct dsa_priv *dsa_priv;
275 struct udevice *master;
278 port_pdata = dev_get_parent_plat(pdev);
279 dsa_priv = dev_get_uclass_priv(dev);
281 port_pdata->phy = dm_eth_phy_connect(pdev);
282 if (!port_pdata->phy)
285 master = dsa_get_master(dev);
290 * Probe the master device. We depend on the master device for proper
291 * operation and we also need it for MAC inheritance below.
293 * TODO: we assume the master device is always there and doesn't get
294 * removed during runtime.
296 err = device_probe(master);
300 dsa_port_set_hwaddr(pdev, master);
305 static int dsa_port_remove(struct udevice *pdev)
307 struct udevice *dev = dev_get_parent(pdev);
308 struct dsa_port_pdata *port_pdata;
309 struct dsa_priv *dsa_priv;
311 port_pdata = dev_get_parent_plat(pdev);
312 dsa_priv = dev_get_uclass_priv(dev);
314 port_pdata->phy = NULL;
319 U_BOOT_DRIVER(dsa_port) = {
320 .name = DSA_PORT_CHILD_DRV_NAME,
322 .ops = &dsa_port_ops,
323 .probe = dsa_port_probe,
324 .remove = dsa_port_remove,
325 .of_to_plat = dsa_port_of_to_pdata,
326 .plat_auto = sizeof(struct eth_pdata),
330 * This function mostly deals with pulling information out of the device tree
331 * into the pdata structure.
332 * It goes through the list of switch ports, registers an eth device for each
333 * front panel port and identifies the cpu port connected to master eth device.
334 * TODO: support cascaded switches
336 static int dsa_post_bind(struct udevice *dev)
338 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
339 ofnode node = dev_ofnode(dev), pnode;
340 int i, err, first_err = 0;
342 if (!ofnode_valid(node))
345 pdata->master_node = ofnode_null();
347 node = ofnode_find_subnode(node, "ports");
348 if (!ofnode_valid(node))
349 node = ofnode_find_subnode(node, "ethernet-ports");
350 if (!ofnode_valid(node)) {
351 dev_err(dev, "ports node is missing under DSA device!\n");
355 pdata->num_ports = ofnode_get_child_count(node);
356 if (pdata->num_ports <= 0 || pdata->num_ports > DSA_MAX_PORTS) {
357 dev_err(dev, "invalid number of ports (%d)\n",
362 /* look for the CPU port */
363 ofnode_for_each_subnode(pnode, node) {
366 if (ofnode_read_u32(pnode, "ethernet", ðernet))
369 pdata->master_node = ofnode_get_by_phandle(ethernet);
370 pdata->cpu_port_node = pnode;
374 if (!ofnode_valid(pdata->master_node)) {
375 dev_err(dev, "master eth node missing!\n");
379 if (ofnode_read_u32(pnode, "reg", &pdata->cpu_port)) {
380 dev_err(dev, "CPU port node not valid!\n");
384 dev_dbg(dev, "master node %s on port %d\n",
385 ofnode_get_name(pdata->master_node), pdata->cpu_port);
387 for (i = 0; i < pdata->num_ports; i++) {
388 char name[DSA_PORT_NAME_LENGTH];
389 struct udevice *pdev;
392 * If this is the CPU port don't register it as an ETH device,
393 * we skip it on purpose since I/O to/from it from the CPU
396 if (i == pdata->cpu_port)
400 * Set up default port names. If present, DT port labels
401 * will override the default port names.
403 snprintf(name, DSA_PORT_NAME_LENGTH, "%s@%d", dev->name, i);
405 ofnode_for_each_subnode(pnode, node) {
408 if (ofnode_read_u32(pnode, "reg", ®))
416 * skip registration if port id not found or if the port
417 * is explicitly disabled in DT
419 if (!ofnode_valid(pnode) || !ofnode_is_available(pnode))
422 err = device_bind_driver_to_node(dev, DSA_PORT_CHILD_DRV_NAME,
425 struct dsa_port_pdata *port_pdata;
427 port_pdata = dev_get_parent_plat(pdev);
428 strncpy(port_pdata->name, name, DSA_PORT_NAME_LENGTH);
429 pdev->name = port_pdata->name;
432 /* try to bind all ports but keep 1st error */
433 if (err && !first_err)
440 dev_dbg(dev, "DSA ports successfully bound\n");
446 * Initialize the uclass per device internal state structure (priv).
447 * TODO: pick up references to other switch devices here, if we're cascaded.
449 static int dsa_pre_probe(struct udevice *dev)
451 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
452 struct dsa_priv *priv = dev_get_uclass_priv(dev);
454 priv->num_ports = pdata->num_ports;
455 priv->cpu_port = pdata->cpu_port;
456 priv->cpu_port_fixed_phy = fixed_phy_create(pdata->cpu_port_node);
457 if (!priv->cpu_port_fixed_phy) {
458 dev_err(dev, "Failed to register fixed-link for CPU port\n");
462 uclass_find_device_by_ofnode(UCLASS_ETH, pdata->master_node,
467 UCLASS_DRIVER(dsa) = {
470 .post_bind = dsa_post_bind,
471 .pre_probe = dsa_pre_probe,
472 .per_device_auto = sizeof(struct dsa_priv),
473 .per_device_plat_auto = sizeof(struct dsa_pdata),
474 .per_child_plat_auto = sizeof(struct dsa_port_pdata),
475 .flags = DM_UC_FLAG_SEQ_ALIAS,