Mt76: MAC address setup

I'm trying to understand how the MAC address of mt76 is set up.

On my module, the MAC address seems to be present in the first 1024 bytes of the "factory" partition mtd2. Somehow, it's read by the kernel module mt76.ko from the code:

void mt76x02_mac_setaddr(struct mt76x02_dev *dev, const u8 *addr)

in drivers/net/wireless/mediatek/mt76/mt76x02_mac.c.

Also, the dts contains:

&factory {
	compatible = "nvmem-cells";
	#address-cells = <1>;
	#size-cells = <1>;

	macaddr_factory_28: macaddr@28 {
		reg = <0x28 0x6>;

And 0x28 is the position of the MAC address in those magic 1024 bytes.

Questions: How does it work? Which code reads mtd2 and feeds the function listed above? More important, how are those 1024 bytes generated in the factory partition? Was this provided by Mediatek (to be customized with the desired MAC address)?

You are missing the part with the m76 node, but most likely macaddr_factory_28 is being set in m76 node as mac-address NVMEM cell.

Then, the driver will call of_get_mac_address() during mt76_eeprom_override() which will try to fetch the MAC address from multiple DTS properties in that node and in the end will then try to get it from NVMEM cell and then it will get read directly from some kind of underlying storage at the offset and length you are seeing.

@robinmarko: Thank you, you are confirming what I'm saying. Above in the dts, there is:

			factory: partition@40000 {
				label = "factory";
				reg = <0x40000 0x10000>;

I understand now (more or less) the path in the kernel. The remaining question is: where does come from those 1024 bytes in the factory partition, which seems to be some kind of firmware settings for the MT76 chip? It definitely includes the desired MAC address. How to generate those 1024 bytes? Is there a MediaTek tool?

There are manufacturing tools for sure, that partition probably includes the caldata as well.

You can
1 - save factory partitiion
find partition with
cat /proc/mtd -> factory is mtdx
cd /tmp:tmp
cat /dev/mtdx > mtdx.bin
2 - hexedit & modify mtdx.bin
3 - write with - > mtd write mtdx.bin factory

Thank you but I know how to edit an hex file!!! What I want to know is the structure of the MT76 calibration/settings file which includes the desired MAC address. This is the sense of my question. Where is the Mediatek tool/script/document for the MT76 driver firmware settings?

Maybe this will help: MT7628 eeprom guide.

Edit: missed that you were talking about MT76x02 so this (very similar) might be better: MT7612 eeprom

OpenWrt use only in the DTS file the MAC address values of the LAN , WAN & WIFI ( mtdx ( factory )
All the rest of the partition is not used

Thank you! The doc mentioned above is the one!