Prepare v2024.10
[platform/kernel/u-boot.git] / board / k+p / kp_imx53 / kp_id_rev.c
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 <env.h>
13 #include <i2c.h>
14 #include "kp_id_rev.h"
15 #include <net.h>
16 #include <vsprintf.h>
17
18 static int eeprom_has_been_read;
19 static struct id_eeprom eeprom;
20
21 void show_eeprom(void)
22 {
23         char safe_string[33];
24         int i;
25         u8 *p;
26
27         puts("Module EEPROM:\n");
28         /* ID */
29         for (i = 0; i <= sizeof(eeprom.id) && 0xff != eeprom.id[i]; ++i)
30                 safe_string[i] = eeprom.id[i];
31         safe_string[i] = '\0';
32
33         if (!strncmp(safe_string, "TQM", 3)) {
34                 printf("  ID: %s\n", safe_string);
35                 env_set("boardtype", safe_string);
36         } else {
37                 puts("  unknown hardware variant\n");
38         }
39
40         /* Serial number */
41         for (i = 0; (sizeof(eeprom.serial) >= i) &&
42                     (eeprom.serial[i] >= 0x30) &&
43                     (eeprom.serial[i] <= 0x39); ++i)
44                 safe_string[i] = eeprom.serial[i];
45         safe_string[i] = '\0';
46
47         if (strlen(safe_string) == 8) {
48                 printf("  SN: %s\n", safe_string);
49                 env_set("serial#", safe_string);
50         } else {
51                 puts("  unknown serial number\n");
52         }
53
54         /* MAC address  */
55         p = eeprom.mac;
56         if (!is_valid_ethaddr(p)) {
57                 printf("  Not valid ETH EEPROM addr!\n");
58                 return;
59         }
60
61         printf("  MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
62                p[0], p[1], p[2], p[3], p[4], p[5]);
63
64         eth_env_set_enetaddr("ethaddr", p);
65 }
66
67 #define I2C_EEPROM_BUS_NUM      1
68 #define I2C_EEPROM_ADDR         0x50
69 #define I2C_EEPROM_ADDR_LEN     2
70
71 int read_eeprom(void)
72 {
73         struct udevice *dev;
74         int ret;
75
76         if (eeprom_has_been_read)
77                 return 0;
78
79         ret = i2c_get_chip_for_busnum(I2C_EEPROM_BUS_NUM, I2C_EEPROM_ADDR,
80                                       I2C_EEPROM_ADDR_LEN, &dev);
81         if (ret) {
82                 printf("Cannot find EEPROM !\n");
83                 return ret;
84         }
85
86         ret = dm_i2c_read(dev, 0x0, (uchar *)&eeprom, sizeof(eeprom));
87
88         eeprom_has_been_read = (ret == 0) ? 1 : 0;
89         return ret;
90 }
91
92 int read_board_id(void)
93 {
94         unsigned char rev_id = 0x42;
95         char rev_str[32], buf[8];
96         struct udevice *dev;
97         int ret;
98
99         ret = i2c_get_chip_for_busnum(2, 0x22, 1, &dev);
100         if (ret) {
101                 printf("Cannot find pcf8574 IO expander !\n");
102                 return ret;
103         }
104
105         dm_i2c_read(dev, 0x0, &rev_id, sizeof(rev_id));
106
107         sprintf(rev_str, "%02X", rev_id);
108         if (rev_id & 0x80) {
109                 printf("BBoard:4x00 Rev:%s\n", rev_str);
110                 env_set("boardtype", "ddc");
111                 env_set("fit_config", "imx53_kb_conf");
112         } else {
113                 printf("BBoard:40x0 Rev:%s\n", rev_str);
114                 env_set("boardtype", "hsc");
115                 env_set("fit_config", "imx53_kb_40x0_conf");
116         }
117
118         sprintf(buf, "kp-%s", env_get("boardtype"));
119         env_set("boardname", buf);
120         env_set("boardsoc", "imx53");
121         env_set("kb53_rev", rev_str);
122
123         return 0;
124 }