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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. From 639dba857aa554f2a78572adc4cf3c32de9ec2e2 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 2/2] 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. drivers/of/of_net.c | 59 ++++++++++++++++++++++++++++++++++-----------
  17. 1 file changed, 45 insertions(+), 14 deletions(-)
  18. --- a/drivers/of/of_net.c
  19. +++ b/drivers/of/of_net.c
  20. @@ -115,27 +115,52 @@ 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-byte to decide what byte to increase
  27. + * (if not defined is increased the last byte)
  28. + * - mac-address-increment to decide how much to increase. The value will
  29. + * not overflow to other bytes if the increment is over 255.
  30. + * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00)
  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 inc_idx, mac_inc;
  37. int ret;
  38. + /* Check first if the increment byte is present and valid.
  39. + * If not set assume to increment the last byte if found.
  40. + */
  41. + if (of_property_read_u32(np, "mac-address-increment-byte", &inc_idx))
  42. + inc_idx = 5;
  43. + if (inc_idx < 3 || inc_idx > 5)
  44. + return -EINVAL;
  45. +
  46. if (!np)
  47. return -ENODEV;
  48. ret = of_get_mac_addr(np, "mac-address", addr);
  49. if (!ret)
  50. - return 0;
  51. + goto found;
  52. ret = of_get_mac_addr(np, "local-mac-address", addr);
  53. if (!ret)
  54. - return 0;
  55. + goto found;
  56. ret = of_get_mac_addr(np, "address", addr);
  57. if (!ret)
  58. - return 0;
  59. + goto found;
  60. +
  61. + ret = of_get_mac_addr_nvmem(np, addr);
  62. + if (ret)
  63. + return ret;
  64. +
  65. +found:
  66. + if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
  67. + addr[inc_idx] += mac_inc;
  68. - return of_get_mac_addr_nvmem(np, addr);
  69. + return ret;
  70. }
  71. EXPORT_SYMBOL(of_get_mac_address);