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