eeprom.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Gateworks Corporation
  4. * Author: Tim Harvey <tharvey@gateworks.com>
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <i2c.h>
  9. #include <malloc.h>
  10. #include <asm/bitops.h>
  11. #include "gsc.h"
  12. #include "ventana_eeprom.h"
  13. /* read ventana EEPROM, check for validity, and return baseboard type */
  14. int
  15. read_eeprom(int bus, struct ventana_board_info *info)
  16. {
  17. int i;
  18. int chksum;
  19. char baseboard;
  20. int type;
  21. unsigned char *buf = (unsigned char *)info;
  22. memset(info, 0, sizeof(*info));
  23. /*
  24. * On a board with a missing/depleted backup battery for GSC, the
  25. * board may be ready to probe the GSC before its firmware is
  26. * running. We will wait here indefinately for the GSC/EEPROM.
  27. */
  28. while (1) {
  29. if (0 == i2c_set_bus_num(bus) &&
  30. 0 == i2c_probe(GSC_EEPROM_ADDR))
  31. break;
  32. mdelay(1);
  33. }
  34. /* read eeprom config section */
  35. if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
  36. puts("EEPROM: Failed to read EEPROM\n");
  37. return GW_UNKNOWN;
  38. }
  39. /* sanity checks */
  40. if (info->model[0] != 'G' || info->model[1] != 'W') {
  41. puts("EEPROM: Invalid Model in EEPROM\n");
  42. return GW_UNKNOWN;
  43. }
  44. /* validate checksum */
  45. for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
  46. chksum += buf[i];
  47. if ((info->chksum[0] != chksum>>8) ||
  48. (info->chksum[1] != (chksum&0xff))) {
  49. puts("EEPROM: Failed EEPROM checksum\n");
  50. return GW_UNKNOWN;
  51. }
  52. /* original GW5400-A prototype */
  53. baseboard = info->model[3];
  54. if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
  55. baseboard = '0';
  56. type = GW_UNKNOWN;
  57. switch (baseboard) {
  58. case '0': /* original GW5400-A prototype */
  59. type = GW54proto;
  60. break;
  61. case '1':
  62. type = GW51xx;
  63. break;
  64. case '2':
  65. type = GW52xx;
  66. break;
  67. case '3':
  68. type = GW53xx;
  69. break;
  70. case '4':
  71. type = GW54xx;
  72. break;
  73. case '5':
  74. if (info->model[4] == '1') {
  75. type = GW551x;
  76. break;
  77. } else if (info->model[4] == '2') {
  78. type = GW552x;
  79. break;
  80. } else if (info->model[4] == '3') {
  81. type = GW553x;
  82. break;
  83. }
  84. break;
  85. case '6':
  86. if (info->model[4] == '0')
  87. type = GW560x;
  88. break;
  89. case '9':
  90. if (info->model[4] == '0' && info->model[5] == '3')
  91. type = GW5903;
  92. if (info->model[4] == '0' && info->model[5] == '4')
  93. type = GW5904;
  94. break;
  95. }
  96. return type;
  97. }
  98. /* list of config bits that the bootloader will remove from dtb if not set */
  99. struct ventana_eeprom_config econfig[] = {
  100. { "eth0", "ethernet0", EECONFIG_ETH0 },
  101. { "usb0", NULL, EECONFIG_USB0 },
  102. { "usb1", NULL, EECONFIG_USB1 },
  103. { "mmc0", NULL, EECONFIG_SD0 },
  104. { "mmc1", NULL, EECONFIG_SD1 },
  105. { "mmc2", NULL, EECONFIG_SD2 },
  106. { "mmc3", NULL, EECONFIG_SD3 },
  107. { /* Sentinel */ }
  108. };
  109. #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
  110. static struct ventana_eeprom_config *get_config(const char *name)
  111. {
  112. struct ventana_eeprom_config *cfg = econfig;
  113. while (cfg->name) {
  114. if (0 == strcmp(name, cfg->name))
  115. return cfg;
  116. cfg++;
  117. }
  118. return NULL;
  119. }
  120. static u8 econfig_bytes[sizeof(ventana_info.config)];
  121. static int econfig_init = -1;
  122. static int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  123. {
  124. struct ventana_eeprom_config *cfg;
  125. struct ventana_board_info *info = &ventana_info;
  126. int i;
  127. if (argc < 2)
  128. return CMD_RET_USAGE;
  129. /* initialize */
  130. if (econfig_init != 1) {
  131. memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
  132. econfig_init = 1;
  133. }
  134. /* list configs */
  135. if ((strncmp(argv[1], "list", 4) == 0)) {
  136. cfg = econfig;
  137. while (cfg->name) {
  138. printf("%s: %d\n", cfg->name,
  139. test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
  140. cfg++;
  141. }
  142. }
  143. /* save */
  144. else if ((strncmp(argv[1], "save", 4) == 0)) {
  145. unsigned char *buf = (unsigned char *)info;
  146. int chksum;
  147. /* calculate new checksum */
  148. memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
  149. for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
  150. chksum += buf[i];
  151. debug("old chksum:0x%04x\n",
  152. (info->chksum[0] << 8) | info->chksum[1]);
  153. debug("new chksum:0x%04x\n", chksum);
  154. info->chksum[0] = chksum >> 8;
  155. info->chksum[1] = chksum & 0xff;
  156. /* write new config data */
  157. if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
  158. 1, econfig_bytes, sizeof(econfig_bytes))) {
  159. printf("EEPROM: Failed updating config\n");
  160. return CMD_RET_FAILURE;
  161. }
  162. /* write new config data */
  163. if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
  164. 1, info->chksum, 2)) {
  165. printf("EEPROM: Failed updating checksum\n");
  166. return CMD_RET_FAILURE;
  167. }
  168. printf("Config saved to EEPROM\n");
  169. }
  170. /* get config */
  171. else if (argc == 2) {
  172. cfg = get_config(argv[1]);
  173. if (cfg) {
  174. printf("%s: %d\n", cfg->name,
  175. test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
  176. } else {
  177. printf("invalid config: %s\n", argv[1]);
  178. return CMD_RET_FAILURE;
  179. }
  180. }
  181. /* set config */
  182. else if (argc == 3) {
  183. cfg = get_config(argv[1]);
  184. if (cfg) {
  185. if (simple_strtol(argv[2], NULL, 10)) {
  186. test_and_set_bit(cfg->bit, econfig_bytes);
  187. printf("Enabled %s\n", cfg->name);
  188. } else {
  189. test_and_clear_bit(cfg->bit, econfig_bytes);
  190. printf("Disabled %s\n", cfg->name);
  191. }
  192. } else {
  193. printf("invalid config: %s\n", argv[1]);
  194. return CMD_RET_FAILURE;
  195. }
  196. }
  197. else
  198. return CMD_RET_USAGE;
  199. return CMD_RET_SUCCESS;
  200. }
  201. U_BOOT_CMD(
  202. econfig, 3, 0, do_econfig,
  203. "EEPROM configuration",
  204. "list - list config\n"
  205. "save - save config to EEPROM\n"
  206. "<name> - get config 'name'\n"
  207. "<name> [0|1] - set config 'name' to value\n"
  208. );
  209. #endif /* CONFIG_CMD_EECONFIG */