xref: /openbmc/u-boot/board/k+p/kp_imx53/kp_id_rev.c (revision 7da7ff54)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018
4  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5  *
6  * Based on code developed by:
7  *
8  * Copyright (C) 2012 TQ-Systems GmbH
9  * Daniel Gericke <daniel.gericke@tqs.de>
10  */
11 
12 #include <common.h>
13 #include <environment.h>
14 #include <i2c.h>
15 #include "kp_id_rev.h"
16 
17 static int eeprom_has_been_read;
18 static struct id_eeprom eeprom;
19 
20 void show_eeprom(void)
21 {
22 	char safe_string[33];
23 	int i;
24 	u8 *p;
25 
26 	puts("Module EEPROM:\n");
27 	/* ID */
28 	for (i = 0; i <= sizeof(eeprom.id) && 0xff != eeprom.id[i]; ++i)
29 		safe_string[i] = eeprom.id[i];
30 	safe_string[i] = '\0';
31 
32 	if (!strncmp(safe_string, "TQM", 3)) {
33 		printf("  ID: %s\n", safe_string);
34 		env_set("boardtype", safe_string);
35 	} else {
36 		puts("  unknown hardware variant\n");
37 	}
38 
39 	/* Serial number */
40 	for (i = 0; (sizeof(eeprom.serial) >= i) &&
41 		    (eeprom.serial[i] >= 0x30) &&
42 		    (eeprom.serial[i] <= 0x39); ++i)
43 		safe_string[i] = eeprom.serial[i];
44 	safe_string[i] = '\0';
45 
46 	if (strlen(safe_string) == 8) {
47 		printf("  SN: %s\n", safe_string);
48 		env_set("serial#", safe_string);
49 	} else {
50 		puts("  unknown serial number\n");
51 	}
52 
53 	/* MAC address  */
54 	p = eeprom.mac;
55 	if (!is_valid_ethaddr(p)) {
56 		printf("  Not valid ETH EEPROM addr!\n");
57 		return;
58 	}
59 
60 	printf("  MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
61 	       p[0], p[1], p[2], p[3], p[4], p[5]);
62 
63 	eth_env_set_enetaddr("ethaddr", p);
64 }
65 
66 int read_eeprom(void)
67 {
68 	struct udevice *dev;
69 	int ret;
70 
71 	if (eeprom_has_been_read)
72 		return 0;
73 
74 	ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
75 				      CONFIG_SYS_I2C_EEPROM_ADDR,
76 				      CONFIG_SYS_I2C_EEPROM_ADDR_LEN, &dev);
77 	if (ret) {
78 		printf("Cannot find EEPROM !\n");
79 		return ret;
80 	}
81 
82 	ret = dm_i2c_read(dev, 0x0, (uchar *)&eeprom, sizeof(eeprom));
83 
84 	eeprom_has_been_read = (ret == 0) ? 1 : 0;
85 	return ret;
86 }
87 
88 int read_board_id(void)
89 {
90 	unsigned char rev_id = 0x42;
91 	char rev_str[32], buf[8];
92 	struct udevice *dev;
93 	int ret;
94 
95 	ret = i2c_get_chip_for_busnum(2, 0x22, 1, &dev);
96 	if (ret) {
97 		printf("Cannot find pcf8574 IO expander !\n");
98 		return ret;
99 	}
100 
101 	dm_i2c_read(dev, 0x0, &rev_id, sizeof(rev_id));
102 
103 	sprintf(rev_str, "%02X", rev_id);
104 	if (rev_id & 0x80) {
105 		printf("BBoard:4x00 Rev:%s\n", rev_str);
106 		env_set("boardtype", "ddc");
107 		env_set("fit_config", "imx53_kb_conf");
108 	} else {
109 		printf("BBoard:40x0 Rev:%s\n", rev_str);
110 		env_set("boardtype", "hsc");
111 		env_set("fit_config", "imx53_kb_40x0_conf");
112 	}
113 
114 	sprintf(buf, "kp-%s", env_get("boardtype"));
115 	env_set("boardname", buf);
116 	env_set("boardsoc", "imx53");
117 	env_set("kb53_rev", rev_str);
118 
119 	return 0;
120 }
121