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