Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 1 | /* |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 2 | * Copyright (C) 2022-2024, STMicroelectronics - All Rights Reserved |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | dcdc2fd | 2022-01-19 14:15:48 +0100 | [diff] [blame] | 7 | #include <common/debug.h> |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 8 | #include <drivers/st/stm32mp_ddr_test.h> |
| 9 | #include <lib/mmio.h> |
| 10 | |
| 11 | #include <platform_def.h> |
| 12 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 13 | #ifdef __aarch64__ |
| 14 | #define DDR_PATTERN 0xAAAAAAAAAAAAAAAAUL |
| 15 | #define DDR_ANTIPATTERN 0x5555555555555555UL |
| 16 | #else /* !__aarch64__ */ |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 17 | #define DDR_PATTERN 0xAAAAAAAAU |
| 18 | #define DDR_ANTIPATTERN 0x55555555U |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 19 | #endif /* __aarch64__ */ |
| 20 | |
| 21 | static void mmio_write_pattern(uintptr_t addr, u_register_t value) |
| 22 | { |
| 23 | #ifdef __aarch64__ |
| 24 | mmio_write_64(addr, (uint64_t)value); |
| 25 | #else /* !__aarch64__ */ |
| 26 | mmio_write_32(addr, (uint32_t)value); |
| 27 | #endif /* __aarch64__ */ |
| 28 | } |
| 29 | |
| 30 | static u_register_t mmio_read_pattern(uintptr_t addr) |
| 31 | { |
| 32 | #ifdef __aarch64__ |
| 33 | return (u_register_t)mmio_read_64(addr); |
| 34 | #else /* !__aarch64__ */ |
| 35 | return (u_register_t)mmio_read_32(addr); |
| 36 | #endif /* __aarch64__ */ |
| 37 | } |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 38 | |
| 39 | /******************************************************************************* |
| 40 | * This function tests a simple read/write access to the DDR. |
| 41 | * Note that the previous content is restored after test. |
| 42 | * Returns 0 if success, and address value else. |
| 43 | ******************************************************************************/ |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 44 | uintptr_t stm32mp_ddr_test_rw_access(void) |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 45 | { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 46 | u_register_t saved_value = mmio_read_pattern(STM32MP_DDR_BASE); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 47 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 48 | mmio_write_pattern(STM32MP_DDR_BASE, DDR_PATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 49 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 50 | if (mmio_read_pattern(STM32MP_DDR_BASE) != DDR_PATTERN) { |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 51 | return STM32MP_DDR_BASE; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 54 | mmio_write_pattern(STM32MP_DDR_BASE, saved_value); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 55 | |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 56 | return 0UL; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /******************************************************************************* |
| 60 | * This function tests the DDR data bus wiring. |
| 61 | * This is inspired from the Data Bus Test algorithm written by Michael Barr |
| 62 | * in "Programming Embedded Systems in C and C++" book. |
| 63 | * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/ |
| 64 | * File: memtest.c - This source code belongs to Public Domain. |
| 65 | * Returns 0 if success, and address value else. |
| 66 | ******************************************************************************/ |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 67 | uintptr_t stm32mp_ddr_test_data_bus(void) |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 68 | { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 69 | u_register_t pattern; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 70 | |
| 71 | for (pattern = 1U; pattern != 0U; pattern <<= 1U) { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 72 | mmio_write_pattern(STM32MP_DDR_BASE, pattern); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 73 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 74 | if (mmio_read_pattern(STM32MP_DDR_BASE) != pattern) { |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 75 | return STM32MP_DDR_BASE; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 79 | return 0UL; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /******************************************************************************* |
| 83 | * This function tests the DDR address bus wiring. |
| 84 | * This is inspired from the Data Bus Test algorithm written by Michael Barr |
| 85 | * in "Programming Embedded Systems in C and C++" book. |
| 86 | * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/ |
| 87 | * File: memtest.c - This source code belongs to Public Domain. |
| 88 | * size: size in bytes of the DDR memory device. |
| 89 | * Returns 0 if success, and address value else. |
| 90 | ******************************************************************************/ |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 91 | uintptr_t stm32mp_ddr_test_addr_bus(size_t size) |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 92 | { |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 93 | size_t addressmask = size - 1U; |
| 94 | size_t offset; |
| 95 | size_t testoffset = 0U; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 96 | |
| 97 | /* Write the default pattern at each of the power-of-two offsets. */ |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 98 | for (offset = sizeof(u_register_t); (offset & addressmask) != 0U; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 99 | offset <<= 1U) { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 100 | mmio_write_pattern(STM32MP_DDR_BASE + offset, DDR_PATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* Check for address bits stuck high. */ |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 104 | mmio_write_pattern(STM32MP_DDR_BASE + testoffset, DDR_ANTIPATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 105 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 106 | for (offset = sizeof(u_register_t); (offset & addressmask) != 0U; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 107 | offset <<= 1U) { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 108 | if (mmio_read_pattern(STM32MP_DDR_BASE + offset) != DDR_PATTERN) { |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 109 | return STM32MP_DDR_BASE + offset; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 113 | mmio_write_pattern(STM32MP_DDR_BASE + testoffset, DDR_PATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 114 | |
| 115 | /* Check for address bits stuck low or shorted. */ |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 116 | for (testoffset = sizeof(u_register_t); (testoffset & addressmask) != 0U; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 117 | testoffset <<= 1U) { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 118 | mmio_write_pattern(STM32MP_DDR_BASE + testoffset, DDR_ANTIPATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 119 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 120 | if (mmio_read_pattern(STM32MP_DDR_BASE) != DDR_PATTERN) { |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 121 | return STM32MP_DDR_BASE; |
| 122 | } |
| 123 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 124 | for (offset = sizeof(u_register_t); (offset & addressmask) != 0U; |
| 125 | offset <<= 1U) { |
| 126 | if ((mmio_read_pattern(STM32MP_DDR_BASE + offset) != DDR_PATTERN) && |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 127 | (offset != testoffset)) { |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 128 | return STM32MP_DDR_BASE + offset; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 132 | mmio_write_pattern(STM32MP_DDR_BASE + testoffset, DDR_PATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 135 | return 0UL; |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /******************************************************************************* |
| 139 | * This function checks the DDR size. It has to be run with Data Cache off. |
| 140 | * This test is run before data have been put in DDR, and is only done for |
| 141 | * cold boot. The DDR data can then be overwritten, and it is not useful to |
| 142 | * restore its content. |
| 143 | * Returns DDR computed size. |
| 144 | ******************************************************************************/ |
Yann Gautier | 41330b2 | 2023-09-18 09:40:37 +0200 | [diff] [blame] | 145 | size_t stm32mp_ddr_check_size(void) |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 146 | { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 147 | size_t offset = sizeof(u_register_t); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 148 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 149 | mmio_write_pattern(STM32MP_DDR_BASE, DDR_PATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 150 | |
| 151 | while (offset < STM32MP_DDR_MAX_SIZE) { |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 152 | mmio_write_pattern(STM32MP_DDR_BASE + offset, DDR_ANTIPATTERN); |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 153 | dsb(); |
| 154 | |
Nicolas Le Bayon | 068d341 | 2021-07-01 14:44:22 +0200 | [diff] [blame^] | 155 | if (mmio_read_pattern(STM32MP_DDR_BASE) != DDR_PATTERN) { |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | |
| 159 | offset <<= 1U; |
| 160 | } |
| 161 | |
Nicolas Le Bayon | 620ce33 | 2021-03-02 11:19:36 +0100 | [diff] [blame] | 162 | return offset; |
| 163 | } |