]> git.ipfire.org Git - people/ms/u-boot.git/blame - board/gateworks/gw_ventana/eeprom.c
imx: ventana: add GW560x support
[people/ms/u-boot.git] / board / gateworks / gw_ventana / eeprom.c
CommitLineData
1badf2f4
TH
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>
9c0fe83e 9#include <errno.h>
1badf2f4 10#include <i2c.h>
9c0fe83e
TH
11#include <malloc.h>
12#include <asm/bitops.h>
1badf2f4
TH
13
14#include "gsc.h"
15#include "ventana_eeprom.h"
16
17/* read ventana EEPROM, check for validity, and return baseboard type */
18int
19read_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");
1badf2f4
TH
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");
1badf2f4
TH
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");
1badf2f4
TH
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
8d1a6ff8 67 type = GW_UNKNOWN;
1badf2f4
TH
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;
3aa22674 84 case '5':
75f21e31
TH
85 if (info->model[4] == '1') {
86 type = GW551x;
87 break;
88 } else if (info->model[4] == '2') {
89 type = GW552x;
90 break;
385575bc
TH
91 } else if (info->model[4] == '3') {
92 type = GW553x;
93 break;
75f21e31 94 }
8d1a6ff8 95 break;
94a1d6c6
TH
96 case '6':
97 if (info->model[4] == '0')
98 type = GW560x;
99 break;
8d1a6ff8
TH
100 case '9':
101 if (info->model[4] == '0' && info->model[5] == '4')
102 type = GW5904;
1badf2f4
TH
103 break;
104 }
105 return type;
106}
9c0fe83e
TH
107
108/* list of config bits that the bootloader will remove from dtb if not set */
109struct ventana_eeprom_config econfig[] = {
110 { "eth0", "ethernet0", EECONFIG_ETH0 },
9c0fe83e
TH
111 { "usb0", NULL, EECONFIG_USB0 },
112 { "usb1", NULL, EECONFIG_USB1 },
113 { "mmc0", NULL, EECONFIG_SD0 },
114 { "mmc1", NULL, EECONFIG_SD1 },
115 { "mmc2", NULL, EECONFIG_SD2 },
116 { "mmc3", NULL, EECONFIG_SD3 },
9c0fe83e
TH
117 { /* Sentinel */ }
118};
119
120#ifdef CONFIG_CMD_EECONFIG
121static struct ventana_eeprom_config *get_config(const char *name)
122{
123 struct ventana_eeprom_config *cfg = econfig;
124
125 while (cfg->name) {
126 if (0 == strcmp(name, cfg->name))
127 return cfg;
128 cfg++;
129 }
130 return NULL;
131}
132
133static u8 econfig_bytes[sizeof(ventana_info.config)];
134static int econfig_init = -1;
135
136int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
137{
138 struct ventana_eeprom_config *cfg;
139 struct ventana_board_info *info = &ventana_info;
140 int i;
141
142 if (argc < 2)
143 return CMD_RET_USAGE;
144
145 /* initialize */
146 if (econfig_init != 1) {
147 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
148 econfig_init = 1;
149 }
150
151 /* list configs */
152 if ((strncmp(argv[1], "list", 4) == 0)) {
153 cfg = econfig;
154 while (cfg->name) {
155 printf("%s: %d\n", cfg->name,
156 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
157 cfg++;
158 }
159 }
160
161 /* save */
162 else if ((strncmp(argv[1], "save", 4) == 0)) {
163 unsigned char *buf = (unsigned char *)info;
164 int chksum;
165
166 /* calculate new checksum */
167 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
168 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
169 chksum += buf[i];
170 debug("old chksum:0x%04x\n",
171 (info->chksum[0] << 8) | info->chksum[1]);
172 debug("new chksum:0x%04x\n", chksum);
173 info->chksum[0] = chksum >> 8;
174 info->chksum[1] = chksum & 0xff;
175
176 /* write new config data */
177 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
178 1, econfig_bytes, sizeof(econfig_bytes))) {
179 printf("EEPROM: Failed updating config\n");
180 return CMD_RET_FAILURE;
181 }
182
183 /* write new config data */
184 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
185 1, info->chksum, 2)) {
186 printf("EEPROM: Failed updating checksum\n");
187 return CMD_RET_FAILURE;
188 }
189
190 printf("Config saved to EEPROM\n");
191 }
192
193 /* get config */
194 else if (argc == 2) {
195 cfg = get_config(argv[1]);
196 if (cfg) {
197 printf("%s: %d\n", cfg->name,
198 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
199 } else {
200 printf("invalid config: %s\n", argv[1]);
201 return CMD_RET_FAILURE;
202 }
203 }
204
205 /* set config */
206 else if (argc == 3) {
207 cfg = get_config(argv[1]);
208 if (cfg) {
209 if (simple_strtol(argv[2], NULL, 10)) {
210 test_and_set_bit(cfg->bit, econfig_bytes);
211 printf("Enabled %s\n", cfg->name);
212 } else {
213 test_and_clear_bit(cfg->bit, econfig_bytes);
214 printf("Disabled %s\n", cfg->name);
215 }
216 } else {
217 printf("invalid config: %s\n", argv[1]);
218 return CMD_RET_FAILURE;
219 }
220 }
221
222 else
223 return CMD_RET_USAGE;
224
225 return CMD_RET_SUCCESS;
226}
227
228U_BOOT_CMD(
229 econfig, 3, 0, do_econfig,
230 "EEPROM configuration",
231 "list - list config\n"
232 "save - save config to EEPROM\n"
233 "<name> - get config 'name'\n"
234 "<name> [0|1] - set config 'name' to value\n"
235);
236
237#endif /* CONFIG_CMD_EECONFIG */