mac-spi-nor.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Microchip Technology Inc. and its subsidiaries
  4. *
  5. * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <env.h>
  10. #include <net.h>
  11. #include <linux/mtd/spi-nor.h>
  12. #include <netdev.h>
  13. #define ETH_ADDR_SIZE 6
  14. #ifdef CONFIG_SPI_FLASH_SST
  15. #define SFDP_MICROCHIP_MANUF_ID 0xbf
  16. #define SFDP_MICROCHIP_MEM_TYPE 0x26
  17. #define SFDP_MICROCHIP_DEV_ID 0x43
  18. #define SFDP_MICROCHIP_EUI_OFFSET 0x60
  19. #define SFDP_MICROCHIP_EUI48 0x30
  20. struct sst26vf064beui {
  21. u8 manufacturer_id;
  22. u8 memory_type;
  23. u8 device_id;
  24. u8 reserved;
  25. };
  26. /**
  27. * sst26vf064beui_check() - Check the validity of the EUI-48 information from
  28. * the sst26vf064beui SPI NOR Microchip SFDP table.
  29. * @manufacturer_sfdp: pointer to the Microchip manufacturer specific SFDP
  30. * table.
  31. *
  32. * Return: 0 on success, -errno otherwise.
  33. */
  34. static int sst26vf064beui_check(const u8 *manufacturer_sfdp)
  35. {
  36. struct sst26vf064beui *sst26vf064beui =
  37. (struct sst26vf064beui *)manufacturer_sfdp;
  38. if (sst26vf064beui->manufacturer_id != SFDP_MICROCHIP_MANUF_ID)
  39. return -EINVAL;
  40. if (sst26vf064beui->memory_type != SFDP_MICROCHIP_MEM_TYPE)
  41. return -EINVAL;
  42. if (sst26vf064beui->device_id != SFDP_MICROCHIP_DEV_ID)
  43. return -EINVAL;
  44. /*
  45. * Check if the EUI-48 MAC address is programmed in the next six address
  46. * locations.
  47. */
  48. if (manufacturer_sfdp[SFDP_MICROCHIP_EUI_OFFSET] !=
  49. SFDP_MICROCHIP_EUI48)
  50. return -EINVAL;
  51. return 0;
  52. }
  53. /**
  54. * sst26vf064beui_get_ethaddr() - Get the ethernet address from the
  55. * sst26vf064beui SPI NOR Microchip SFDP table.
  56. * @manufacturer_sfdp: pointer to the Microchip manufacturer specific SFDP
  57. * table.
  58. * @ethaddr: pointer where to fill the ethernet address
  59. * @size: size of the ethernet address.
  60. *
  61. * Return: 0 on success, -errno otherwise.
  62. */
  63. static int sst26vf064beui_get_ethaddr(const u8 *manufacturer_sfdp,
  64. u8 *ethaddr, size_t size)
  65. {
  66. u64 eui_table[2];
  67. u64 *p = (u64 *)&manufacturer_sfdp[SFDP_MICROCHIP_EUI_OFFSET];
  68. int i, ret;
  69. ret = sst26vf064beui_check(manufacturer_sfdp);
  70. if (ret)
  71. return ret;
  72. for (i = 0; i < 2; i++)
  73. eui_table[i] = le64_to_cpu(p[i]);
  74. /* Ethaddr starts at offset one. */
  75. memcpy(ethaddr, &((u8 *)eui_table)[1], size);
  76. return 0;
  77. }
  78. #endif
  79. /**
  80. * at91_spi_nor_set_ethaddr() - Retrieve and set the ethernet address from the
  81. * SPI NOR manufacturer specific SFDP table.
  82. */
  83. void at91_spi_nor_set_ethaddr(void)
  84. {
  85. struct udevice *dev;
  86. struct spi_nor *nor;
  87. const char *ethaddr_name = "ethaddr";
  88. u8 ethaddr[ETH_ADDR_SIZE] = {0};
  89. if (env_get(ethaddr_name))
  90. return;
  91. if (uclass_first_device_err(UCLASS_SPI_FLASH, &dev))
  92. return;
  93. nor = dev_get_uclass_priv(dev);
  94. if (!nor)
  95. return;
  96. if (!nor->manufacturer_sfdp)
  97. return;
  98. #ifdef CONFIG_SPI_FLASH_SST
  99. if (sst26vf064beui_get_ethaddr(nor->manufacturer_sfdp, ethaddr,
  100. ETH_ADDR_SIZE))
  101. return;
  102. #endif
  103. if (is_valid_ethaddr(ethaddr))
  104. eth_env_set_enetaddr(ethaddr_name, ethaddr);
  105. }