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