eeprom.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc.
  20. */
  21. #include <common.h>
  22. #include <i2c.h>
  23. #define EEPROM_LAYOUT_VER_OFFSET 44
  24. #define BOARD_SERIAL_OFFSET 20
  25. #define BOARD_SERIAL_OFFSET_LEGACY 8
  26. #define BOARD_REV_OFFSET 0
  27. #define BOARD_REV_OFFSET_LEGACY 6
  28. #define BOARD_REV_SIZE 4
  29. #define BOARD_REV_SIZE_LEGACY 2
  30. #define MAC_ADDR_OFFSET 4
  31. #define MAC_ADDR_OFFSET_LEGACY 0
  32. #define LAYOUT_INVALID 0
  33. #define LAYOUT_LEGACY 0xff
  34. static int eeprom_layout; /* Implicitly LAYOUT_INVALID */
  35. static int cm_t3x_eeprom_read(uint offset, uchar *buf, int len)
  36. {
  37. return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
  38. CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
  39. }
  40. static int eeprom_setup_layout(void)
  41. {
  42. int res;
  43. if (eeprom_layout != LAYOUT_INVALID)
  44. return 0;
  45. res = cm_t3x_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
  46. (uchar *)&eeprom_layout, 1);
  47. if (res) {
  48. eeprom_layout = LAYOUT_INVALID;
  49. return res;
  50. }
  51. if (eeprom_layout == 0 || eeprom_layout >= 0x20)
  52. eeprom_layout = LAYOUT_LEGACY;
  53. return 0;
  54. }
  55. void get_board_serial(struct tag_serialnr *serialnr)
  56. {
  57. u32 serial[2];
  58. uint offset;
  59. memset(serialnr, 0, sizeof(*serialnr));
  60. if (eeprom_setup_layout())
  61. return;
  62. offset = (eeprom_layout != LAYOUT_LEGACY) ?
  63. BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
  64. if (cm_t3x_eeprom_read(offset, (uchar *)serial, 8))
  65. return;
  66. if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
  67. serialnr->low = serial[0];
  68. serialnr->high = serial[1];
  69. }
  70. }
  71. /*
  72. * Routine: cm_t3x_eeprom_read_mac_addr
  73. * Description: read mac address and store it in buf.
  74. */
  75. int cm_t3x_eeprom_read_mac_addr(uchar *buf)
  76. {
  77. uint offset;
  78. if (eeprom_setup_layout())
  79. return 0;
  80. offset = (eeprom_layout != LAYOUT_LEGACY) ?
  81. MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
  82. return cm_t3x_eeprom_read(offset, buf, 6);
  83. }
  84. /*
  85. * Routine: get_board_rev
  86. * Description: read system revision
  87. */
  88. u32 get_board_rev(void)
  89. {
  90. u32 rev = 0;
  91. uint offset = BOARD_REV_OFFSET_LEGACY;
  92. int len = BOARD_REV_SIZE_LEGACY;
  93. if (eeprom_setup_layout())
  94. return 0;
  95. if (eeprom_layout != LAYOUT_LEGACY) {
  96. offset = BOARD_REV_OFFSET;
  97. len = BOARD_REV_SIZE;
  98. }
  99. if (cm_t3x_eeprom_read(offset, (uchar *)&rev, len))
  100. return 0;
  101. return rev;
  102. };