summaryrefslogtreecommitdiff
path: root/board/phytec/common/imx93_som_detection.c
blob: eb9574d43b5871945948a906048feb537a158724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2024 PHYTEC Messtechnik GmbH
 * Author: Primoz Fiser <primoz.fiser@norik.com>
 */

#include <asm/arch/sys_proto.h>
#include <dm/device.h>
#include <dm/uclass.h>
#include <i2c.h>
#include <u-boot/crc.h>

#include "imx93_som_detection.h"

extern struct phytec_eeprom_data eeprom_data;

#if IS_ENABLED(CONFIG_PHYTEC_IMX93_SOM_DETECTION)

/* Check if the SoM is actually one of the following products:
 * - i.MX93
 *
 * Returns 0 in case it's a known SoM. Otherwise, returns 1.
 */
u8 __maybe_unused phytec_imx93_detect(struct phytec_eeprom_data *data)
{
	u8 som;

	if (!data)
		data = &eeprom_data;

	/* Early API revisions are not supported */
	if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
		return 1;

	som = data->payload.data.data_api2.som_no;
	debug("%s: som id: %u\n", __func__, som);

	if (som == PHYTEC_IMX93_SOM && is_imx93())
		return 0;

	pr_err("%s: SoM ID does not match. Wrong EEPROM data?\n", __func__);
	return 1;
}

/*
 * Filter PHYTEC i.MX93 SoM options by option index
 *
 * Returns:
 *  - option value
 *  - PHYTEC_EEPROM_INVAL when the data is invalid
 *
 */
u8 __maybe_unused phytec_imx93_get_opt(struct phytec_eeprom_data *data,
				       enum phytec_imx93_option_index idx)
{
	char *opt;
	u8 opt_id;

	if (!data)
		data = &eeprom_data;

	if (!data->valid || data->payload.api_rev < PHYTEC_API_REV2)
		return PHYTEC_EEPROM_INVAL;

	opt = phytec_get_opt(data);
	if (opt)
		opt_id = PHYTEC_GET_OPTION(opt[idx]);
	else
		opt_id = PHYTEC_EEPROM_INVAL;

	debug("%s: opt[%d] id: %u\n", __func__, idx, opt_id);
	return opt_id;
}

/*
 * Filter PHYTEC i.MX93 SoM voltage
 *
 * Returns:
 *  - PHYTEC_IMX93_VOLTAGE_1V8 or PHYTEC_IMX93_VOLTAGE_3V3
 *  - PHYTEC_EEPROM_INVAL when the data is invalid
 *
 */
enum phytec_imx93_voltage __maybe_unused phytec_imx93_get_voltage(struct phytec_eeprom_data *data)
{
	u8 option = phytec_imx93_get_opt(data, PHYTEC_IMX93_OPT_FEAT);

	if (option == PHYTEC_EEPROM_INVAL)
		return PHYTEC_IMX93_VOLTAGE_INVALID;
	return (option & 0x01) ? PHYTEC_IMX93_VOLTAGE_1V8 : PHYTEC_IMX93_VOLTAGE_3V3;
}

#else

inline u8 __maybe_unused phytec_imx93_detect(struct phytec_eeprom_data *data)
{
	return 1;
}

inline u8 __maybe_unused phytec_imx93_get_opt(struct phytec_eeprom_data *data,
					      enum phytec_imx93_option_index idx)
{
	return PHYTEC_EEPROM_INVAL;
}

inline enum phytec_imx93_voltage __maybe_unused phytec_imx93_get_voltage
	(struct phytec_eeprom_data *data)
{
	return PHYTEC_EEPROM_INVAL;
}

#endif /* IS_ENABLED(CONFIG_PHYTEC_IMX93_SOM_DETECTION) */