stm32mp_ddr_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2022-2023, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include <drivers/st/stm32mp_ddr_test.h>
  8. #include <lib/mmio.h>
  9. #include <platform_def.h>
  10. #define DDR_PATTERN 0xAAAAAAAAU
  11. #define DDR_ANTIPATTERN 0x55555555U
  12. /*******************************************************************************
  13. * This function tests a simple read/write access to the DDR.
  14. * Note that the previous content is restored after test.
  15. * Returns 0 if success, and address value else.
  16. ******************************************************************************/
  17. uintptr_t stm32mp_ddr_test_rw_access(void)
  18. {
  19. uint32_t saved_value = mmio_read_32(STM32MP_DDR_BASE);
  20. mmio_write_32(STM32MP_DDR_BASE, DDR_PATTERN);
  21. if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
  22. return STM32MP_DDR_BASE;
  23. }
  24. mmio_write_32(STM32MP_DDR_BASE, saved_value);
  25. return 0UL;
  26. }
  27. /*******************************************************************************
  28. * This function tests the DDR data bus wiring.
  29. * This is inspired from the Data Bus Test algorithm written by Michael Barr
  30. * in "Programming Embedded Systems in C and C++" book.
  31. * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/
  32. * File: memtest.c - This source code belongs to Public Domain.
  33. * Returns 0 if success, and address value else.
  34. ******************************************************************************/
  35. uintptr_t stm32mp_ddr_test_data_bus(void)
  36. {
  37. uint32_t pattern;
  38. for (pattern = 1U; pattern != 0U; pattern <<= 1U) {
  39. mmio_write_32(STM32MP_DDR_BASE, pattern);
  40. if (mmio_read_32(STM32MP_DDR_BASE) != pattern) {
  41. return STM32MP_DDR_BASE;
  42. }
  43. }
  44. return 0UL;
  45. }
  46. /*******************************************************************************
  47. * This function tests the DDR address bus wiring.
  48. * This is inspired from the Data Bus Test algorithm written by Michael Barr
  49. * in "Programming Embedded Systems in C and C++" book.
  50. * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/
  51. * File: memtest.c - This source code belongs to Public Domain.
  52. * size: size in bytes of the DDR memory device.
  53. * Returns 0 if success, and address value else.
  54. ******************************************************************************/
  55. uintptr_t stm32mp_ddr_test_addr_bus(size_t size)
  56. {
  57. size_t addressmask = size - 1U;
  58. size_t offset;
  59. size_t testoffset = 0U;
  60. /* Write the default pattern at each of the power-of-two offsets. */
  61. for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
  62. offset <<= 1U) {
  63. mmio_write_32(STM32MP_DDR_BASE + offset, DDR_PATTERN);
  64. }
  65. /* Check for address bits stuck high. */
  66. mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_ANTIPATTERN);
  67. for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
  68. offset <<= 1U) {
  69. if (mmio_read_32(STM32MP_DDR_BASE + offset) != DDR_PATTERN) {
  70. return STM32MP_DDR_BASE + offset;
  71. }
  72. }
  73. mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_PATTERN);
  74. /* Check for address bits stuck low or shorted. */
  75. for (testoffset = sizeof(uint32_t); (testoffset & addressmask) != 0U;
  76. testoffset <<= 1U) {
  77. mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_ANTIPATTERN);
  78. if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
  79. return STM32MP_DDR_BASE;
  80. }
  81. for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
  82. offset <<= 1) {
  83. if ((mmio_read_32(STM32MP_DDR_BASE + offset) != DDR_PATTERN) &&
  84. (offset != testoffset)) {
  85. return STM32MP_DDR_BASE + offset;
  86. }
  87. }
  88. mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_PATTERN);
  89. }
  90. return 0UL;
  91. }
  92. /*******************************************************************************
  93. * This function checks the DDR size. It has to be run with Data Cache off.
  94. * This test is run before data have been put in DDR, and is only done for
  95. * cold boot. The DDR data can then be overwritten, and it is not useful to
  96. * restore its content.
  97. * Returns DDR computed size.
  98. ******************************************************************************/
  99. size_t stm32mp_ddr_check_size(void)
  100. {
  101. size_t offset = sizeof(uint32_t);
  102. mmio_write_32(STM32MP_DDR_BASE, DDR_PATTERN);
  103. while (offset < STM32MP_DDR_MAX_SIZE) {
  104. mmio_write_32(STM32MP_DDR_BASE + offset, DDR_ANTIPATTERN);
  105. dsb();
  106. if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
  107. break;
  108. }
  109. offset <<= 1U;
  110. }
  111. return offset;
  112. }