summaryrefslogtreecommitdiff
path: root/driver/gl3590.c
blob: 03af496293db047f1344ff93f15d073787306925 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "console.h"
#include "gl3590.h"
#include "hooks.h"
#include "i2c.h"
#include "pwr_defs.h"
#include "system.h"
#include "util.h"

#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)
#define CPRINTF(format, args...) cprintf(CC_I2C, format, ##args)

/* GL3590 is unique in terms of i2c_read, since it doesn't support repeated
 * start sequence. One need to issue two separate transactions - first is write
 * with a register offset, then after a delay second transaction is actual read.
 */
int gl3590_read(int hub, uint8_t reg, uint8_t *data, int count)
{
	int rv;
	struct uhub_i2c_iface_t *uhub_p = &uhub_config[hub];

	i2c_lock(uhub_p->i2c_host_port, 1);
	rv = i2c_xfer_unlocked(uhub_p->i2c_host_port, uhub_p->i2c_addr, &reg, 1,
			       NULL, 0, I2C_XFER_SINGLE);
	i2c_lock(uhub_p->i2c_host_port, 0);

	if (rv)
		return rv;

	/* GL3590 requires at least 1ms between consecutive i2c transactions */
	udelay(MSEC);

	i2c_lock(uhub_p->i2c_host_port, 1);
	rv = i2c_xfer_unlocked(uhub_p->i2c_host_port, uhub_p->i2c_addr, NULL, 0,
			       data, count, I2C_XFER_SINGLE);
	i2c_lock(uhub_p->i2c_host_port, 0);

	/*
	 * GL3590 requires at least 1ms between consecutive i2c transactions.
	 * Make sure that we are safe across API calls.
	 */
	udelay(MSEC);

	return rv;
};

int gl3590_write(int hub, uint8_t reg, uint8_t *data, int count)
{
	int rv;
	uint8_t buf[5];
	struct uhub_i2c_iface_t *uhub_p = &uhub_config[hub];

	/* GL3590 registers accept 4 bytes at max */
	if (count > (sizeof(buf) - 1)) {
		ccprintf("Too many bytes to write");
		return EC_ERROR_INVAL;
	}

	buf[0] = reg;
	memcpy(&buf[1], data, count);

	i2c_lock(uhub_p->i2c_host_port, 1);
	rv = i2c_xfer_unlocked(uhub_p->i2c_host_port, uhub_p->i2c_addr, buf,
			       count + 1, NULL, 0, I2C_XFER_SINGLE);
	i2c_lock(uhub_p->i2c_host_port, 0);

	/*
	 * GL3590 requires at least 1ms between consecutive i2c transactions.
	 * Make sure that we are safe across API calls.
	 */
	udelay(MSEC);

	return rv;
}

/*
 * Basic initialization of GL3590 I2C interface.
 *
 * Please note, that I2C interface is online not earlier than ~50ms after
 * RESETJ# is deasserted. Platform should check that PGREEN_A_SMD pin is
 * asserted. This init function shouldn't be invoked until that time.
 */
void gl3590_init(int hub)
{
	uint8_t tmp;
	struct uhub_i2c_iface_t *uhub_p = &uhub_config[hub];

	if (uhub_p->initialized)
		return;

	if (gl3590_read(hub, GL3590_HUB_MODE_REG, &tmp, 1)) {
		CPRINTF("GL3590: Cannot read HUB_MODE register");
		return;
	}
	if ((tmp & GL3590_HUB_MODE_I2C_READY) == 0)
		CPRINTF("GL3590 interface isn't ready, consider deferring "
			"this init\n");

	/* Deassert INTR# signal */
	tmp = GL3590_INT_CLEAR;
	if (gl3590_write(hub, GL3590_INT_REG, &tmp, 1)) {
		CPRINTF("GL3590: Cannot write to INT register");
		return;
	};

	uhub_p->initialized = 1;
}

/*
 * GL3590 chip may drive I2C_SDA and I2C_SCL lines for 200ms (max) after it is
 * released from reset (through gpio de-assertion in main()). In order to avoid
 * broken I2C transactions, we need to add an extra delay before any activity on
 * the I2C bus in the system.
 */
static void gl3590_delay_on_init(void)
{
	CPRINTS("Applying 200ms delay for GL3590 to release I2C lines");
	udelay(200 * MSEC);
}
DECLARE_HOOK(HOOK_INIT, gl3590_delay_on_init, HOOK_PRIO_INIT_I2C - 1);

void gl3590_irq_handler(int hub)
{
	uint8_t buf = 0;
	uint8_t res_reg[2];
	struct uhub_i2c_iface_t *uhub_p = &uhub_config[hub];

	if (!uhub_p->initialized)
		return;

	/* Verify that irq is pending */
	if (gl3590_read(hub, GL3590_INT_REG, &buf, sizeof(buf))) {
		ccprintf("Cannot read from the host hub i2c\n");
		goto exit;
	}

	if ((buf & GL3590_INT_PENDING) == 0) {
		ccprintf("Invalid hub event\n");
		goto exit;
	}

	/* Get the hub event reason */
	if (gl3590_read(hub, GL3590_RESPONSE_REG, res_reg, sizeof(res_reg))) {
		ccprintf("Cannot read from the host hub i2c\n");
		goto exit;
	}

	if ((res_reg[0] & GL3590_RESPONSE_REG_SYNC_MASK) == 0)
		ccprintf("Host hub response: ");
	else
		ccprintf("Host hub event! ");

	switch (res_reg[0]) {
	case 0x0:
		ccprintf("No response");
		break;
	case 0x1:
		ccprintf("Successful");
		break;
	case 0x2:
		ccprintf("Invalid command");
		break;
	case 0x3:
		ccprintf("Invalid arguments");
		break;
	case 0x4:
		ccprintf("Invalid port: %d", res_reg[1]);
		break;
	case 0x5:
		ccprintf("Command not completed");
		break;
	case 0x80:
		ccprintf("Reset complete");
		break;
	case 0x81:
		ccprintf("Power operation mode change");
		break;
	case 0x82:
		ccprintf("Connect change");
		break;
	case 0x83:
		ccprintf("Error on the specific port");
		break;
	case 0x84:
		ccprintf("Hub state change");
		break;
	case 0x85:
		ccprintf("SetFeature PORT_POWER failure");
		break;
	default:
		ccprintf("Unknown value: 0x%0x", res_reg[0]);
	}
	ccprintf("\n");

	if (res_reg[1])
		ccprintf("Affected port %d\n", res_reg[1]);

exit:
	/* Try to clear interrupt */
	buf = GL3590_INT_CLEAR;
	gl3590_write(hub, GL3590_INT_REG, &buf, sizeof(buf));
}

enum ec_error_list gl3590_ufp_pwr(int hub, struct pwr_con_t *pwr)
{
	uint8_t hub_sts, hub_mode;
	int rv = 0;
	struct uhub_i2c_iface_t *uhub_p = &uhub_config[hub];

	if (!uhub_p->initialized)
		return EC_ERROR_HW_INTERNAL;

	if (gl3590_read(hub, GL3590_HUB_STS_REG, &hub_sts, sizeof(hub_sts))) {
		CPRINTF("Error reading HUB_STS %d\n", rv);
		return EC_ERROR_BUSY;
	}

	pwr->volts = 5;

	switch ((hub_sts & GL3590_HUB_STS_HOST_PWR_MASK) >>
		GL3590_HUB_STS_HOST_PWR_SHIFT) {
	case GL3590_DEFAULT_HOST_PWR_SRC:
		if (gl3590_read(hub, GL3590_HUB_MODE_REG, &hub_mode,
				sizeof(hub_mode))) {
			CPRINTF("Error reading HUB_MODE %d\n", rv);
			return EC_ERROR_BUSY;
		}
		if (hub_mode & GL3590_HUB_MODE_USB3_EN) {
			pwr->milli_amps = 900;
			return EC_SUCCESS;
		} else if (hub_mode & GL3590_HUB_MODE_USB2_EN) {
			pwr->milli_amps = 500;
			return EC_SUCCESS;
		} else {
			CPRINTF("GL3590: Neither USB3 nor USB2 hubs "
				"configured\n");
			return EC_ERROR_HW_INTERNAL;
		}
	case GL3590_1_5_A_HOST_PWR_SRC:
		pwr->milli_amps = 1500;
		return EC_SUCCESS;
	case GL3590_3_0_A_HOST_PWR_SRC:
		pwr->milli_amps = 3000;
		return EC_SUCCESS;
	default:
		CPRINTF("GL3590: Unkown host power source %d\n", hub_sts);
		return EC_ERROR_UNKNOWN;
	}
}

#define GL3590_EN_PORT_MAX_RETRY_COUNT 10

int gl3590_enable_ports(int hub, uint8_t port_mask, bool enable)
{
	uint8_t buf[4] = { 0 };
	uint8_t en_mask = 0;
	uint8_t tmp;
	int rv, i;
	struct uhub_i2c_iface_t *uhub_p = &uhub_config[hub];

	if (!uhub_p->initialized)
		return EC_ERROR_HW_INTERNAL;

	if (!enable)
		en_mask = port_mask;

	buf[0] = en_mask;
	buf[2] = port_mask;

	for (i = 1; i <= GL3590_EN_PORT_MAX_RETRY_COUNT; i++) {
		rv = gl3590_write(hub, GL3590_PORT_DISABLED_REG, buf,
				  sizeof(buf));
		if (rv)
			return rv;

		usleep(200 * MSEC);

		/* Verify whether port is enabled/disabled */
		rv = gl3590_read(hub, GL3590_PORT_EN_STS_REG, &tmp, 1);
		if (rv)
			return rv;

		if (enable && ((tmp & port_mask) == port_mask))
			break;
		if (!enable && ((tmp & port_mask) == 0))
			break;

		if (i > GL3590_EN_PORT_MAX_RETRY_COUNT) {
			CPRINTF("GL3590: Failed to %s port 0x%x\n",
				enable ? "enable" : "disable", port_mask);
			return EC_ERROR_HW_INTERNAL;
		}

		CPRINTF("GL3590: Port %s retrying.. %d/%d\n"
			"Port status is 0x%x\n",
			enable ? "enable" : "disable", i,
			GL3590_EN_PORT_MAX_RETRY_COUNT, tmp);
	}

	return EC_SUCCESS;
}

#ifdef CONFIG_CMD_GL3590
static int command_gl3590(int argc, const char **argv)
{
	char *e;
	int port;

	if (argc < 2)
		return EC_ERROR_PARAM_COUNT;

	port = strtoi(argv[2], &e, 0);
	if (*e)
		return EC_ERROR_PARAM2;

	if (strcasecmp(argv[1], "enable") == 0) {
		if (!gl3590_enable_ports(0, port, 1))
			return EC_SUCCESS;
		else
			return EC_ERROR_HW_INTERNAL;
	} else if (strcasecmp(argv[1], "disable") == 0) {
		if (!gl3590_enable_ports(0, port, 0))
			return EC_SUCCESS;
		else
			return EC_ERROR_HW_INTERNAL;
	}

	return EC_ERROR_PARAM1;
}
DECLARE_CONSOLE_COMMAND(gl3590, command_gl3590,
			"<enable | disable> <port_bitmask>",
			"Manage GL3590 USB3.1 hub and its ports");
#endif /* CONFIG_CMD_GL3590 */