1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Date: Thu, 13 Jul 2023 18:29:19 +0200
3 Subject: [PATCH] nvmem: core: support "mac-base" fixed layout cells
5 Fixed layout binding allows specifying "mac-base" NVMEM cells. It's used
6 for base MAC address (that can be used for calculating relative
7 addresses). It can be stored in a raw binary format or as an ASCII
11 --- a/drivers/nvmem/Kconfig
12 +++ b/drivers/nvmem/Kconfig
17 + select GENERIC_NET_UTILS
19 Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
21 --- a/drivers/nvmem/core.c
22 +++ b/drivers/nvmem/core.c
26 #include <linux/device.h>
27 +#include <linux/ctype.h>
28 +#include <linux/etherdevice.h>
29 #include <linux/export.h>
31 #include <linux/idr.h>
32 +#include <linux/if_ether.h>
33 #include <linux/init.h>
34 #include <linux/kref.h>
35 #include <linux/module.h>
36 @@ -779,6 +782,62 @@ static int nvmem_validate_keepouts(struc
40 +static int nvmem_mac_base_raw_read(void *context, const char *id, int index, unsigned int offset,
41 + void *buf, size_t bytes)
43 + if (WARN_ON(bytes != ETH_ALEN))
47 + eth_addr_add(buf, index);
52 +static int nvmem_mac_base_ascii_read(void *context, const char *id, int index, unsigned int offset,
53 + void *buf, size_t bytes)
57 + if (WARN_ON(bytes != 3 * ETH_ALEN - 1))
60 + if (!mac_pton(buf, mac))
64 + eth_addr_add(mac, index);
66 + ether_addr_copy(buf, mac);
71 +static int nvmem_mac_base_hex_read(void *context, const char *id, int index, unsigned int offset,
72 + void *buf, size_t bytes)
74 + u8 mac[ETH_ALEN], *hexstr;
77 + if (WARN_ON(bytes != 2 * ETH_ALEN))
81 + for (i = 0; i < ETH_ALEN; i++) {
82 + if (!isxdigit(hexstr[i * 2]) || !isxdigit(hexstr[i * 2 + 1]))
85 + mac[i] = (hex_to_bin(hexstr[i * 2]) << 4) | hex_to_bin(hexstr[i * 2 + 1]);
89 + eth_addr_add(mac, index);
91 + ether_addr_copy(buf, mac);
96 static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
98 struct device *dev = &nvmem->dev;
99 @@ -813,6 +872,25 @@ static int nvmem_add_cells_from_dt(struc
100 if (nvmem->fixup_dt_cell_info)
101 nvmem->fixup_dt_cell_info(nvmem, &info);
103 + if (of_device_is_compatible(np, "fixed-layout")) {
104 + if (of_device_is_compatible(child, "mac-base")) {
105 + if (info.bytes == ETH_ALEN) {
106 + info.raw_len = info.bytes;
107 + info.bytes = ETH_ALEN;
108 + info.read_post_process = nvmem_mac_base_raw_read;
109 + } else if (info.bytes == 2 * ETH_ALEN) {
110 + info.raw_len = info.bytes;
111 + info.bytes = ETH_ALEN;
112 + info.read_post_process = nvmem_mac_base_hex_read;
113 + } else if (info.bytes == 3 * ETH_ALEN - 1) {
114 + info.raw_len = info.bytes;
115 + info.bytes = ETH_ALEN;
116 + info.read_post_process = nvmem_mac_base_ascii_read;
122 ret = nvmem_add_one_cell(nvmem, &info);