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);
73 dev_err(pdev, "DSA master Ethernet device not found!\n");
77 if (ops->port_enable) {
78 struct dsa_port_pdata *port_pdata;
80 port_pdata = dev_get_parent_plat(pdev);
81 err = ops->port_enable(dev, port_pdata->index,
86 err = ops->port_enable(dev, priv->cpu_port,
87 priv->cpu_port_fixed_phy);
92 return eth_get_ops(master)->start(master);
95 /* Stop the desired port, the CPU port and the master Eth interface */
96 static void dsa_port_stop(struct udevice *pdev)
98 struct udevice *dev = dev_get_parent(pdev);
99 struct dsa_priv *priv = dev_get_uclass_priv(dev);
100 struct udevice *master = dsa_get_master(dev);
101 struct dsa_ops *ops = dsa_get_ops(dev);
103 if (ops->port_disable) {
104 struct dsa_port_pdata *port_pdata;
106 port_pdata = dev_get_parent_plat(pdev);
107 ops->port_disable(dev, port_pdata->index, port_pdata->phy);
108 ops->port_disable(dev, priv->cpu_port, NULL);
112 * stop master only if it's active, don't probe it otherwise.
113 * Under normal usage it would be active because we're using it, but
114 * during tear-down it may have been removed ahead of us.
116 if (master && device_active(master))
117 eth_get_ops(master)->stop(master);
121 * Insert a DSA tag and call master Ethernet send on the resulting packet
122 * We copy the frame to a stack buffer where we have reserved headroom and
123 * tailroom space. Headroom and tailroom are set to 0.
125 static int dsa_port_send(struct udevice *pdev, void *packet, int length)
127 struct udevice *dev = dev_get_parent(pdev);
128 struct dsa_priv *priv = dev_get_uclass_priv(dev);
129 int head = priv->headroom, tail = priv->tailroom;
130 struct udevice *master = dsa_get_master(dev);
131 struct dsa_ops *ops = dsa_get_ops(dev);
132 uchar dsa_packet_tmp[PKTSIZE_ALIGN];
133 struct dsa_port_pdata *port_pdata;
139 if (length + head + tail > PKTSIZE_ALIGN)
142 memset(dsa_packet_tmp, 0, head);
143 memset(dsa_packet_tmp + head + length, 0, tail);
144 memcpy(dsa_packet_tmp + head, packet, length);
145 length += head + tail;
146 /* copy back to preserve original buffer alignment */
147 memcpy(packet, dsa_packet_tmp, length);
149 port_pdata = dev_get_parent_plat(pdev);
150 err = ops->xmit(dev, port_pdata->index, packet, length);
154 return eth_get_ops(master)->send(master, packet, length);
157 /* Receive a frame from master Ethernet, process it and pass it on */
158 static int dsa_port_recv(struct udevice *pdev, int flags, uchar **packetp)
160 struct udevice *dev = dev_get_parent(pdev);
161 struct dsa_priv *priv = dev_get_uclass_priv(dev);
162 int head = priv->headroom, tail = priv->tailroom;
163 struct udevice *master = dsa_get_master(dev);
164 struct dsa_ops *ops = dsa_get_ops(dev);
165 struct dsa_port_pdata *port_pdata;
166 int length, port_index, err;
171 length = eth_get_ops(master)->recv(master, flags, packetp);
176 * If we receive frames from a different port or frames that DSA driver
177 * doesn't like we discard them here.
178 * In case of discard we return with no frame and expect to be called
179 * again instead of looping here, so upper layer can deal with timeouts.
181 port_pdata = dev_get_parent_plat(pdev);
182 err = ops->rcv(dev, &port_index, *packetp, length);
183 if (err || port_index != port_pdata->index || (length <= head + tail)) {
184 if (eth_get_ops(master)->free_pkt)
185 eth_get_ops(master)->free_pkt(master, *packetp, length);
190 * We move the pointer over headroom here to avoid a copy. If free_pkt
191 * gets called we move the pointer back before calling master free_pkt.
195 return length - head - tail;
198 static int dsa_port_free_pkt(struct udevice *pdev, uchar *packet, int length)
200 struct udevice *dev = dev_get_parent(pdev);
201 struct udevice *master = dsa_get_master(dev);
202 struct dsa_priv *priv;
207 priv = dev_get_uclass_priv(dev);
208 if (eth_get_ops(master)->free_pkt) {
209 /* return the original pointer and length to master Eth */
210 packet -= priv->headroom;
211 length += priv->headroom - priv->tailroom;
213 return eth_get_ops(master)->free_pkt(master, packet, length);
219 static int dsa_port_of_to_pdata(struct udevice *pdev)
221 struct dsa_port_pdata *port_pdata;
222 struct dsa_pdata *dsa_pdata;
223 struct eth_pdata *eth_pdata;
232 err = ofnode_read_u32(dev_ofnode(pdev), "reg", &index);
236 dev = dev_get_parent(pdev);
237 dsa_pdata = dev_get_uclass_plat(dev);
239 port_pdata = dev_get_parent_plat(pdev);
240 port_pdata->index = index;
242 label = ofnode_read_string(dev_ofnode(pdev), "label");
244 strncpy(port_pdata->name, label, DSA_PORT_NAME_LENGTH);
246 eth_pdata = dev_get_plat(pdev);
247 eth_pdata->priv_pdata = port_pdata;
249 dev_dbg(pdev, "port %d node %s\n", port_pdata->index,
250 ofnode_get_name(dev_ofnode(pdev)));
255 static const struct eth_ops dsa_port_ops = {
256 .start = dsa_port_start,
257 .send = dsa_port_send,
258 .recv = dsa_port_recv,
259 .stop = dsa_port_stop,
260 .free_pkt = dsa_port_free_pkt,
263 static int dsa_port_probe(struct udevice *pdev)
265 struct udevice *dev = dev_get_parent(pdev);
266 struct eth_pdata *eth_pdata, *master_pdata;
267 unsigned char env_enetaddr[ARP_HLEN];
268 struct dsa_port_pdata *port_pdata;
269 struct dsa_priv *dsa_priv;
270 struct udevice *master;
273 port_pdata = dev_get_parent_plat(pdev);
274 dsa_priv = dev_get_uclass_priv(dev);
276 port_pdata->phy = dm_eth_phy_connect(pdev);
277 if (!port_pdata->phy)
280 master = dsa_get_master(dev);
285 * Probe the master device. We depend on the master device for proper
286 * operation and we also need it for MAC inheritance below.
288 ret = device_probe(master);
293 * Inherit port's hwaddr from the DSA master, unless the port already
294 * has a unique MAC address specified in the environment.
296 eth_env_get_enetaddr_by_index("eth", dev_seq(pdev), env_enetaddr);
297 if (!is_zero_ethaddr(env_enetaddr))
300 master_pdata = dev_get_plat(master);
301 eth_pdata = dev_get_plat(pdev);
302 memcpy(eth_pdata->enetaddr, master_pdata->enetaddr, ARP_HLEN);
303 eth_env_set_enetaddr_by_index("eth", dev_seq(pdev),
304 master_pdata->enetaddr);
309 static int dsa_port_remove(struct udevice *pdev)
311 struct udevice *dev = dev_get_parent(pdev);
312 struct dsa_port_pdata *port_pdata;
313 struct dsa_priv *dsa_priv;
315 port_pdata = dev_get_parent_plat(pdev);
316 dsa_priv = dev_get_uclass_priv(dev);
318 port_pdata->phy = NULL;
323 U_BOOT_DRIVER(dsa_port) = {
324 .name = DSA_PORT_CHILD_DRV_NAME,
326 .ops = &dsa_port_ops,
327 .probe = dsa_port_probe,
328 .remove = dsa_port_remove,
329 .of_to_plat = dsa_port_of_to_pdata,
330 .plat_auto = sizeof(struct eth_pdata),
334 * This function mostly deals with pulling information out of the device tree
335 * into the pdata structure.
336 * It goes through the list of switch ports, registers an eth device for each
337 * front panel port and identifies the cpu port connected to master eth device.
338 * TODO: support cascaded switches
340 static int dsa_post_bind(struct udevice *dev)
342 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
343 ofnode node = dev_ofnode(dev), pnode;
344 int i, err, first_err = 0;
346 if (!ofnode_valid(node))
349 pdata->master_node = ofnode_null();
351 node = ofnode_find_subnode(node, "ports");
352 if (!ofnode_valid(node))
353 node = ofnode_find_subnode(node, "ethernet-ports");
354 if (!ofnode_valid(node)) {
355 dev_err(dev, "ports node is missing under DSA device!\n");
359 pdata->num_ports = ofnode_get_child_count(node);
360 if (pdata->num_ports <= 0 || pdata->num_ports > DSA_MAX_PORTS) {
361 dev_err(dev, "invalid number of ports (%d)\n",
366 /* look for the CPU port */
367 ofnode_for_each_subnode(pnode, node) {
370 if (ofnode_read_u32(pnode, "ethernet", ðernet))
373 pdata->master_node = ofnode_get_by_phandle(ethernet);
374 pdata->cpu_port_node = pnode;
378 if (!ofnode_valid(pdata->master_node)) {
379 dev_err(dev, "master eth node missing!\n");
383 if (ofnode_read_u32(pnode, "reg", &pdata->cpu_port)) {
384 dev_err(dev, "CPU port node not valid!\n");
388 dev_dbg(dev, "master node %s on port %d\n",
389 ofnode_get_name(pdata->master_node), pdata->cpu_port);
391 for (i = 0; i < pdata->num_ports; i++) {
392 char name[DSA_PORT_NAME_LENGTH];
393 struct udevice *pdev;
396 * If this is the CPU port don't register it as an ETH device,
397 * we skip it on purpose since I/O to/from it from the CPU
400 if (i == pdata->cpu_port)
404 * Set up default port names. If present, DT port labels
405 * will override the default port names.
407 snprintf(name, DSA_PORT_NAME_LENGTH, "%s@%d", dev->name, i);
409 ofnode_for_each_subnode(pnode, node) {
412 if (ofnode_read_u32(pnode, "reg", ®))
420 * skip registration if port id not found or if the port
421 * is explicitly disabled in DT
423 if (!ofnode_valid(pnode) || !ofnode_is_available(pnode))
426 err = device_bind_driver_to_node(dev, DSA_PORT_CHILD_DRV_NAME,
429 struct dsa_port_pdata *port_pdata;
431 port_pdata = dev_get_parent_plat(pdev);
432 strncpy(port_pdata->name, name, DSA_PORT_NAME_LENGTH);
433 pdev->name = port_pdata->name;
436 /* try to bind all ports but keep 1st error */
437 if (err && !first_err)
444 dev_dbg(dev, "DSA ports successfully bound\n");
450 * Initialize the uclass per device internal state structure (priv).
451 * TODO: pick up references to other switch devices here, if we're cascaded.
453 static int dsa_pre_probe(struct udevice *dev)
455 struct dsa_pdata *pdata = dev_get_uclass_plat(dev);
456 struct dsa_priv *priv = dev_get_uclass_priv(dev);
458 priv->num_ports = pdata->num_ports;
459 priv->cpu_port = pdata->cpu_port;
460 priv->cpu_port_fixed_phy = fixed_phy_create(pdata->cpu_port_node);
461 if (!priv->cpu_port_fixed_phy) {
462 dev_err(dev, "Failed to register fixed-link for CPU port\n");
466 uclass_find_device_by_ofnode(UCLASS_ETH, pdata->master_node,
471 UCLASS_DRIVER(dsa) = {
474 .post_bind = dsa_post_bind,
475 .pre_probe = dsa_pre_probe,
476 .per_device_auto = sizeof(struct dsa_priv),
477 .per_device_plat_auto = sizeof(struct dsa_pdata),
478 .per_child_plat_auto = sizeof(struct dsa_port_pdata),
479 .flags = DM_UC_FLAG_SEQ_ALIAS,