aboutsummaryrefslogtreecommitdiff
path: root/src/jtag/swim.c
blob: de3e106a1a7523fe3e03122e9cb6a676dc0502ef (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
// SPDX-License-Identifier: GPL-2.0-or-later

/*
 * Copyright (C) 2020 by Antonio Borneo <borneo.antonio@gmail.com
 *
 * SWIM (Single Wire Interface Module) is a low-pin-count debug protocol
 * used by STMicroelectronics MCU family STM8 and documented in UM470
 * https://www.st.com/resource/en/user_manual/cd00173911.pdf
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "interface.h"
#include "swim.h"
#include <helper/command.h>
#include <transport/transport.h>

extern struct adapter_driver *adapter_driver;

int swim_system_reset(void)
{
	assert(adapter_driver->swim_ops);

	return adapter_driver->swim_ops->srst();
}

int swim_read_mem(uint32_t addr, uint32_t size, uint32_t count,
				  uint8_t *buffer)
{
	assert(adapter_driver->swim_ops);

	return adapter_driver->swim_ops->read_mem(addr, size, count, buffer);
}

int swim_write_mem(uint32_t addr, uint32_t size, uint32_t count,
				   const uint8_t *buffer)
{
	assert(adapter_driver->swim_ops);

	return adapter_driver->swim_ops->write_mem(addr, size, count, buffer);
}

int swim_reconnect(void)
{
	assert(adapter_driver->swim_ops);

	return adapter_driver->swim_ops->reconnect();
}

COMMAND_HANDLER(handle_swim_newtap_command)
{
	struct jtag_tap *tap;

	/*
	 * only need "basename" and "tap_type", but for backward compatibility
	 * ignore extra parameters
	 */
	if (CMD_ARGC < 2)
		return ERROR_COMMAND_SYNTAX_ERROR;

	tap = calloc(1, sizeof(*tap));
	if (!tap) {
		LOG_ERROR("Out of memory");
		return ERROR_FAIL;
	}

	tap->chip = strdup(CMD_ARGV[0]);
	tap->tapname = strdup(CMD_ARGV[1]);
	tap->dotted_name = alloc_printf("%s.%s", CMD_ARGV[0], CMD_ARGV[1]);
	if (!tap->chip || !tap->tapname || !tap->dotted_name) {
		LOG_ERROR("Out of memory");
		free(tap->dotted_name);
		free(tap->tapname);
		free(tap->chip);
		free(tap);
		return ERROR_FAIL;
	}

	LOG_DEBUG("Creating new SWIM \"tap\", Chip: %s, Tap: %s, Dotted: %s",
			  tap->chip, tap->tapname, tap->dotted_name);

	/* default is enabled-after-reset */
	tap->enabled = true;

	jtag_tap_init(tap);
	return ERROR_OK;
}

static const struct command_registration swim_transport_subcommand_handlers[] = {
	{
		.name = "newtap",
		.handler = handle_swim_newtap_command,
		.mode = COMMAND_CONFIG,
		.help = "Create a new TAP instance named basename.tap_type, "
				"and appends it to the scan chain.",
		.usage = "basename tap_type",
	},
	COMMAND_REGISTRATION_DONE
};

static const struct command_registration swim_transport_command_handlers[] = {
	{
		.name = "swim",
		.mode = COMMAND_ANY,
		.help = "perform swim adapter actions",
		.usage = "",
		.chain = swim_transport_subcommand_handlers,
	},
	COMMAND_REGISTRATION_DONE
};

static int swim_transport_select(struct command_context *cmd_ctx)
{
	LOG_DEBUG(__func__);

	return register_commands(cmd_ctx, NULL, swim_transport_command_handlers);
}

static int swim_transport_init(struct command_context *cmd_ctx)
{
	enum reset_types jtag_reset_config = jtag_get_reset_config();

	LOG_DEBUG(__func__);

	if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
		if (jtag_reset_config & RESET_SRST_NO_GATING)
			adapter_assert_reset();
		else
			LOG_WARNING("\'srst_nogate\' reset_config option is required");
	} else
		adapter_deassert_reset();

	return ERROR_OK;
}

static struct transport swim_transport = {
	.name = "swim",
	.select = swim_transport_select,
	.init = swim_transport_init,
};

static void swim_constructor(void) __attribute__ ((constructor));
static void swim_constructor(void)
{
	transport_register(&swim_transport);
}

bool transport_is_swim(void)
{
	return get_current_transport() == &swim_transport;
}