eeprom.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
  3. *
  4. * Authors: Nikita Kiryanov <nikita@compulab.co.il>
  5. * Igor Grinberg <grinberg@compulab.co.il>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <i2c.h>
  11. #ifndef CONFIG_SYS_I2C_EEPROM_ADDR
  12. # define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
  13. # define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
  14. #endif
  15. #ifndef CONFIG_SYS_I2C_EEPROM_BUS
  16. #define CONFIG_SYS_I2C_EEPROM_BUS 0
  17. #endif
  18. #define EEPROM_LAYOUT_VER_OFFSET 44
  19. #define BOARD_SERIAL_OFFSET 20
  20. #define BOARD_SERIAL_OFFSET_LEGACY 8
  21. #define BOARD_REV_OFFSET 0
  22. #define BOARD_REV_OFFSET_LEGACY 6
  23. #define BOARD_REV_SIZE 2
  24. #define MAC_ADDR_OFFSET 4
  25. #define MAC_ADDR_OFFSET_LEGACY 0
  26. #define LAYOUT_INVALID 0
  27. #define LAYOUT_LEGACY 0xff
  28. static int cl_eeprom_bus;
  29. static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
  30. static int cl_eeprom_read(uint offset, uchar *buf, int len)
  31. {
  32. int res;
  33. unsigned int current_i2c_bus = i2c_get_bus_num();
  34. res = i2c_set_bus_num(cl_eeprom_bus);
  35. if (res < 0)
  36. return res;
  37. res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
  38. CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
  39. i2c_set_bus_num(current_i2c_bus);
  40. return res;
  41. }
  42. static int cl_eeprom_setup(uint eeprom_bus)
  43. {
  44. int res;
  45. /*
  46. * We know the setup was already done when the layout is set to a valid
  47. * value and we're using the same bus as before.
  48. */
  49. if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
  50. return 0;
  51. cl_eeprom_bus = eeprom_bus;
  52. res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
  53. (uchar *)&cl_eeprom_layout, 1);
  54. if (res) {
  55. cl_eeprom_layout = LAYOUT_INVALID;
  56. return res;
  57. }
  58. if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
  59. cl_eeprom_layout = LAYOUT_LEGACY;
  60. return 0;
  61. }
  62. void get_board_serial(struct tag_serialnr *serialnr)
  63. {
  64. u32 serial[2];
  65. uint offset;
  66. memset(serialnr, 0, sizeof(*serialnr));
  67. if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
  68. return;
  69. offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
  70. BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
  71. if (cl_eeprom_read(offset, (uchar *)serial, 8))
  72. return;
  73. if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
  74. serialnr->low = serial[0];
  75. serialnr->high = serial[1];
  76. }
  77. }
  78. /*
  79. * Routine: cl_eeprom_read_mac_addr
  80. * Description: read mac address and store it in buf.
  81. */
  82. int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
  83. {
  84. uint offset;
  85. if (cl_eeprom_setup(eeprom_bus))
  86. return 0;
  87. offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
  88. MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
  89. return cl_eeprom_read(offset, buf, 6);
  90. }
  91. static u32 board_rev;
  92. /*
  93. * Routine: cl_eeprom_get_board_rev
  94. * Description: read system revision from eeprom
  95. */
  96. u32 cl_eeprom_get_board_rev(void)
  97. {
  98. char str[5]; /* Legacy representation can contain at most 4 digits */
  99. uint offset = BOARD_REV_OFFSET_LEGACY;
  100. if (board_rev)
  101. return board_rev;
  102. if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
  103. return 0;
  104. if (cl_eeprom_layout != LAYOUT_LEGACY)
  105. offset = BOARD_REV_OFFSET;
  106. if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
  107. return 0;
  108. /*
  109. * Convert legacy syntactic representation to semantic
  110. * representation. i.e. for rev 1.00: 0x100 --> 0x64
  111. */
  112. if (cl_eeprom_layout == LAYOUT_LEGACY) {
  113. sprintf(str, "%x", board_rev);
  114. board_rev = simple_strtoul(str, NULL, 10);
  115. }
  116. return board_rev;
  117. };