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 <i2c.h>
10 
11 #include "gsc.h"
12 #include "ventana_eeprom.h"
13 
14 /* read ventana EEPROM, check for validity, and return baseboard type */
15 int
16 read_eeprom(int bus, struct ventana_board_info *info)
17 {
18 	int i;
19 	int chksum;
20 	char baseboard;
21 	int type;
22 	unsigned char *buf = (unsigned char *)info;
23 
24 	memset(info, 0, sizeof(*info));
25 
26 	/*
27 	 * On a board with a missing/depleted backup battery for GSC, the
28 	 * board may be ready to probe the GSC before its firmware is
29 	 * running.  We will wait here indefinately for the GSC/EEPROM.
30 	 */
31 	while (1) {
32 		if (0 == i2c_set_bus_num(bus) &&
33 		    0 == i2c_probe(GSC_EEPROM_ADDR))
34 			break;
35 		mdelay(1);
36 	}
37 
38 	/* read eeprom config section */
39 	if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
40 		puts("EEPROM: Failed to read EEPROM\n");
41 		info->model[0] = 0;
42 		return GW_UNKNOWN;
43 	}
44 
45 	/* sanity checks */
46 	if (info->model[0] != 'G' || info->model[1] != 'W') {
47 		puts("EEPROM: Invalid Model in EEPROM\n");
48 		info->model[0] = 0;
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 		info->model[0] = 0;
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 	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 	default:
84 		printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
85 		type = GW_UNKNOWN;
86 		break;
87 	}
88 	return type;
89 }
90