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