eeprom.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2014 Gateworks Corporation
  3. * Author: Tim Harvey <tharvey@gateworks.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <i2c.h>
  10. #include <malloc.h>
  11. #include <asm/bitops.h>
  12. #include "gsc.h"
  13. #include "ventana_eeprom.h"
  14. /* read ventana EEPROM, check for validity, and return baseboard type */
  15. int
  16. read_eeprom(int bus, struct ventana_board_info *info)
  17. {
  18. int i;
  19. int chksum;
  20. char baseboard;
  21. int type;
  22. unsigned char *buf = (unsigned char *)info;
  23. memset(info, 0, sizeof(*info));
  24. /*
  25. * On a board with a missing/depleted backup battery for GSC, the
  26. * board may be ready to probe the GSC before its firmware is
  27. * running. We will wait here indefinately for the GSC/EEPROM.
  28. */
  29. while (1) {
  30. if (0 == i2c_set_bus_num(bus) &&
  31. 0 == i2c_probe(GSC_EEPROM_ADDR))
  32. break;
  33. mdelay(1);
  34. }
  35. /* read eeprom config section */
  36. if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
  37. puts("EEPROM: Failed to read EEPROM\n");
  38. return GW_UNKNOWN;
  39. }
  40. /* sanity checks */
  41. if (info->model[0] != 'G' || info->model[1] != 'W') {
  42. puts("EEPROM: Invalid Model in EEPROM\n");
  43. return GW_UNKNOWN;
  44. }
  45. /* validate checksum */
  46. for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
  47. chksum += buf[i];
  48. if ((info->chksum[0] != chksum>>8) ||
  49. (info->chksum[1] != (chksum&0xff))) {
  50. puts("EEPROM: Failed EEPROM checksum\n");
  51. return GW_UNKNOWN;
  52. }
  53. /* original GW5400-A prototype */
  54. baseboard = info->model[3];
  55. if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
  56. baseboard = '0';
  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. }
  81. /* fall through */
  82. default:
  83. printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
  84. type = GW_UNKNOWN;
  85. break;
  86. }
  87. return type;
  88. }
  89. /* list of config bits that the bootloader will remove from dtb if not set */
  90. struct ventana_eeprom_config econfig[] = {
  91. { "eth0", "ethernet0", EECONFIG_ETH0 },
  92. { "eth1", "ethernet1", EECONFIG_ETH1 },
  93. { "sata", "ahci0", EECONFIG_SATA },
  94. { "pcie", NULL, EECONFIG_PCIE},
  95. { "lvds0", NULL, EECONFIG_LVDS0 },
  96. { "lvds1", NULL, EECONFIG_LVDS1 },
  97. { "usb0", NULL, EECONFIG_USB0 },
  98. { "usb1", NULL, EECONFIG_USB1 },
  99. { "mmc0", NULL, EECONFIG_SD0 },
  100. { "mmc1", NULL, EECONFIG_SD1 },
  101. { "mmc2", NULL, EECONFIG_SD2 },
  102. { "mmc3", NULL, EECONFIG_SD3 },
  103. { "uart0", NULL, EECONFIG_UART0 },
  104. { "uart1", NULL, EECONFIG_UART1 },
  105. { "uart2", NULL, EECONFIG_UART2 },
  106. { "uart3", NULL, EECONFIG_UART3 },
  107. { "uart4", NULL, EECONFIG_UART4 },
  108. { "ipu0", NULL, EECONFIG_IPU0 },
  109. { "ipu1", NULL, EECONFIG_IPU1 },
  110. { "can0", NULL, EECONFIG_FLEXCAN },
  111. { "i2c0", NULL, EECONFIG_I2C0 },
  112. { "i2c1", NULL, EECONFIG_I2C1 },
  113. { "i2c2", NULL, EECONFIG_I2C2 },
  114. { "vpu", NULL, EECONFIG_VPU },
  115. { "csi0", NULL, EECONFIG_CSI0 },
  116. { "csi1", NULL, EECONFIG_CSI1 },
  117. { "spi0", NULL, EECONFIG_ESPCI0 },
  118. { "spi1", NULL, EECONFIG_ESPCI1 },
  119. { "spi2", NULL, EECONFIG_ESPCI2 },
  120. { "spi3", NULL, EECONFIG_ESPCI3 },
  121. { "spi4", NULL, EECONFIG_ESPCI4 },
  122. { "spi5", NULL, EECONFIG_ESPCI5 },
  123. { "gps", "pps", EECONFIG_GPS },
  124. { "hdmi_in", NULL, EECONFIG_HDMI_IN },
  125. { "hdmi_out", NULL, EECONFIG_HDMI_OUT },
  126. { "cvbs_in", NULL, EECONFIG_VID_IN },
  127. { "cvbs_out", NULL, EECONFIG_VID_OUT },
  128. { "nand", NULL, EECONFIG_NAND },
  129. { /* Sentinel */ }
  130. };
  131. #ifdef CONFIG_CMD_EECONFIG
  132. static struct ventana_eeprom_config *get_config(const char *name)
  133. {
  134. struct ventana_eeprom_config *cfg = econfig;
  135. while (cfg->name) {
  136. if (0 == strcmp(name, cfg->name))
  137. return cfg;
  138. cfg++;
  139. }
  140. return NULL;
  141. }
  142. static u8 econfig_bytes[sizeof(ventana_info.config)];
  143. static int econfig_init = -1;
  144. int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  145. {
  146. struct ventana_eeprom_config *cfg;
  147. struct ventana_board_info *info = &ventana_info;
  148. int i;
  149. if (argc < 2)
  150. return CMD_RET_USAGE;
  151. /* initialize */
  152. if (econfig_init != 1) {
  153. memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
  154. econfig_init = 1;
  155. }
  156. /* list configs */
  157. if ((strncmp(argv[1], "list", 4) == 0)) {
  158. cfg = econfig;
  159. while (cfg->name) {
  160. printf("%s: %d\n", cfg->name,
  161. test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
  162. cfg++;
  163. }
  164. }
  165. /* save */
  166. else if ((strncmp(argv[1], "save", 4) == 0)) {
  167. unsigned char *buf = (unsigned char *)info;
  168. int chksum;
  169. /* calculate new checksum */
  170. memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
  171. for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
  172. chksum += buf[i];
  173. debug("old chksum:0x%04x\n",
  174. (info->chksum[0] << 8) | info->chksum[1]);
  175. debug("new chksum:0x%04x\n", chksum);
  176. info->chksum[0] = chksum >> 8;
  177. info->chksum[1] = chksum & 0xff;
  178. /* write new config data */
  179. if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
  180. 1, econfig_bytes, sizeof(econfig_bytes))) {
  181. printf("EEPROM: Failed updating config\n");
  182. return CMD_RET_FAILURE;
  183. }
  184. /* write new config data */
  185. if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
  186. 1, info->chksum, 2)) {
  187. printf("EEPROM: Failed updating checksum\n");
  188. return CMD_RET_FAILURE;
  189. }
  190. printf("Config saved to EEPROM\n");
  191. }
  192. /* get config */
  193. else if (argc == 2) {
  194. cfg = get_config(argv[1]);
  195. if (cfg) {
  196. printf("%s: %d\n", cfg->name,
  197. test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
  198. } else {
  199. printf("invalid config: %s\n", argv[1]);
  200. return CMD_RET_FAILURE;
  201. }
  202. }
  203. /* set config */
  204. else if (argc == 3) {
  205. cfg = get_config(argv[1]);
  206. if (cfg) {
  207. if (simple_strtol(argv[2], NULL, 10)) {
  208. test_and_set_bit(cfg->bit, econfig_bytes);
  209. printf("Enabled %s\n", cfg->name);
  210. } else {
  211. test_and_clear_bit(cfg->bit, econfig_bytes);
  212. printf("Disabled %s\n", cfg->name);
  213. }
  214. } else {
  215. printf("invalid config: %s\n", argv[1]);
  216. return CMD_RET_FAILURE;
  217. }
  218. }
  219. else
  220. return CMD_RET_USAGE;
  221. return CMD_RET_SUCCESS;
  222. }
  223. U_BOOT_CMD(
  224. econfig, 3, 0, do_econfig,
  225. "EEPROM configuration",
  226. "list - list config\n"
  227. "save - save config to EEPROM\n"
  228. "<name> - get config 'name'\n"
  229. "<name> [0|1] - set config 'name' to value\n"
  230. );
  231. #endif /* CONFIG_CMD_EECONFIG */