sysreset_mpc83xx.c 5.5 KB

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