summaryrefslogtreecommitdiff
path: root/src/mainboard/google/fizz/mainboard.c
blob: e6a0296274d3d8d30b685dd4e78f0a7852a17a65 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2017 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <arch/acpi.h>
#include <console/console.h>
#include <chip.h>
#include <device/device.h>
#include <ec/ec.h>
#include <ec/google/chromeec/ec.h>
#include <gpio.h>
#include <mainboard/google/fizz/gpio.h>
#include <smbios.h>
#include <soc/gpio.h>
#include <soc/pci_devs.h>
#include <soc/nhlt.h>
#include <string.h>
#include <vendorcode/google/chromeos/chromeos.h>

#define FIZZ_SKU_ID_I7_U42  0x4
#define FIZZ_SKU_ID_I5_U42  0x5
#define FIZZ_SKU_ID_I3_U42  0x6
#define FIZZ_SKU_ID_I7_U22  0x3
#define FIZZ_SKU_ID_I5_U22  0x2
#define FIZZ_SKU_ID_I3_U22  0x1
#define FIZZ_SKU_ID_CEL_U22 0x0
#define FIZZ_PL2_U42        44
#define FIZZ_PL2_U22        29
#define FIZZ_PSYSPL2_U22    65
#define FIZZ_PSYSPL2_U42    90
#define FIZZ_MAX_TIME_WINDOW 6
#define FIZZ_MIN_DUTYCYCLE   4
/*
 * For type-C chargers, set PL2 to 90% of max power to account for
 * cable loss and FET Rdson loss in the path from the source.
 */
#define SET_PSYSPL2(w)     (9 * (w) / 10)

#define OEM_ID_COUNT	3
#define SKU_ID_COUNT	7

/* List of BJ adapters shipped with Fizz or its variants */
enum bj_adapter {
	BJ_UNKNOWN,
	BJ_65W_19V,
	BJ_90W_19V,
	BJ_65W_19P5V,
	BJ_90W_19P5V,
	BJ_COUNT,
};

/* BJ adapter specs */
static const struct {
	uint16_t current_lim; /* in mA */
	uint16_t voltage_lim; /* in mV */
} bj_adapters[] = {
	[BJ_65W_19V] = { .current_lim = 3420, .voltage_lim = 19000 },
	[BJ_90W_19V] = { .current_lim = 4740, .voltage_lim = 19000 },
	[BJ_65W_19P5V] = { .current_lim = 3330, .voltage_lim = 19500 },
	[BJ_90W_19P5V] = { .current_lim = 4620, .voltage_lim = 19500 },
};

/*
 * The table showing which device is shipped with which BJ adapter.
 *
 *        | SKU0     SKU1     ...
 *   OEM0 | AdapterX AdapterZ ...
 *   OEM1 | AdapterY ...
 *   ...  |
 */
static const enum bj_adapter bj_adapter_table[OEM_ID_COUNT][SKU_ID_COUNT] = {
	{ BJ_65W_19P5V, BJ_65W_19P5V, BJ_90W_19P5V, BJ_90W_19P5V,
			BJ_90W_19P5V, BJ_90W_19P5V, BJ_65W_19P5V },
	{ BJ_65W_19V, BJ_65W_19V, BJ_UNKNOWN, BJ_UNKNOWN,
			BJ_90W_19V, BJ_90W_19V, BJ_UNKNOWN },
	{ BJ_65W_19V, BJ_65W_19V, BJ_90W_19V, BJ_90W_19V,
			BJ_90W_19V, BJ_90W_19V, BJ_65W_19V },
};

static uint8_t read_sku_id_from_gpio(void)
{
	const gpio_t sku_id_gpios[] = {
		GPIO_SKU_ID0,
		GPIO_SKU_ID1,
		GPIO_SKU_ID2,
		GPIO_SKU_ID3,
	};
	return gpio_base2_value(sku_id_gpios, ARRAY_SIZE(sku_id_gpios));
}

static uint8_t board_sku_id(void)
{
	static int sku_id = -1;

	if (sku_id < 0) {
		uint32_t id;
		if (google_chromeec_cbi_get_sku_id(&id))
			/* TODO: Once transition completes, raise error instead
			   of returning gpio value which could be unintended. */
			/* Reading from EC may succeed next time but we do not
			   want to return different values. So, we cache the
			   value read from GPIOs. */
			id = read_sku_id_from_gpio();
		sku_id = id;
	}

	return sku_id;
}

/*
 * mainboard_set_power_limits
 *
 * Set Pl2 and SysPl2 values based on detected charger.
 * If detected barrel jack, use values below based on SKU.
 * definitions:
 * x = no value entered. Use default value in parenthesis.
 *     will set 0 to anything that shouldn't be set.
 * n = max value of power adapter.
 * +-------------+-----+---------+-----------+-------+
 * | sku_id      | PL2 | PsysPL2 |  PsysPL3  |  PL4  |
 * +-------------+-----+---------+-----------+-------+
 * | i7 U42      |  44 |   81    | x(.85PL4) | x(71) |
 * | i5 U42      |  44 |   81    | x(.85PL4) | x(71) |
 * | i3 U42      |  44 |   81    | x(.85PL4) | x(71) |
 * | i7 U22      |  29 |   58    | x(.85PL4) | x(43) |
 * | i5 U22      |  29 |   58    | x(.85PL4) | x(43) |
 * | i3 U22      |  29 |   58    | x(.85PL4) | x(43) |
 * | celeron U22 |  29 |   58    | x(.85PL4) | x(43) |
 * +-------------+-----+---------+-----------+-------+
 * For USB C charger:
 * +-------------+-----+---------+---------+-------+
 * | Max Power(W)| PL2 | PsysPL2 | PsysPL3 |  PL4  |
 * +-------------+-----+---------+---------+-------+
 * | 60 (U42)    |  44 |   54    |    54   |   54  |
 * | 60 (U22)    |  29 |   54    |    54   | x(43) |
 * | n  (U42)    |  44 |   .9n   |   .9n   |  .9n  |
 * | n  (U22)    |  29 |   .9n   |   .9n   | x(43) |
 * +-------------+-----+---------+---------+-------+
 */
static void mainboard_set_power_limits(config_t *conf)
{
	enum usb_chg_type type;
	u32 watts;
	u32 pl2, psyspl2;
	int rv = google_chromeec_get_usb_pd_power_info(&type, &watts);
	uint8_t sku = board_sku_id();
	const uint32_t u42_mask = (1 << FIZZ_SKU_ID_I7_U42) |
				  (1 << FIZZ_SKU_ID_I5_U42) |
				  (1 << FIZZ_SKU_ID_I3_U42);

	/* PL2 value is sku-based, no matter what charger we are using */
	pl2 = FIZZ_PL2_U22;
	if ((1 << sku) & u42_mask)
		pl2 = FIZZ_PL2_U42;
	conf->tdp_psyspl3 = conf->tdp_pl4 = 0;

	/* If we can't get charger info or not PD charger, assume barrel jack */
	if (rv != 0 || type != USB_CHG_TYPE_PD) {
		/* using the barrel jack, get PsysPL2 based on sku id */
		psyspl2 = FIZZ_PSYSPL2_U22;
		/* Running a U42 SKU */
		if ((1 << sku) & u42_mask)
			psyspl2 = FIZZ_PSYSPL2_U42;
	} else {
		/* Detected TypeC.  Base on max value of adapter */
		psyspl2 = watts;
		conf->tdp_psyspl3 = SET_PSYSPL2(psyspl2);
		/* set max possible time window */
		conf->tdp_psyspl3_time = FIZZ_MAX_TIME_WINDOW;
		/* set minimum duty cycle */
		conf->tdp_psyspl3_dutycycle = FIZZ_MIN_DUTYCYCLE;
		if ((1 << sku) & u42_mask)
			conf->tdp_pl4 = SET_PSYSPL2(psyspl2);
	}

	conf->tdp_pl2_override = pl2;
	/* set psyspl2 to 90% of max adapter power */
	conf->tdp_psyspl2 = SET_PSYSPL2(psyspl2);
}

static uint8_t read_oem_id_from_gpio(void)
{
	const gpio_t oem_id_gpios[] = {
		GPIO_OEM_ID1,
		GPIO_OEM_ID2,
		GPIO_OEM_ID3,
	};
	return gpio_base2_value(oem_id_gpios, ARRAY_SIZE(oem_id_gpios));
}

static uint8_t board_oem_id(void)
{
	static int oem_id = -1;

	if (oem_id < 0) {
		uint32_t id;
		if (google_chromeec_cbi_get_oem_id(&id))
			/* TODO: Once transition completes, raise error instead
			   of returning gpio value which could be unintended. */
			/* Reading from EC may succeed next time but we do not
			   want to return different values. So, we cache the
			   value read from GPIOs. */
			id = read_oem_id_from_gpio();
		oem_id = id;
	}

	return oem_id;
}

const char *smbios_mainboard_sku(void)
{
	static char sku_str[5]; /* sku{0..7} */

	snprintf(sku_str, sizeof(sku_str), "sku%d", board_oem_id());

	return sku_str;
}

static void mainboard_init(device_t dev)
{
	mainboard_ec_init();
}

static unsigned long mainboard_write_acpi_tables(
	device_t device, unsigned long current, acpi_rsdp_t *rsdp)
{
	const char *oem_id = "GOOGLE";
	const char *oem_table_id = "FIZZ";
	uintptr_t start_addr;
	uintptr_t end_addr;
	struct nhlt *nhlt;

	start_addr = current;

	nhlt = nhlt_init();
	if (!nhlt)
		return start_addr;

	/* RT5663 Headset codec */
	if (nhlt_soc_add_rt5663(nhlt, AUDIO_LINK_SSP1))
		printk(BIOS_ERR, "Couldn't add headset codec.\n");

	end_addr = nhlt_soc_serialize_oem_overrides(nhlt, start_addr,
				oem_id, oem_table_id, 0);

	if (end_addr != start_addr)
		acpi_add_table(rsdp, (void *)start_addr);

	return end_addr;
}

/*
 * Set max current and voltage for a barrel jack adapter based on {OEM, SKU}.
 * If this fails, the limit will remain unchanged = default values, which make
 * the system run under safe but under-rated power.
 * If a BJ adapter isn't plugged, this is a no-op.
 */
static void set_bj_adapter_limit(void)
{
	uint8_t oem = board_oem_id();
	uint8_t sku = board_sku_id();
	enum bj_adapter bj;

	printk(BIOS_INFO, "OEM:%u(0x%x) SKU:%u(0x%x)\n", oem, oem, sku, sku);
	if (oem >= OEM_ID_COUNT || sku >= SKU_ID_COUNT) {
		printk(BIOS_ERR, "Unrecognized OEM or SKU\n");
		return;
	}

	bj = bj_adapter_table[oem][sku];
	if (bj <= BJ_UNKNOWN || BJ_COUNT <= bj) {
		printk(BIOS_ERR, "Invalid BJ adapter ID: %d\n", bj);
		return;
	}
	printk(BIOS_INFO, "Setting BJ limit: %dmA/%dmV\n",
	       bj_adapters[bj].current_lim, bj_adapters[bj].voltage_lim);
	if (google_chromeec_override_dedicated_charger_limit(
			bj_adapters[bj].current_lim,
			bj_adapters[bj].voltage_lim))
		printk(BIOS_ERR, "Failed to set BJ limit\n");
}

static void mainboard_enable(device_t dev)
{
	device_t root = SA_DEV_ROOT;
	config_t *conf = root->chip_info;

	mainboard_set_power_limits(conf);

	set_bj_adapter_limit();

	dev->ops->init = mainboard_init;
	dev->ops->acpi_inject_dsdt_generator = chromeos_dsdt_generator;
	dev->ops->write_acpi_tables = mainboard_write_acpi_tables;
}

struct chip_operations mainboard_ops = {
	.enable_dev = mainboard_enable,
};