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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Plankton board configuration */
#include "adc.h"
#include "adc_chip.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "ina2xx.h"
#include "ioexpander_pca9534.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "usb_pd.h"
#include "usb_pd_config.h"
#include "util.h"
void button_event(enum gpio_signal signal);
void hpd_event(enum gpio_signal signal);
void vbus_event(enum gpio_signal signal);
#include "gpio_list.h"
static volatile uint64_t hpd_prev_ts;
static volatile int hpd_prev_level;
static volatile int hpd_possible_irq;
static int sn75dp130_dpcd_init(void);
/**
* Hotplug detect deferred task
*
* Called after level change on hpd GPIO to evaluate (and debounce) what event
* has occurred. There are 3 events that occur on HPD:
* 1. low : downstream display sink is deattached
* 2. high : downstream display sink is attached
* 3. irq : downstream display sink signalling an interrupt.
*
* The debounce times for these various events are:
* 100MSEC : min pulse width of level value.
* 2MSEC : min pulse width of IRQ low pulse. Max is level debounce min.
*
* lvl(n-2) lvl(n-1) lvl prev_delta now_delta event
* ----------------------------------------------------
* 1 0 1 <2ms n/a low glitch (ignore)
* 1 0 1 >2ms <100ms irq
* x 0 1 n/a >100ms high
* 0 1 0 <100ms n/a high glitch (ignore)
* x 1 0 n/a >100ms low
*/
void hpd_lvl_deferred(void)
{
int level = gpio_get_level(GPIO_DPSRC_HPD);
if (level != hpd_prev_level) {
/* Stable level changed. Send HPD event */
hpd_prev_level = level;
pd_send_hpd(0, level ? hpd_high : hpd_low);
/* Configure redriver's back side */
if (level)
sn75dp130_dpcd_init();
}
/* Send queued IRQ if the cable is attached */
if (hpd_possible_irq && level)
pd_send_hpd(0, hpd_irq);
hpd_possible_irq = 0;
}
DECLARE_DEFERRED(hpd_lvl_deferred);
void hpd_event(enum gpio_signal signal)
{
timestamp_t now = get_time();
int level = gpio_get_level(signal);
uint64_t cur_delta = now.val - hpd_prev_ts;
/* Record low pulse */
if (cur_delta >= HPD_DEBOUNCE_IRQ && level)
hpd_possible_irq = 1;
/* store current time */
hpd_prev_ts = now.val;
/* All previous hpd level events need to be re-triggered */
hook_call_deferred(hpd_lvl_deferred, HPD_DEBOUNCE_LVL);
}
/* Debounce time for voltage buttons */
#define BUTTON_DEBOUNCE_US (100 * MSEC)
static enum gpio_signal button_pressed;
static int fake_pd_disconnected;
static int fake_pd_host_mode;
static int fake_pd_disconnect_duration_ms;
enum usbc_action {
USBC_ACT_5V_TO_DUT,
USBC_ACT_12V_TO_DUT,
USBC_ACT_20V_TO_DUT,
USBC_ACT_DEVICE,
USBC_ACT_USBDP_TOGGLE,
USBC_ACT_USB_EN,
USBC_ACT_DP_EN,
USBC_ACT_MUX_FLIP,
USBC_ACT_CABLE_POLARITY0,
USBC_ACT_CABLE_POLARITY1,
/* Number of USBC actions */
USBC_ACT_COUNT
};
enum board_src_cap src_cap_mapping[USBC_ACT_COUNT] =
{
[USBC_ACT_5V_TO_DUT] = SRC_CAP_5V,
[USBC_ACT_12V_TO_DUT] = SRC_CAP_12V,
[USBC_ACT_20V_TO_DUT] = SRC_CAP_20V,
};
static void set_usbc_action(enum usbc_action act)
{
int need_soft_reset;
int was_usb_mode;
switch (act) {
case USBC_ACT_5V_TO_DUT:
case USBC_ACT_12V_TO_DUT:
case USBC_ACT_20V_TO_DUT:
need_soft_reset = gpio_get_level(GPIO_VBUS_CHARGER_EN);
board_set_source_cap(src_cap_mapping[act]);
pd_set_dual_role(PD_DRP_FORCE_SOURCE);
if (need_soft_reset)
pd_soft_reset();
break;
case USBC_ACT_DEVICE:
pd_set_dual_role(PD_DRP_FORCE_SINK);
break;
case USBC_ACT_USBDP_TOGGLE:
was_usb_mode = gpio_get_level(GPIO_USBC_SS_USB_MODE);
gpio_set_level(GPIO_USBC_SS_USB_MODE, !was_usb_mode);
gpio_set_level(GPIO_CASE_CLOSE_EN, !was_usb_mode);
break;
case USBC_ACT_USB_EN:
gpio_set_level(GPIO_USBC_SS_USB_MODE, 1);
break;
case USBC_ACT_DP_EN:
gpio_set_level(GPIO_USBC_SS_USB_MODE, 0);
break;
case USBC_ACT_MUX_FLIP:
pd_send_vdm(0, USB_VID_GOOGLE, VDO_CMD_FLIP, NULL, 0);
gpio_set_level(GPIO_USBC_POLARITY,
!gpio_get_level(GPIO_USBC_POLARITY));
break;
case USBC_ACT_CABLE_POLARITY0:
gpio_set_level(GPIO_USBC_POLARITY, 0);
break;
case USBC_ACT_CABLE_POLARITY1:
gpio_set_level(GPIO_USBC_POLARITY, 1);
break;
default:
break;
}
}
/* has Pull-up */
static int prev_dbg20v = 1;
static void button_dbg20v_deferred(void);
static void enable_dbg20v_poll(void)
{
hook_call_deferred(button_dbg20v_deferred, 10 * MSEC);
}
/* Handle debounced button press */
static void button_deferred(void)
{
if (button_pressed == GPIO_DBG_20V_TO_DUT_L) {
enable_dbg20v_poll();
if (gpio_get_level(GPIO_DBG_20V_TO_DUT_L) == prev_dbg20v)
return;
else
prev_dbg20v = !prev_dbg20v;
}
/* bounce ? */
if (gpio_get_level(button_pressed) != 0)
return;
switch (button_pressed) {
case GPIO_DBG_5V_TO_DUT_L:
set_usbc_action(USBC_ACT_5V_TO_DUT);
break;
case GPIO_DBG_12V_TO_DUT_L:
set_usbc_action(USBC_ACT_12V_TO_DUT);
break;
case GPIO_DBG_20V_TO_DUT_L:
set_usbc_action(USBC_ACT_20V_TO_DUT);
break;
case GPIO_DBG_CHG_TO_DEV_L:
set_usbc_action(USBC_ACT_DEVICE);
break;
case GPIO_DBG_USB_TOGGLE_L:
set_usbc_action(USBC_ACT_USBDP_TOGGLE);
break;
case GPIO_DBG_MUX_FLIP_L:
set_usbc_action(USBC_ACT_MUX_FLIP);
break;
default:
break;
}
ccprintf("Button %d = %d\n",
button_pressed, gpio_get_level(button_pressed));
}
DECLARE_DEFERRED(button_deferred);
void button_event(enum gpio_signal signal)
{
button_pressed = signal;
/* reset debounce time */
hook_call_deferred(button_deferred, BUTTON_DEBOUNCE_US);
}
static void button_dbg20v_deferred(void)
{
if (gpio_get_level(GPIO_DBG_20V_TO_DUT_L) == 0)
button_event(GPIO_DBG_20V_TO_DUT_L);
else
enable_dbg20v_poll();
}
DECLARE_DEFERRED(button_dbg20v_deferred);
void vbus_event(enum gpio_signal signal)
{
ccprintf("VBUS! =%d\n", gpio_get_level(signal));
task_wake(TASK_ID_PD);
}
/* ADC channels */
const struct adc_t adc_channels[] = {
/* USB PD CC lines sensing. Converted to mV (3300mV/4096). */
[ADC_CH_CC1_PD] = {"CC1_PD", 3300, 4096, 0, STM32_AIN(0)},
[ADC_CH_CC2_PD] = {"CC2_PD", 3300, 4096, 0, STM32_AIN(4)},
};
BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT);
/* I2C ports */
const struct i2c_port_t i2c_ports[] = {
{"master", I2C_PORT_MASTER, 100,
GPIO_MASTER_I2C_SCL, GPIO_MASTER_I2C_SDA},
};
const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports);
/* 8-bit address */
#define SN75DP130_I2C_ADDR 0x5c
/*
* Pin number for active-high reset from PCA9534 to CMOS pull-down to
* SN75DP130's RSTN (active-low)
*/
#define REDRIVER_RST_PIN 0x1
static int sn75dp130_i2c_write(uint8_t index, uint8_t value)
{
return i2c_write8(I2C_PORT_MASTER, SN75DP130_I2C_ADDR, index, value);
}
/**
* Reset redriver.
*
* Note, MUST set SW15 to 'PD' in order to control i2c from PD-MCU. This can
* NOT be done via software.
*/
static int sn75dp130_reset(void)
{
int rv;
rv = pca9534_config_pin(I2C_PORT_MASTER, 0x40, REDRIVER_RST_PIN,
PCA9534_OUTPUT);
/* Assert (its active high) */
rv |= pca9534_set_level(I2C_PORT_MASTER, 0x40, REDRIVER_RST_PIN, 1);
/* datasheet recommends > 100usec */
usleep(200);
/* De-assert */
rv |= pca9534_set_level(I2C_PORT_MASTER, 0x40, REDRIVER_RST_PIN, 0);
/* datasheet recommends > 400msec */
usleep(450 * MSEC);
return rv;
}
static int sn75dp130_dpcd_init(void)
{
int i, rv;
/* set upper & middle DPCD addr ... constant for writes below */
rv = sn75dp130_i2c_write(0x1c, 0x0);
rv |= sn75dp130_i2c_write(0x1d, 0x1);
/* link_bw_set: 5.4gbps */
rv |= sn75dp130_i2c_write(0x1e, 0x0);
rv |= sn75dp130_i2c_write(0x1f, 0x14);
/* lane_count_set: 4 */
rv |= sn75dp130_i2c_write(0x1e, 0x1);
rv |= sn75dp130_i2c_write(0x1f, 0x4);
/*
* Force Link voltage level & pre-emphasis by writing each of the lane's
* DPCD config registers 103-106h accordingly.
*/
for (i = 0x3; i < 0x7; i++) {
rv |= sn75dp130_i2c_write(0x1e, i);
rv |= sn75dp130_i2c_write(0x1f, 0x3);
}
return rv;
}
static int sn75dp130_redriver_init(void)
{
int rv;
rv = sn75dp130_reset();
/* Disable squelch detect */
rv |= sn75dp130_i2c_write(0x3, 0x1a);
/* Disable link training on re-driver source side */
rv |= sn75dp130_i2c_write(0x4, 0x0);
/* Can only configure DPCD portion of redriver in presence of an HPD */
if (gpio_get_level(GPIO_DPSRC_HPD))
sn75dp130_dpcd_init();
return rv;
}
static void board_init(void)
{
timestamp_t now = get_time();
hpd_prev_level = gpio_get_level(GPIO_DPSRC_HPD);
hpd_prev_ts = now.val;
gpio_enable_interrupt(GPIO_DPSRC_HPD);
/* Enable interrupts on VBUS transitions. */
gpio_enable_interrupt(GPIO_VBUS_WAKE);
/* Enable button interrupts. */
gpio_enable_interrupt(GPIO_DBG_5V_TO_DUT_L);
gpio_enable_interrupt(GPIO_DBG_12V_TO_DUT_L);
gpio_enable_interrupt(GPIO_DBG_CHG_TO_DEV_L);
gpio_enable_interrupt(GPIO_DBG_USB_TOGGLE_L);
gpio_enable_interrupt(GPIO_DBG_MUX_FLIP_L);
/* TODO(crosbug.com/33761): poll DBG_20V_TO_DUT_L */
enable_dbg20v_poll();
ina2xx_init(0, 0x399f, INA2XX_CALIB_1MA(10 /* mOhm */));
sn75dp130_redriver_init();
}
DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT);
static int cmd_usbc_action(int argc, char *argv[])
{
enum usbc_action act;
if (argc != 2)
return EC_ERROR_PARAM_COUNT;
if (!strcasecmp(argv[1], "5v"))
act = USBC_ACT_5V_TO_DUT;
else if (!strcasecmp(argv[1], "12v"))
act = USBC_ACT_12V_TO_DUT;
else if (!strcasecmp(argv[1], "20v"))
act = USBC_ACT_20V_TO_DUT;
else if (!strcasecmp(argv[1], "dev"))
act = USBC_ACT_DEVICE;
else if (!strcasecmp(argv[1], "usb"))
act = USBC_ACT_USB_EN;
else if (!strcasecmp(argv[1], "dp"))
act = USBC_ACT_DP_EN;
else if (!strcasecmp(argv[1], "flip"))
act = USBC_ACT_MUX_FLIP;
else if (!strcasecmp(argv[1], "pol0"))
act = USBC_ACT_CABLE_POLARITY0;
else if (!strcasecmp(argv[1], "pol1"))
act = USBC_ACT_CABLE_POLARITY1;
else
return EC_ERROR_PARAM1;
set_usbc_action(act);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(usbc_action, cmd_usbc_action,
"<5v|12v|20v|dev|usb|dp|flip|pol0|pol1>",
"Set Plankton type-C port state",
NULL);
int board_in_hub_mode(void)
{
int ret;
int level;
ret = pca9534_config_pin(I2C_PORT_MASTER, 0x40, 6, PCA9534_INPUT);
if (ret)
return -1;
ret = pca9534_get_level(I2C_PORT_MASTER, 0x40, 6, &level);
if (ret)
return -1;
return level;
}
static int board_usb_hub_reset(void)
{
int ret;
ret = pca9534_config_pin(I2C_PORT_MASTER, 0x40, 7, PCA9534_OUTPUT);
if (ret)
return ret;
ret = pca9534_set_level(I2C_PORT_MASTER, 0x40, 7, 0);
if (ret)
return ret;
usleep(100 * MSEC);
return pca9534_set_level(I2C_PORT_MASTER, 0x40, 7, 1);
}
void board_maybe_reset_usb_hub(void)
{
if (board_in_hub_mode() == 1)
board_usb_hub_reset();
}
static int cmd_usb_hub_reset(int argc, char *argv[])
{
return board_usb_hub_reset();
}
DECLARE_CONSOLE_COMMAND(hub_reset, cmd_usb_hub_reset,
NULL, "Reset USB hub", NULL);
static void board_usb_hub_reset_no_return(void)
{
board_usb_hub_reset();
}
DECLARE_DEFERRED(board_usb_hub_reset_no_return);
static void board_init_usb_hub(void)
{
if (system_get_reset_flags() & RESET_FLAG_POWER_ON)
hook_call_deferred(board_usb_hub_reset_no_return, 500 * MSEC);
}
DECLARE_HOOK(HOOK_INIT, board_init_usb_hub, HOOK_PRIO_DEFAULT);
void board_pd_set_host_mode(int enable)
{
cprintf(CC_USBPD, "Host mode: %d\n", enable);
if (board_pd_fake_disconnected()) {
board_update_fake_adc_value(enable);
return;
}
if (enable) {
/* Source mode, disable charging */
gpio_set_level(GPIO_USBC_CHARGE_EN, 0);
/* High Z for no pull-down resistor on CC1 */
gpio_set_flags_by_mask(GPIO_A, (1 << 9), GPIO_INPUT);
/* Set pull-up resistor on CC1 */
gpio_set_flags_by_mask(GPIO_A, (1 << 2), GPIO_OUT_HIGH);
/* High Z for no pull-down resistor on CC2 */
gpio_set_flags_by_mask(GPIO_B, (1 << 7), GPIO_INPUT);
/* Set pull-up resistor on CC2 */
gpio_set_flags_by_mask(GPIO_B, (1 << 6), GPIO_OUT_HIGH);
} else {
/* Device mode, disable VBUS */
gpio_set_level(GPIO_VBUS_CHARGER_EN, 0);
gpio_set_level(GPIO_USBC_VSEL_0, 0);
gpio_set_level(GPIO_USBC_VSEL_1, 0);
/* High Z for no pull-up resistor on CC1 */
gpio_set_flags_by_mask(GPIO_A, (1 << 2), GPIO_INPUT);
/* Set pull-down resistor on CC1 */
gpio_set_flags_by_mask(GPIO_A, (1 << 9), GPIO_OUT_LOW);
/* High Z for no pull-up resistor on CC2 */
gpio_set_flags_by_mask(GPIO_B, (1 << 6), GPIO_INPUT);
/* Set pull-down resistor on CC2 */
gpio_set_flags_by_mask(GPIO_B, (1 << 7), GPIO_OUT_LOW);
/* Set charge enable */
gpio_set_level(GPIO_USBC_CHARGE_EN, 1);
}
}
int board_pd_fake_disconnected(void)
{
return fake_pd_disconnected;
}
int board_fake_pd_adc_read(void)
{
if (fake_pd_host_mode)
return 3000; /* mV */
else
return 0; /* mV */
}
void board_update_fake_adc_value(int host_mode)
{
fake_pd_host_mode = host_mode;
}
static void fake_disconnect_end(void)
{
fake_pd_disconnected = 0;
board_pd_set_host_mode(fake_pd_host_mode);
}
DECLARE_DEFERRED(fake_disconnect_end);
static void fake_disconnect_start(void)
{
/* Record the current host mode */
fake_pd_host_mode = !gpio_get_level(GPIO_USBC_CHARGE_EN);
/* Disable VBUS */
gpio_set_level(GPIO_VBUS_CHARGER_EN, 0);
gpio_set_level(GPIO_USBC_VSEL_0, 0);
gpio_set_level(GPIO_USBC_VSEL_1, 0);
/* High Z for no pull-up resistor on CC1 */
gpio_set_flags_by_mask(GPIO_A, (1 << 2), GPIO_INPUT);
/* High Z for no pull-up resistor on CC2 */
gpio_set_flags_by_mask(GPIO_B, (1 << 6), GPIO_INPUT);
/* High Z for no pull-down resistor on CC1 */
gpio_set_flags_by_mask(GPIO_A, (1 << 9), GPIO_INPUT);
/* High Z for no pull-down resistor on CC2 */
gpio_set_flags_by_mask(GPIO_B, (1 << 7), GPIO_INPUT);
fake_pd_disconnected = 1;
hook_call_deferred(fake_disconnect_end,
fake_pd_disconnect_duration_ms * MSEC);
}
DECLARE_DEFERRED(fake_disconnect_start);
static int cmd_fake_disconnect(int argc, char *argv[])
{
int delay_ms, duration_ms;
char *e;
if (argc < 3)
return EC_ERROR_PARAM_COUNT;
delay_ms = strtoi(argv[1], &e, 0);
if (*e || delay_ms < 0)
return EC_ERROR_PARAM1;
duration_ms = strtoi(argv[2], &e, 0);
if (*e || duration_ms < 0)
return EC_ERROR_PARAM2;
/* Cancel any pending function calls */
hook_call_deferred(fake_disconnect_start, -1);
hook_call_deferred(fake_disconnect_end, -1);
fake_pd_disconnect_duration_ms = duration_ms;
hook_call_deferred(fake_disconnect_start, delay_ms * MSEC);
ccprintf("Fake disconnect for %d ms starting in %d ms.\n",
duration_ms, delay_ms);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(fake_disconnect, cmd_fake_disconnect,
"<delay_ms> <duration_ms>", NULL, NULL);
static void trigger_dfu_release(void)
{
gpio_set_level(GPIO_CASE_CLOSE_DFU_L, 1);
ccprintf("Deasserting CASE_CLOSE_DFU_L.\n");
}
DECLARE_DEFERRED(trigger_dfu_release);
static int cmd_trigger_dfu(int argc, char *argv[])
{
gpio_set_level(GPIO_CASE_CLOSE_DFU_L, 0);
ccprintf("Asserting CASE_CLOSE_DFU_L.\n");
ccprintf("If you expect to see DFU debug but it doesn't show up,\n");
ccprintf("try flipping the USB type-C cable.\n");
hook_call_deferred(trigger_dfu_release, 1500 * MSEC);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(dfu, cmd_trigger_dfu, NULL, NULL, NULL);
|