2
0

804-nvmem-core-support-mac-base-fixed-layout-cells.patch 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <[email protected]>
  2. Date: Thu, 13 Jul 2023 18:29:19 +0200
  3. Subject: [PATCH] nvmem: core: support "mac-base" fixed layout cells
  4. Fixed layout binding allows specifying "mac-base" NVMEM cells. It's used
  5. for base MAC address (that can be used for calculating relative
  6. addresses). It can be stored in a raw binary format or as an ASCII
  7. string.
  8. ---
  9. --- a/drivers/nvmem/Kconfig
  10. +++ b/drivers/nvmem/Kconfig
  11. @@ -2,6 +2,7 @@
  12. menuconfig NVMEM
  13. bool "NVMEM Support"
  14. imply NVMEM_LAYOUTS
  15. + select GENERIC_NET_UTILS
  16. help
  17. Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
  18. --- a/drivers/nvmem/core.c
  19. +++ b/drivers/nvmem/core.c
  20. @@ -7,9 +7,12 @@
  21. */
  22. #include <linux/device.h>
  23. +#include <linux/ctype.h>
  24. +#include <linux/etherdevice.h>
  25. #include <linux/export.h>
  26. #include <linux/fs.h>
  27. #include <linux/idr.h>
  28. +#include <linux/if_ether.h>
  29. #include <linux/init.h>
  30. #include <linux/kref.h>
  31. #include <linux/module.h>
  32. @@ -780,6 +783,62 @@ static int nvmem_validate_keepouts(struc
  33. return 0;
  34. }
  35. +static int nvmem_mac_base_raw_read(void *context, const char *id, int index, unsigned int offset,
  36. + void *buf, size_t bytes)
  37. +{
  38. + if (WARN_ON(bytes != ETH_ALEN))
  39. + return -EINVAL;
  40. +
  41. + if (index)
  42. + eth_addr_add(buf, index);
  43. +
  44. + return 0;
  45. +}
  46. +
  47. +static int nvmem_mac_base_ascii_read(void *context, const char *id, int index, unsigned int offset,
  48. + void *buf, size_t bytes)
  49. +{
  50. + u8 mac[ETH_ALEN];
  51. +
  52. + if (WARN_ON(bytes != 3 * ETH_ALEN - 1))
  53. + return -EINVAL;
  54. +
  55. + if (!mac_pton(buf, mac))
  56. + return -EINVAL;
  57. +
  58. + if (index)
  59. + eth_addr_add(mac, index);
  60. +
  61. + ether_addr_copy(buf, mac);
  62. +
  63. + return 0;
  64. +}
  65. +
  66. +static int nvmem_mac_base_hex_read(void *context, const char *id, int index, unsigned int offset,
  67. + void *buf, size_t bytes)
  68. +{
  69. + u8 mac[ETH_ALEN], *hexstr;
  70. + int i;
  71. +
  72. + if (WARN_ON(bytes != 2 * ETH_ALEN))
  73. + return -EINVAL;
  74. +
  75. + hexstr = (u8 *)buf;
  76. + for (i = 0; i < ETH_ALEN; i++) {
  77. + if (!isxdigit(hexstr[i * 2]) || !isxdigit(hexstr[i * 2 + 1]))
  78. + return -EINVAL;
  79. +
  80. + mac[i] = (hex_to_bin(hexstr[i * 2]) << 4) | hex_to_bin(hexstr[i * 2 + 1]);
  81. + }
  82. +
  83. + if (index)
  84. + eth_addr_add(mac, index);
  85. +
  86. + ether_addr_copy(buf, mac);
  87. +
  88. + return 0;
  89. +}
  90. +
  91. static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
  92. {
  93. struct device *dev = &nvmem->dev;
  94. @@ -814,6 +873,25 @@ static int nvmem_add_cells_from_dt(struc
  95. if (nvmem->fixup_dt_cell_info)
  96. nvmem->fixup_dt_cell_info(nvmem, &info);
  97. + if (of_device_is_compatible(np, "fixed-layout")) {
  98. + if (of_device_is_compatible(child, "mac-base")) {
  99. + if (info.bytes == ETH_ALEN) {
  100. + info.raw_len = info.bytes;
  101. + info.bytes = ETH_ALEN;
  102. + info.read_post_process = nvmem_mac_base_raw_read;
  103. + } else if (info.bytes == 2 * ETH_ALEN) {
  104. + info.raw_len = info.bytes;
  105. + info.bytes = ETH_ALEN;
  106. + info.read_post_process = nvmem_mac_base_hex_read;
  107. + } else if (info.bytes == 3 * ETH_ALEN - 1) {
  108. + info.raw_len = info.bytes;
  109. + info.bytes = ETH_ALEN;
  110. + info.read_post_process = nvmem_mac_base_ascii_read;
  111. + }
  112. +
  113. + }
  114. + }
  115. +
  116. ret = nvmem_add_one_cell(nvmem, &info);
  117. kfree(info.name);
  118. if (ret) {