eeprom.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 2
  29. #define MAC_ADDR_OFFSET 4
  30. #define MAC_ADDR_OFFSET_LEGACY 0
  31. #define LAYOUT_INVALID 0
  32. #define LAYOUT_LEGACY 0xff
  33. static int eeprom_layout; /* Implicitly LAYOUT_INVALID */
  34. static int cm_t3x_eeprom_read(uint offset, uchar *buf, int len)
  35. {
  36. return i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
  37. CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
  38. }
  39. static int eeprom_setup_layout(void)
  40. {
  41. int res;
  42. if (eeprom_layout != LAYOUT_INVALID)
  43. return 0;
  44. res = cm_t3x_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
  45. (uchar *)&eeprom_layout, 1);
  46. if (res) {
  47. eeprom_layout = LAYOUT_INVALID;
  48. return res;
  49. }
  50. if (eeprom_layout == 0 || eeprom_layout >= 0x20)
  51. eeprom_layout = LAYOUT_LEGACY;
  52. return 0;
  53. }
  54. void get_board_serial(struct tag_serialnr *serialnr)
  55. {
  56. u32 serial[2];
  57. uint offset;
  58. memset(serialnr, 0, sizeof(*serialnr));
  59. if (eeprom_setup_layout())
  60. return;
  61. offset = (eeprom_layout != LAYOUT_LEGACY) ?
  62. BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
  63. if (cm_t3x_eeprom_read(offset, (uchar *)serial, 8))
  64. return;
  65. if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
  66. serialnr->low = serial[0];
  67. serialnr->high = serial[1];
  68. }
  69. }
  70. /*
  71. * Routine: cm_t3x_eeprom_read_mac_addr
  72. * Description: read mac address and store it in buf.
  73. */
  74. int cm_t3x_eeprom_read_mac_addr(uchar *buf)
  75. {
  76. uint offset;
  77. if (eeprom_setup_layout())
  78. return 0;
  79. offset = (eeprom_layout != LAYOUT_LEGACY) ?
  80. MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
  81. return cm_t3x_eeprom_read(offset, buf, 6);
  82. }
  83. /*
  84. * Routine: cm_t3x_eeprom_get_board_rev
  85. * Description: read system revision from eeprom
  86. */
  87. u32 cm_t3x_eeprom_get_board_rev(void)
  88. {
  89. u32 rev = 0;
  90. char str[5]; /* Legacy representation can contain at most 4 digits */
  91. uint offset = BOARD_REV_OFFSET_LEGACY;
  92. if (eeprom_setup_layout())
  93. return 0;
  94. if (eeprom_layout != LAYOUT_LEGACY)
  95. offset = BOARD_REV_OFFSET;
  96. if (cm_t3x_eeprom_read(offset, (uchar *)&rev, BOARD_REV_SIZE))
  97. return 0;
  98. /*
  99. * Convert legacy syntactic representation to semantic
  100. * representation. i.e. for rev 1.00: 0x100 --> 0x64
  101. */
  102. if (eeprom_layout == LAYOUT_LEGACY) {
  103. sprintf(str, "%x", rev);
  104. rev = simple_strtoul(str, NULL, 10);
  105. }
  106. return rev;
  107. };