summaryrefslogtreecommitdiff
path: root/board/keyborg/master_slave.c
blob: 564bc502d6d24e6541adcfc77040c620646a57b7 (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
/* 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.
 */
/* Master/slave identification */

#include "config.h"
#include "debug.h"
#include "master_slave.h"
#include "registers.h"
#include "timer.h"
#include "util.h"

#define SYNC1 (1 << 1) /* PI1 */
#define SYNC2 (1 << 2) /* PI2 */

static int is_master = -1;

int master_slave_is_master(void)
{
	return is_master;
}

static int wait_sync_signal(int mask, int v, int timeout_ms)
{
	uint32_t start = get_time().val;

	while ((!!(STM32_GPIO_IDR(GPIO_I) & mask)) != v) {
		if ((get_time().val - start) >= timeout_ms * MSEC)
			return EC_ERROR_TIMEOUT;
	}
	return EC_SUCCESS;
}

int master_slave_sync_impl(const char *filename, int line, int timeout_ms)
{
	int err = EC_SUCCESS;
	if (is_master) {
		STM32_GPIO_BSRR(GPIO_I) = SYNC1 << 0;
		if (wait_sync_signal(SYNC2, 1, timeout_ms))
			err = EC_ERROR_TIMEOUT;
		STM32_GPIO_BSRR(GPIO_I) = SYNC1 << 16;
		if (wait_sync_signal(SYNC2, 0, 5))
			err = EC_ERROR_TIMEOUT;
	} else {
		if (wait_sync_signal(SYNC1, 1, timeout_ms))
			err = EC_ERROR_TIMEOUT;
		STM32_GPIO_BSRR(GPIO_I) = SYNC2 << 0;
		if (wait_sync_signal(SYNC1, 0, 5))
			err = EC_ERROR_TIMEOUT;
		STM32_GPIO_BSRR(GPIO_I) = SYNC2 << 16;
	}
	if (err != EC_SUCCESS)
		debug_printf("Sync failed at %s:%d\n", filename, line);
	return err;
}

static int master_handshake(void)
{
	uint32_t val;
	int err;

	/* SYNC2 is the sync signal from the slave. Set it to input. */
	val = STM32_GPIO_CRL(GPIO_I);
	val &= ~0x00000f00;
	val |=  0x00000400;
	STM32_GPIO_CRL(GPIO_I) = val;

	err = master_slave_sync(1000);
	err |= master_slave_sync(20);
	err |= master_slave_sync(20);

	return err;
}

static int slave_handshake(void)
{
	uint32_t val;
	int err;

	/*
	 * N_CHG is used to drive SPI_NSS on the master. Set it to
	 * output low.
	 */
	val = STM32_GPIO_CRL(GPIO_A);
	val &= ~0x000000f0;
	val |= 0x00000010;
	STM32_GPIO_CRL(GPIO_A) = val;
	STM32_GPIO_BSRR(GPIO_A) = 1 << (1 + 16);

	/* SYNC1 is the sync signal from the master. Set it to input. */
	val = STM32_GPIO_CRL(GPIO_I);
	val &= ~0x000000f0;
	val |=  0x00000040;
	STM32_GPIO_CRL(GPIO_I) = val;

	err = master_slave_sync(1000);
	err |= master_slave_sync(20);
	err |= master_slave_sync(20);

	return err;
}

static void master_slave_check(void)
{
	/*
	 * Master slave identity check:
	 *   - Master has USB_PU connected to N_CHG through 1.5K
	 *     resistor. USB_PU is initially low, so N_CHG is low.
	 *   - Slave has N_CHG connected to master NSS with a 20K
	 *     pull-up. Master NSS is initially Hi-Z, so N_CHG is
	 *     high.
	 */

	if (STM32_GPIO_IDR(GPIO_A) & (1 << 1) /* N_CHG */) {
		debug_printf("I'm slave\n");
		is_master = 0;
	} else {
		debug_printf("I'm master\n");
		is_master = 1;
	}
}

int master_slave_init(void)
{
	int handshake_err;

	master_slave_check();

	if (is_master)
		handshake_err = master_handshake();
	else
		handshake_err = slave_handshake();

	if (handshake_err != EC_SUCCESS)
		debug_printf("handshake error\n");
	else
		debug_printf("handshake done\n");

	return handshake_err;
}