682-of_net-add-mac-address-increment-support.patch 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. From 844c273286f328acf0dab5fbd5d864366b4904dc Mon Sep 17 00:00:00 2001
  2. From: Ansuel Smith <[email protected]>
  3. Date: Tue, 30 Mar 2021 18:21:14 +0200
  4. Subject: [PATCH] of_net: add mac-address-increment support
  5. Lots of embedded devices use the mac-address of other interface
  6. extracted from nvmem cells and increments it by one or two. Add two
  7. bindings to integrate this and directly use the right mac-address for
  8. the interface. Some example are some routers that use the gmac
  9. mac-address stored in the art partition and increments it by one for the
  10. wifi. mac-address-increment-byte bindings is used to tell what byte of
  11. the mac-address has to be increased (if not defined the last byte is
  12. increased) and mac-address-increment tells how much the byte decided
  13. early has to be increased.
  14. Signed-off-by: Ansuel Smith <[email protected]>
  15. ---
  16. net/core/of_net.c | 43 +++++++++++++++++++++++++++++++++++++++----
  17. 1 file changed, 39 insertions(+), 4 deletions(-)
  18. --- a/net/core/of_net.c
  19. +++ b/net/core/of_net.c
  20. @@ -119,10 +119,19 @@ static int of_get_mac_addr_nvmem(struct
  21. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  22. * but is all zeros.
  23. *
  24. + * DT can tell the system to increment the mac-address after is extracted by
  25. + * using:
  26. + * - mac-address-increment to decide how much to increase. The value WILL
  27. + * overflow to other bytes if the increment is over 255 or the total
  28. + * increment will exceed 255 of the current byte.
  29. + * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:05:00)
  30. + * (example 00:01:02:03:04:fe + 5 == 00:01:02:03:05:03)
  31. + *
  32. * Return: 0 on success and errno in case of error.
  33. */
  34. int of_get_mac_address(struct device_node *np, u8 *addr)
  35. {
  36. + u32 mac_inc, mac_val;
  37. int ret;
  38. if (!np)
  39. @@ -130,17 +139,33 @@ int of_get_mac_address(struct device_nod
  40. ret = of_get_mac_addr(np, "mac-address", addr);
  41. if (!ret)
  42. - return 0;
  43. + goto found;
  44. ret = of_get_mac_addr(np, "local-mac-address", addr);
  45. if (!ret)
  46. - return 0;
  47. + goto found;
  48. ret = of_get_mac_addr(np, "address", addr);
  49. if (!ret)
  50. - return 0;
  51. + goto found;
  52. +
  53. + ret = of_get_mac_addr_nvmem(np, addr);
  54. + if (ret)
  55. + return ret;
  56. +
  57. +found:
  58. + if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) {
  59. + /* Convert to a contiguous value */
  60. + mac_val = (addr[3] << 16) + (addr[4] << 8) + addr[5];
  61. + mac_val += mac_inc;
  62. +
  63. + /* Apply the incremented value handling overflow case */
  64. + addr[3] = (mac_val >> 16) & 0xff;
  65. + addr[4] = (mac_val >> 8) & 0xff;
  66. + addr[5] = (mac_val >> 0) & 0xff;
  67. + }
  68. - return of_get_mac_addr_nvmem(np, addr);
  69. + return ret;
  70. }
  71. EXPORT_SYMBOL(of_get_mac_address);