804-nvmem-core-support-mac-base-fixed-layout-cells.patch 3.2 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. @@ -1,6 +1,7 @@
  12. # SPDX-License-Identifier: GPL-2.0-only
  13. menuconfig NVMEM
  14. bool "NVMEM Support"
  15. + select GENERIC_NET_UTILS
  16. imply NVMEM_LAYOUTS
  17. help
  18. Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
  19. --- a/drivers/nvmem/core.c
  20. +++ b/drivers/nvmem/core.c
  21. @@ -7,9 +7,12 @@
  22. */
  23. #include <linux/device.h>
  24. +#include <linux/ctype.h>
  25. +#include <linux/etherdevice.h>
  26. #include <linux/export.h>
  27. #include <linux/fs.h>
  28. #include <linux/idr.h>
  29. +#include <linux/if_ether.h>
  30. #include <linux/init.h>
  31. #include <linux/kref.h>
  32. #include <linux/module.h>
  33. @@ -780,6 +783,62 @@ static int nvmem_validate_keepouts(struc
  34. return 0;
  35. }
  36. +static int nvmem_mac_base_raw_read(void *context, const char *id, int index, unsigned int offset,
  37. + void *buf, size_t bytes)
  38. +{
  39. + if (WARN_ON(bytes != ETH_ALEN))
  40. + return -EINVAL;
  41. +
  42. + if (index)
  43. + eth_addr_add(buf, index);
  44. +
  45. + return 0;
  46. +}
  47. +
  48. +static int nvmem_mac_base_ascii_read(void *context, const char *id, int index, unsigned int offset,
  49. + void *buf, size_t bytes)
  50. +{
  51. + u8 mac[ETH_ALEN];
  52. +
  53. + if (WARN_ON(bytes != 3 * ETH_ALEN - 1))
  54. + return -EINVAL;
  55. +
  56. + if (!mac_pton(buf, mac))
  57. + return -EINVAL;
  58. +
  59. + if (index)
  60. + eth_addr_add(mac, index);
  61. +
  62. + ether_addr_copy(buf, mac);
  63. +
  64. + return 0;
  65. +}
  66. +
  67. +static int nvmem_mac_base_hex_read(void *context, const char *id, int index, unsigned int offset,
  68. + void *buf, size_t bytes)
  69. +{
  70. + u8 mac[ETH_ALEN], *hexstr;
  71. + int i;
  72. +
  73. + if (WARN_ON(bytes != 2 * ETH_ALEN))
  74. + return -EINVAL;
  75. +
  76. + hexstr = (u8 *)buf;
  77. + for (i = 0; i < ETH_ALEN; i++) {
  78. + if (!isxdigit(hexstr[i * 2]) || !isxdigit(hexstr[i * 2 + 1]))
  79. + return -EINVAL;
  80. +
  81. + mac[i] = (hex_to_bin(hexstr[i * 2]) << 4) | hex_to_bin(hexstr[i * 2 + 1]);
  82. + }
  83. +
  84. + if (index)
  85. + eth_addr_add(mac, index);
  86. +
  87. + ether_addr_copy(buf, mac);
  88. +
  89. + return 0;
  90. +}
  91. +
  92. static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
  93. {
  94. struct device *dev = &nvmem->dev;
  95. @@ -814,6 +873,25 @@ static int nvmem_add_cells_from_dt(struc
  96. if (nvmem->fixup_dt_cell_info)
  97. nvmem->fixup_dt_cell_info(nvmem, &info);
  98. + if (of_device_is_compatible(np, "fixed-layout")) {
  99. + if (of_device_is_compatible(child, "mac-base")) {
  100. + if (info.bytes == ETH_ALEN) {
  101. + info.raw_len = info.bytes;
  102. + info.bytes = ETH_ALEN;
  103. + info.read_post_process = nvmem_mac_base_raw_read;
  104. + } else if (info.bytes == 2 * ETH_ALEN) {
  105. + info.raw_len = info.bytes;
  106. + info.bytes = ETH_ALEN;
  107. + info.read_post_process = nvmem_mac_base_hex_read;
  108. + } else if (info.bytes == 3 * ETH_ALEN - 1) {
  109. + info.raw_len = info.bytes;
  110. + info.bytes = ETH_ALEN;
  111. + info.read_post_process = nvmem_mac_base_ascii_read;
  112. + }
  113. +
  114. + }
  115. + }
  116. +
  117. ret = nvmem_add_one_cell(nvmem, &info);
  118. kfree(info.name);
  119. if (ret) {