sysreset_mpc83xx.c 5.5 KB

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