summaryrefslogtreecommitdiff
path: root/drivers/sysreset/sysreset_mpc83xx.c
blob: ff08d356bfe855a243abd0dd1af386ebf87fbae0 (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
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2018
 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
 */

#include <common.h>
#include <command.h>
#include <dm.h>
#include <log.h>
#include <sysreset.h>
#include <wait_bit.h>

#include "sysreset_mpc83xx.h"

/* Magic 4-byte word to enable reset ('RSTE' in ASCII) */
static const u32 RPR_MAGIC = 0x52535445;
/* Wait at most 2000ms for reset control enable bit */
static const uint RESET_WAIT_TIMEOUT = 2000;

/**
 * __do_reset() - Execute the system reset
 *
 * Return: The functions resets the system, and never returns.
 */
static int __do_reset(void)
{
	ulong msr;
	int res;

	immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;

	puts("Resetting the board.\n");

	/* Interrupts and MMU off */
	msr = mfmsr();
	msr &= ~(MSR_EE | MSR_IR | MSR_DR);
	mtmsr(msr);

	/* Enable Reset Control Reg */
	out_be32(&immap->reset.rpr, RPR_MAGIC);
	sync();
	isync();

	/* Confirm Reset Control Reg is enabled */
	res = wait_for_bit_be32(&immap->reset.rcer, RCER_CRE, true,
				RESET_WAIT_TIMEOUT, false);
	if (res) {
		debug("%s: Timed out waiting for reset control to be set\n",
		      __func__);
		return res;
	}

	udelay(200);

	/* Perform reset, only one bit */
	out_be32(&immap->reset.rcr, RCR_SWHR);

	/* Never executes */
	return 0;
}

static int mpc83xx_sysreset_request(struct udevice *dev, enum sysreset_t type)
{
	switch (type) {
	case SYSRESET_WARM:
	case SYSRESET_COLD:
		return __do_reset();
	default:
		return -EPROTONOSUPPORT;
	}

	return -EINPROGRESS;
}

/**
 * print_83xx_arb_event() - Print arbiter events to buffer
 * @force: Print arbiter events, even if none are indicated by the system
 * @buf:   The buffer to receive the printed arbiter event information
 * @size:  The size of the buffer to receive the printed arbiter event
 *	   information in bytes
 *
 * Return: Number of bytes printed to buffer, -ve on error
 */
static int print_83xx_arb_event(bool force, char *buf, int size)
{
	int etype = (gd->arch.arbiter_event_attributes & AEATR_EVENT)
		    >> AEATR_EVENT_SHIFT;
	int mstr_id = (gd->arch.arbiter_event_attributes & AEATR_MSTR_ID)
		      >> AEATR_MSTR_ID_SHIFT;
	int tbst = (gd->arch.arbiter_event_attributes & AEATR_TBST)
		   >> AEATR_TBST_SHIFT;
	int tsize = (gd->arch.arbiter_event_attributes & AEATR_TSIZE)
		    >> AEATR_TSIZE_SHIFT;
	int ttype = (gd->arch.arbiter_event_attributes & AEATR_TTYPE)
		    >> AEATR_TTYPE_SHIFT;
	int tsize_val = (tbst << 3) | tsize;
	int tsize_bytes = tbst ? (tsize ? tsize : 8) : 16 + 8 * tsize;
	int res = 0;

	/*
	 * If we don't force output, and there is no event (event address ==
	 * 0), then don't print anything
	 */
	if (!force && !gd->arch.arbiter_event_address)
		return 0;

	if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_FULL)) {
		res = snprintf(buf, size,
			       "Arbiter Event Status:\n"
			       "    %s: 0x%08lX\n"
			       "    %s:    0x%1x  = %s\n"
			       "    %s:     0x%02x = %s\n"
			       "    %s: 0x%1x  = %d bytes\n"
			       "    %s: 0x%02x = %s\n",
			       "Event Address", gd->arch.arbiter_event_address,
			       "Event Type", etype, event[etype],
			       "Master ID", mstr_id, master[mstr_id],
			       "Transfer Size", tsize_val, tsize_bytes,
			       "Transfer Type", ttype, transfer[ttype]);
	} else if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_BRIEF)) {
		res = snprintf(buf, size,
			       "Arbiter Event Status: AEATR=0x%08lX, AEADR=0x%08lX\n",
			       gd->arch.arbiter_event_attributes,
			       gd->arch.arbiter_event_address);
	}

	return res;
}

static int mpc83xx_sysreset_get_status(struct udevice *dev, char *buf, int size)
{
	/* Ad-hoc data structure to map RSR bit values to their descriptions */
	static const struct {
		/* Bit mask for the bit in question */
		ulong mask;
		/* Description of the bitmask in question */
		char *desc;
	} bits[] = {
		{
		RSR_SWSR, "Software Soft"}, {
		RSR_SWHR, "Software Hard"}, {
		RSR_JSRS, "JTAG Soft"}, {
		RSR_CSHR, "Check Stop"}, {
		RSR_SWRS, "Software Watchdog"}, {
		RSR_BMRS, "Bus Monitor"}, {
		RSR_SRS,  "External/Internal Soft"}, {
		RSR_HRS,  "External/Internal Hard"}
	};
	int res;
	ulong rsr = gd->arch.reset_status;
	int i;
	char *sep;

	res = snprintf(buf, size, "Reset Status:");
	if (res < 0) {
		debug("%s: Could not write reset status message (err = %d)\n",
		      dev->name, res);
		return -EIO;
	}

	buf += res;
	size -= res;

	sep = " ";
	for (i = 0; i < ARRAY_SIZE(bits); i++)
		/* Print description of set bits */
		if (rsr & bits[i].mask) {
			res = snprintf(buf, size, "%s%s%s", sep, bits[i].desc,
				       (i == ARRAY_SIZE(bits) - 1) ? "\n" : "");
			if (res < 0) {
				debug("%s: Could not write reset status message (err = %d)\n",
				      dev->name, res);
				return -EIO;
			}
			buf += res;
			size -= res;
			sep = ", ";
		}

	/*
	 * TODO(mario.six@gdsys.cc): Move this into a dedicated
	 *			     arbiter driver
	 */
	if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_FULL) ||
	    CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_BRIEF)) {
		/*
		 * If there was a bus monitor reset event, we force the arbiter
		 * event to be printed
		 */
		res = print_83xx_arb_event(rsr & RSR_BMRS, buf, size);
		if (res < 0) {
			debug("%s: Could not write arbiter event message (err = %d)\n",
			      dev->name, res);
			return -EIO;
		}
		buf += res;
		size -= res;
	}
	snprintf(buf, size, "\n");

	return 0;
}

static struct sysreset_ops mpc83xx_sysreset = {
	.request	= mpc83xx_sysreset_request,
	.get_status	= mpc83xx_sysreset_get_status,
};

U_BOOT_DRIVER(sysreset_mpc83xx) = {
	.name	= "mpc83xx_sysreset",
	.id	= UCLASS_SYSRESET,
	.ops	= &mpc83xx_sysreset,
};