summaryrefslogtreecommitdiff
path: root/src/mainboard/google/corsola/boardid.c
blob: a0c08abb5470ab69a0f41ef2e37f166d5cc53e74 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* SPDX-License-Identifier: GPL-2.0-only */

#include <assert.h>
#include <boardid.h>
#include <console/console.h>
#include <ec/google/chromeec/ec.h>
#include <soc/auxadc.h>

#include "display.h"

/* board_id is provided by ec/google/chromeec/ec_boardid.c */

#define ADC_LEVELS 12

enum {
	/* RAM IDs */
	RAM_ID_LOW_CHANNEL = 2,
	RAM_ID_HIGH_CHANNEL = 3,
	/* SKU IDs */
	SKU_ID_LOW_CHANNEL = 4,
	SKU_ID_HIGH_CHANNEL = 5,
};

static const unsigned int lcm_voltages[ADC_LEVELS] = {
	/* ID : Voltage (unit: uV) */
	[0]  =        0,
	[1]  =   283000,
	[2]  =   394000,
	[3]  =   490000,
	[4]  =   640000,
	[5]  =   712000,
	[6]  =   800000,
	[7]  =   867000,
	[8]  =   960000,
	[9]  =  1070000,
	[10] =  1190000,
	[11] =  1434000,
};

static const unsigned int ram_voltages[ADC_LEVELS] = {
	/* ID : Voltage (unit: uV) */
	[0]  =   74300,
	[1]  =  211700,
	[2]  =  318800,
	[3]  =  428600,
	[4]  =  541700,
	[5]  =  665800,
	[6]  =  781400,
	[7]  =  900000,
	[8]  = 1023100,
	[9]  = 1137000,
	[10] = 1240000,
	[11] = 1342600,
};

static const unsigned int *adc_voltages[] = {
	[RAM_ID_LOW_CHANNEL] = ram_voltages,
	[RAM_ID_HIGH_CHANNEL] = ram_voltages,
	[SKU_ID_LOW_CHANNEL] = ram_voltages,
	[SKU_ID_HIGH_CHANNEL] = ram_voltages,
};

static const unsigned int *adc_voltages_detachable[] = {
	[RAM_ID_LOW_CHANNEL] = ram_voltages,
	[RAM_ID_HIGH_CHANNEL] = ram_voltages,
	[SKU_ID_LOW_CHANNEL] = ram_voltages,
	[SKU_ID_HIGH_CHANNEL] = lcm_voltages,
};

static uint32_t get_adc_index(unsigned int channel)
{
	unsigned int value = auxadc_get_voltage_uv(channel);
	const unsigned int *voltages;

	if (CONFIG(BOARD_GOOGLE_STARYU_COMMON)) {
		assert(channel < ARRAY_SIZE(adc_voltages_detachable));
		voltages = adc_voltages_detachable[channel];
	} else {
		assert(channel < ARRAY_SIZE(adc_voltages));
		voltages = adc_voltages[channel];
	}

	assert(voltages);

	/* Find the closest voltage */
	uint32_t id;
	for (id = 0; id < ADC_LEVELS - 1; id++)
		if (value < (voltages[id] + voltages[id + 1]) / 2)
			break;

	printk(BIOS_DEBUG, "ADC[%u]: Raw value=%u ID=%u\n", channel, value, id);
	return id;
}

/* Detachables use ADC channel 5 for panel ID */
uint32_t panel_id(void)
{
	static uint32_t cached_panel_id = BOARD_ID_INIT;

	if (cached_panel_id == BOARD_ID_INIT)
		cached_panel_id = get_adc_index(SKU_ID_HIGH_CHANNEL);

	return cached_panel_id;
}

uint32_t sku_id(void)
{
	static uint32_t cached_sku_code = BOARD_ID_INIT;

	if (cached_sku_code != BOARD_ID_INIT)
		return cached_sku_code;

	cached_sku_code = google_chromeec_get_board_sku();

	if (CONFIG(BOARD_GOOGLE_STARYU_COMMON)) {
		if (cached_sku_code == CROS_SKU_UNPROVISIONED ||
		    cached_sku_code == CROS_SKU_UNKNOWN) {
			printk(BIOS_WARNING, "SKU code from EC: %s\n",
			       (cached_sku_code == CROS_SKU_UNKNOWN) ?
			       "CROS_SKU_UNKNOWN" : "CROS_SKU_UNPROVISIONED");
			/* Reserve last 4 bits to report PANEL_ID */
			cached_sku_code = 0x7FFFFFF0UL | panel_id();
		}
	} else if (cached_sku_code == CROS_SKU_UNKNOWN) {
		printk(BIOS_WARNING, "Failed to get SKU code from EC\n");
		cached_sku_code = (get_adc_index(SKU_ID_HIGH_CHANNEL) << 4 |
				   get_adc_index(SKU_ID_LOW_CHANNEL));
	}

	printk(BIOS_DEBUG, "SKU Code: %#02x\n", cached_sku_code);
	return cached_sku_code;
}

uint32_t ram_code(void)
{
	static uint32_t cached_ram_code = BOARD_ID_INIT;

	if (cached_ram_code == BOARD_ID_INIT) {
		cached_ram_code = (get_adc_index(RAM_ID_HIGH_CHANNEL) << 4 |
				   get_adc_index(RAM_ID_LOW_CHANNEL));
		printk(BIOS_DEBUG, "RAM Code: %#02x\n", cached_ram_code);
	}

	return cached_ram_code;
}