sysreset_mpc83xx.c 5.4 KB

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