b.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. /*
  8. * CPU test
  9. * Branch instructions: b, bl, bc
  10. *
  11. * The first 2 instructions (b, bl) are verified by jumping
  12. * to a fixed address and checking whether control was transferred
  13. * to that very point. For the bl instruction the value of the
  14. * link register is checked as well (using mfspr).
  15. * To verify the bc instruction various combinations of the BI/BO
  16. * fields, the CTR and the condition register values are
  17. * checked. The list of such combinations is pre-built and
  18. * linked in U-Boot at build time.
  19. */
  20. #include <post.h>
  21. #include "cpu_asm.h"
  22. #if CONFIG_POST & CONFIG_SYS_POST_CPU
  23. extern void cpu_post_exec_11 (ulong *code, ulong *res, ulong op1);
  24. extern void cpu_post_exec_31 (ulong *code, ulong *ctr, ulong *lr, ulong *jump,
  25. ulong cr);
  26. static int cpu_post_test_bc (ulong cmd, ulong bo, ulong bi,
  27. int pjump, int decr, int link, ulong pctr, ulong cr)
  28. {
  29. int ret = 0;
  30. ulong lr = 0;
  31. ulong ctr = pctr;
  32. ulong jump;
  33. unsigned long code[] =
  34. {
  35. ASM_MTCR(6),
  36. ASM_MFLR(6),
  37. ASM_MTCTR(3),
  38. ASM_MTLR(4),
  39. ASM_LI(5, 1),
  40. ASM_3O(cmd, bo, bi, 8),
  41. ASM_LI(5, 0),
  42. ASM_MFCTR(3),
  43. ASM_MFLR(4),
  44. ASM_MTLR(6),
  45. ASM_BLR,
  46. };
  47. cpu_post_exec_31 (code, &ctr, &lr, &jump, cr);
  48. if (ret == 0)
  49. ret = pjump == jump ? 0 : -1;
  50. if (ret == 0)
  51. {
  52. if (decr)
  53. ret = pctr == ctr + 1 ? 0 : -1;
  54. else
  55. ret = pctr == ctr ? 0 : -1;
  56. }
  57. if (ret == 0)
  58. {
  59. if (link)
  60. ret = lr == (ulong) code + 24 ? 0 : -1;
  61. else
  62. ret = lr == 0 ? 0 : -1;
  63. }
  64. return ret;
  65. }
  66. int cpu_post_test_b (void)
  67. {
  68. int ret = 0;
  69. unsigned int i;
  70. int flag = disable_interrupts();
  71. if (ret == 0)
  72. {
  73. ulong code[] =
  74. {
  75. ASM_MFLR(4),
  76. ASM_MTLR(3),
  77. ASM_B(4),
  78. ASM_MFLR(3),
  79. ASM_MTLR(4),
  80. ASM_BLR,
  81. };
  82. ulong res;
  83. cpu_post_exec_11 (code, &res, 0);
  84. ret = res == 0 ? 0 : -1;
  85. if (ret != 0)
  86. {
  87. post_log ("Error at b1 test !\n");
  88. }
  89. }
  90. if (ret == 0)
  91. {
  92. ulong code[] =
  93. {
  94. ASM_MFLR(4),
  95. ASM_MTLR(3),
  96. ASM_BL(4),
  97. ASM_MFLR(3),
  98. ASM_MTLR(4),
  99. ASM_BLR,
  100. };
  101. ulong res;
  102. cpu_post_exec_11 (code, &res, 0);
  103. ret = res == (ulong)code + 12 ? 0 : -1;
  104. if (ret != 0)
  105. {
  106. post_log ("Error at b2 test !\n");
  107. }
  108. }
  109. if (ret == 0)
  110. {
  111. ulong cc, cd;
  112. int cond;
  113. ulong ctr;
  114. int link;
  115. i = 0;
  116. for (cc = 0; cc < 4 && ret == 0; cc++)
  117. {
  118. for (cd = 0; cd < 4 && ret == 0; cd++)
  119. {
  120. for (link = 0; link <= 1 && ret == 0; link++)
  121. {
  122. for (cond = 0; cond <= 1 && ret == 0; cond++)
  123. {
  124. for (ctr = 1; ctr <= 2 && ret == 0; ctr++)
  125. {
  126. int decr = cd < 2;
  127. int cr = cond ? 0x80000000 : 0x00000000;
  128. int jumpc = cc >= 2 ||
  129. (cc == 0 && !cond) ||
  130. (cc == 1 && cond);
  131. int jumpd = cd >= 2 ||
  132. (cd == 0 && ctr != 1) ||
  133. (cd == 1 && ctr == 1);
  134. int jump = jumpc && jumpd;
  135. ret = cpu_post_test_bc (link ? OP_BCL : OP_BC,
  136. (cc << 3) + (cd << 1), 0, jump, decr, link,
  137. ctr, cr);
  138. if (ret != 0)
  139. {
  140. post_log ("Error at b3 test %d !\n", i);
  141. }
  142. i++;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if (flag)
  150. enable_interrupts();
  151. return ret;
  152. }
  153. #endif