ff08d356bfe855a243abd0dd1af386ebf87fbae0
[oweals/u-boot.git] / drivers / sysreset / sysreset_mpc83xx.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2018
4  * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <dm.h>
10 #include <log.h>
11 #include <sysreset.h>
12 #include <wait_bit.h>
13
14 #include "sysreset_mpc83xx.h"
15
16 /* Magic 4-byte word to enable reset ('RSTE' in ASCII) */
17 static const u32 RPR_MAGIC = 0x52535445;
18 /* Wait at most 2000ms for reset control enable bit */
19 static const uint RESET_WAIT_TIMEOUT = 2000;
20
21 /**
22  * __do_reset() - Execute the system reset
23  *
24  * Return: The functions resets the system, and never returns.
25  */
26 static int __do_reset(void)
27 {
28         ulong msr;
29         int res;
30
31         immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
32
33         puts("Resetting the board.\n");
34
35         /* Interrupts and MMU off */
36         msr = mfmsr();
37         msr &= ~(MSR_EE | MSR_IR | MSR_DR);
38         mtmsr(msr);
39
40         /* Enable Reset Control Reg */
41         out_be32(&immap->reset.rpr, RPR_MAGIC);
42         sync();
43         isync();
44
45         /* Confirm Reset Control Reg is enabled */
46         res = wait_for_bit_be32(&immap->reset.rcer, RCER_CRE, true,
47                                 RESET_WAIT_TIMEOUT, false);
48         if (res) {
49                 debug("%s: Timed out waiting for reset control to be set\n",
50                       __func__);
51                 return res;
52         }
53
54         udelay(200);
55
56         /* Perform reset, only one bit */
57         out_be32(&immap->reset.rcr, RCR_SWHR);
58
59         /* Never executes */
60         return 0;
61 }
62
63 static int mpc83xx_sysreset_request(struct udevice *dev, enum sysreset_t type)
64 {
65         switch (type) {
66         case SYSRESET_WARM:
67         case SYSRESET_COLD:
68                 return __do_reset();
69         default:
70                 return -EPROTONOSUPPORT;
71         }
72
73         return -EINPROGRESS;
74 }
75
76 /**
77  * print_83xx_arb_event() - Print arbiter events to buffer
78  * @force: Print arbiter events, even if none are indicated by the system
79  * @buf:   The buffer to receive the printed arbiter event information
80  * @size:  The size of the buffer to receive the printed arbiter event
81  *         information in bytes
82  *
83  * Return: Number of bytes printed to buffer, -ve on error
84  */
85 static int print_83xx_arb_event(bool force, char *buf, int size)
86 {
87         int etype = (gd->arch.arbiter_event_attributes & AEATR_EVENT)
88                     >> AEATR_EVENT_SHIFT;
89         int mstr_id = (gd->arch.arbiter_event_attributes & AEATR_MSTR_ID)
90                       >> AEATR_MSTR_ID_SHIFT;
91         int tbst = (gd->arch.arbiter_event_attributes & AEATR_TBST)
92                    >> AEATR_TBST_SHIFT;
93         int tsize = (gd->arch.arbiter_event_attributes & AEATR_TSIZE)
94                     >> AEATR_TSIZE_SHIFT;
95         int ttype = (gd->arch.arbiter_event_attributes & AEATR_TTYPE)
96                     >> AEATR_TTYPE_SHIFT;
97         int tsize_val = (tbst << 3) | tsize;
98         int tsize_bytes = tbst ? (tsize ? tsize : 8) : 16 + 8 * tsize;
99         int res = 0;
100
101         /*
102          * If we don't force output, and there is no event (event address ==
103          * 0), then don't print anything
104          */
105         if (!force && !gd->arch.arbiter_event_address)
106                 return 0;
107
108         if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_FULL)) {
109                 res = snprintf(buf, size,
110                                "Arbiter Event Status:\n"
111                                "    %s: 0x%08lX\n"
112                                "    %s:    0x%1x  = %s\n"
113                                "    %s:     0x%02x = %s\n"
114                                "    %s: 0x%1x  = %d bytes\n"
115                                "    %s: 0x%02x = %s\n",
116                                "Event Address", gd->arch.arbiter_event_address,
117                                "Event Type", etype, event[etype],
118                                "Master ID", mstr_id, master[mstr_id],
119                                "Transfer Size", tsize_val, tsize_bytes,
120                                "Transfer Type", ttype, transfer[ttype]);
121         } else if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_BRIEF)) {
122                 res = snprintf(buf, size,
123                                "Arbiter Event Status: AEATR=0x%08lX, AEADR=0x%08lX\n",
124                                gd->arch.arbiter_event_attributes,
125                                gd->arch.arbiter_event_address);
126         }
127
128         return res;
129 }
130
131 static int mpc83xx_sysreset_get_status(struct udevice *dev, char *buf, int size)
132 {
133         /* Ad-hoc data structure to map RSR bit values to their descriptions */
134         static const struct {
135                 /* Bit mask for the bit in question */
136                 ulong mask;
137                 /* Description of the bitmask in question */
138                 char *desc;
139         } bits[] = {
140                 {
141                 RSR_SWSR, "Software Soft"}, {
142                 RSR_SWHR, "Software Hard"}, {
143                 RSR_JSRS, "JTAG Soft"}, {
144                 RSR_CSHR, "Check Stop"}, {
145                 RSR_SWRS, "Software Watchdog"}, {
146                 RSR_BMRS, "Bus Monitor"}, {
147                 RSR_SRS,  "External/Internal Soft"}, {
148                 RSR_HRS,  "External/Internal Hard"}
149         };
150         int res;
151         ulong rsr = gd->arch.reset_status;
152         int i;
153         char *sep;
154
155         res = snprintf(buf, size, "Reset Status:");
156         if (res < 0) {
157                 debug("%s: Could not write reset status message (err = %d)\n",
158                       dev->name, res);
159                 return -EIO;
160         }
161
162         buf += res;
163         size -= res;
164
165         sep = " ";
166         for (i = 0; i < ARRAY_SIZE(bits); i++)
167                 /* Print description of set bits */
168                 if (rsr & bits[i].mask) {
169                         res = snprintf(buf, size, "%s%s%s", sep, bits[i].desc,
170                                        (i == ARRAY_SIZE(bits) - 1) ? "\n" : "");
171                         if (res < 0) {
172                                 debug("%s: Could not write reset status message (err = %d)\n",
173                                       dev->name, res);
174                                 return -EIO;
175                         }
176                         buf += res;
177                         size -= res;
178                         sep = ", ";
179                 }
180
181         /*
182          * TODO(mario.six@gdsys.cc): Move this into a dedicated
183          *                           arbiter driver
184          */
185         if (CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_FULL) ||
186             CONFIG_IS_ENABLED(CONFIG_DISPLAY_AER_BRIEF)) {
187                 /*
188                  * If there was a bus monitor reset event, we force the arbiter
189                  * event to be printed
190                  */
191                 res = print_83xx_arb_event(rsr & RSR_BMRS, buf, size);
192                 if (res < 0) {
193                         debug("%s: Could not write arbiter event message (err = %d)\n",
194                               dev->name, res);
195                         return -EIO;
196                 }
197                 buf += res;
198                 size -= res;
199         }
200         snprintf(buf, size, "\n");
201
202         return 0;
203 }
204
205 static struct sysreset_ops mpc83xx_sysreset = {
206         .request        = mpc83xx_sysreset_request,
207         .get_status     = mpc83xx_sysreset_get_status,
208 };
209
210 U_BOOT_DRIVER(sysreset_mpc83xx) = {
211         .name   = "mpc83xx_sysreset",
212         .id     = UCLASS_SYSRESET,
213         .ops    = &mpc83xx_sysreset,
214 };