Commit ebf30e8451f457aebc8232a674fa75823dd10d49

Authored by Thierry Reding
Committed by Tom Warren
1 parent 6d93d245c1

fdtdec: Add fdtdec_set_ethernet_mac_address()

This function can be used to set the local MAC address for the default
Ethernet interface in its device tree node. The default interface is
identified by the "ethernet" alias.

One case where this is useful is for devices that store their MAC
address in a custom location. Once extracted, board code can store the
MAC address in U-Boot's control DTB so that it will automatically be
used by the Ethernet uclass.

Signed-off-by: Thierry Reding <treding@nvidia.com>
Signed-off-by: Tom Warren <twarren@nvidia.com>

Showing 2 changed files with 53 additions and 0 deletions Side-by-side Diff

... ... @@ -997,6 +997,30 @@
997 997 int fdtdec_setup_memory_banksize(void);
998 998  
999 999 /**
  1000 + * fdtdec_set_ethernet_mac_address() - set MAC address for default interface
  1001 + *
  1002 + * Looks up the default interface via the "ethernet" alias (in the /aliases
  1003 + * node) and stores the given MAC in its "local-mac-address" property. This
  1004 + * is useful on platforms that store the MAC address in a custom location.
  1005 + * Board code can call this in the late init stage to make sure that the
  1006 + * interface device tree node has the right MAC address configured for the
  1007 + * Ethernet uclass to pick it up.
  1008 + *
  1009 + * Typically the FDT passed into this function will be U-Boot's control DTB.
  1010 + * Given that a lot of code may be holding offsets to various nodes in that
  1011 + * tree, this code will only set the "local-mac-address" property in-place,
  1012 + * which means that it needs to exist and have space for the 6-byte address.
  1013 + * This ensures that the operation is non-destructive and does not invalidate
  1014 + * offsets that other drivers may be using.
  1015 + *
  1016 + * @param fdt FDT blob
  1017 + * @param mac buffer containing the MAC address to set
  1018 + * @param size size of MAC address
  1019 + * @return 0 on success or a negative error code on failure
  1020 + */
  1021 +int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size);
  1022 +
  1023 +/**
1000 1024 * fdtdec_set_phandle() - sets the phandle of a given node
1001 1025 *
1002 1026 * @param blob FDT blob
... ... @@ -1261,6 +1261,35 @@
1261 1261 }
1262 1262 #endif
1263 1263  
  1264 +int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size)
  1265 +{
  1266 + const char *path;
  1267 + int offset, err;
  1268 +
  1269 + if (!is_valid_ethaddr(mac))
  1270 + return -EINVAL;
  1271 +
  1272 + path = fdt_get_alias(fdt, "ethernet");
  1273 + if (!path)
  1274 + return 0;
  1275 +
  1276 + debug("ethernet alias found: %s\n", path);
  1277 +
  1278 + offset = fdt_path_offset(fdt, path);
  1279 + if (offset < 0) {
  1280 + debug("ethernet alias points to absent node %s\n", path);
  1281 + return -ENOENT;
  1282 + }
  1283 +
  1284 + err = fdt_setprop_inplace(fdt, offset, "local-mac-address", mac, size);
  1285 + if (err < 0)
  1286 + return err;
  1287 +
  1288 + debug("MAC address: %pM\n", mac);
  1289 +
  1290 + return 0;
  1291 +}
  1292 +
1264 1293 static int fdtdec_init_reserved_memory(void *blob)
1265 1294 {
1266 1295 int na, ns, node, err;