summaryrefslogtreecommitdiff
path: root/board/cr50/board.c
blob: fa03e429ba26b697393fd3ad5db44d6632816026 (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
/* 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.
 */

#include "common.h"
#include "console.h"
#include "ec_version.h"
#include "gpio.h"
#include "hooks.h"
#include "registers.h"
#include "task.h"
#include "trng.h"
#include "usb.h"
#include "usb_hid.h"
#include "util.h"

/*
 * There's no way to trigger on both rising and falling edges, so force a
 * compiler error if we try. The workaround is to use the pinmux to connect
 * two GPIOs to the same input and configure each one for a separate edge.
 */
#undef GPIO_INT_BOTH
#define GPIO_INT_BOTH NOT_SUPPORTED_ON_CR50

#include "gpio_list.h"

#ifdef CONFIG_USB_HID
static void send_hid_event(void)
{
	uint64_t rpt = 0;
	uint8_t *key_ptr = (void *)&rpt + 2;
	/* Convert SW_N/SW_S/SW_W/SW_E to A,B,C,D keys */
	if (gpio_get_level(GPIO_SW_N))
		*key_ptr++ = 0x04; /* A keycode */
	if (gpio_get_level(GPIO_SW_S))
		*key_ptr++ = 0x05; /* B keycode */
	if (gpio_get_level(GPIO_SW_W))
		*key_ptr++ = 0x06; /* C keycode */
	if (gpio_get_level(GPIO_SW_E))
		*key_ptr++ = 0x07; /* D keycode */
	/* send the keyboard state over USB HID */
	set_keyboard_report(rpt);
	/* check release in the future */
	hook_call_deferred(send_hid_event, 40);
}
DECLARE_DEFERRED(send_hid_event);
#endif

/* Interrupt handler for button pushes */
void button_event(enum gpio_signal signal)
{
	int v;

	/* We have two GPIOs on the same input (one rising edge, one falling
	 * edge), so de-alias them */
	if (signal >= GPIO_SW_N_)
		signal -= (GPIO_SW_N_ - GPIO_SW_N);

	v = gpio_get_level(signal);
#ifdef CONFIG_USB_HID
	send_hid_event();
#endif
	ccprintf("Button %d = %d\n", signal, v);
	gpio_set_level(signal - GPIO_SW_N + GPIO_LED_4, v);
}

static void init_interrutps(void)
{
	int i;
	static const enum gpio_signal gpio_signals[] = {
		GPIO_SW_N, GPIO_SW_S, GPIO_SW_W, GPIO_SW_E,
		GPIO_SW_N_, GPIO_SW_S_, GPIO_SW_W_, GPIO_SW_E_
	};

	for (i = 0; i < ARRAY_SIZE(gpio_signals); i++)
		gpio_enable_interrupt(gpio_signals[i]);
}

enum permission_level {
	PERMISSION_LOW = 0x00,
	PERMISSION_MEDIUM = 0x33,    /* APPS run at medium */
	PERMISSION_HIGH = 0x3C,
	PERMISSION_HIGHEST = 0x55
};

/* Drop run level to at least medium. */
static void init_runlevel(const enum permission_level desired_level)
{
	volatile uint32_t *const reg_addrs[] = {
		GREG32_ADDR(GLOBALSEC, CPU0_S_PERMISSION),
		GREG32_ADDR(GLOBALSEC, CPU0_S_DAP_PERMISSION),
		GREG32_ADDR(GLOBALSEC, DDMA0_PERMISSION),
	};
	int i;

	/* Permission registers drop by 1 level (e.g. HIGHEST -> HIGH)
	 * each time a write is encountered (the value written does
	 * not matter).  So we repeat writes and reads, until the
	 * desired level is reached.
	 */
	for (i = 0; i < ARRAY_SIZE(reg_addrs); i++) {
		uint32_t current_level;

		while (1) {
			current_level = *reg_addrs[i];
			if (current_level <= desired_level)
				break;
			*reg_addrs[i] = desired_level;
		}
	}
}

/* Initialize board. */
static void board_init(void)
{
	init_interrutps();
	init_trng();
	init_runlevel(PERMISSION_MEDIUM);
}

DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT);

#if defined(CONFIG_USB)
const void * const usb_strings[] = {
	[USB_STR_DESC] = usb_string_desc,
	[USB_STR_VENDOR] = USB_STRING_DESC("Google Inc."),
	[USB_STR_PRODUCT] = USB_STRING_DESC("Cr50"),
	[USB_STR_VERSION] = USB_STRING_DESC(CROS_EC_VERSION32),
	[USB_STR_CONSOLE_NAME] = USB_STRING_DESC("Shell"),
	[USB_STR_BLOB_NAME] = USB_STRING_DESC("Blob"),
	[USB_STR_HID_NAME] = USB_STRING_DESC("PokeyPokey"),
};
BUILD_ASSERT(ARRAY_SIZE(usb_strings) == USB_STR_COUNT);
#endif