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