summaryrefslogtreecommitdiff
path: root/chip/mchp/gpio_cmds.c
blob: a53f8cca2aeb3ec472567a5c5f38707190aadf9b (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
/* Copyright 2017 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* MCHP MEC GPIO module EC UART commands */

#include "common.h"
#include "console.h"
#include "gpio.h"
#include "gpio_chip.h"
#include "hooks.h"
#include "registers.h"
#include "system.h"
#include "tfdp_chip.h"
#include "util.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_LPC, outstr)
#define CPRINTS(format, args...) cprints(CC_LPC, format, ##args)

static int cmd_gp_get_config(int argc, const char **argv)
{
	char *e;
	int i;
	uint32_t gctrl;

	/* If a signal is specified, print only that one */
	if (argc == 2) {
		i = strtoi(argv[1], &e, 0);
		if (*e)
			return EC_ERROR_PARAM1;

		if (!gpio_is_implemented(i))
			return EC_ERROR_PARAM1;

		gctrl = MCHP_GPIO_CTRL(i);

		ccprintf(" GPIO[0x%X].Ctrl = 0x%08X\n", i, gctrl);

	} else { /* Otherwise print them all */
		for (i = 0; i < GPIO_COUNT; i++) {
			if (!gpio_is_implemented(i))
				continue; /* Skip unsupported signals */

			gctrl = MCHP_GPIO_CTRL(i);

			ccprintf(" GPIO[0x%X].Ctrl = 0x%08X\n", i, gctrl);
		}
	}

	/* Flush console to avoid truncating output */
	cflush();

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(gpgetcfg, cmd_gp_get_config, "[number]",
			"Read GPIO config");

static int cmd_gp_set_config(int argc, const char **argv)
{
	char *e;
	int i;
	uint32_t gctrl;

	/* If a signal is specified, print only that one */
	if (argc > 2) {
		i = strtoi(argv[1], &e, 0);
		if (*e)
			return EC_ERROR_PARAM1;

		if (!gpio_is_implemented(i))
			return EC_ERROR_PARAM1;

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

		MCHP_GPIO_CTRL(i) = gctrl;
		gctrl = MCHP_GPIO_CTRL(i);
		ccprintf(" GPIO[0x%X].Ctrl = 0x%08X\n", i, gctrl);

	} else {
		ccprintf(" Requires two parameters: GPIO num and new config");
	}
	/* Flush console to avoid truncating output */
	cflush();

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(gpsetcfg, cmd_gp_set_config, "gp_num val",
			"Set GPIO config");