blob: 0f6aff1db4347e2a5d7546e3a39889935fba07c5 [file] [log] [blame]
Nicolas Le Bayon620ce332021-03-02 11:19:36 +01001/*
Yann Gautier41330b22023-09-18 09:40:37 +02002 * Copyright (C) 2022-2023, STMicroelectronics - All Rights Reserved
Nicolas Le Bayon620ce332021-03-02 11:19:36 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautierdcdc2fd2022-01-19 14:15:48 +01007#include <common/debug.h>
Nicolas Le Bayon620ce332021-03-02 11:19:36 +01008#include <drivers/st/stm32mp_ddr_test.h>
9#include <lib/mmio.h>
10
11#include <platform_def.h>
12
13#define DDR_PATTERN 0xAAAAAAAAU
14#define DDR_ANTIPATTERN 0x55555555U
15
16/*******************************************************************************
17 * This function tests a simple read/write access to the DDR.
18 * Note that the previous content is restored after test.
19 * Returns 0 if success, and address value else.
20 ******************************************************************************/
Yann Gautier41330b22023-09-18 09:40:37 +020021uintptr_t stm32mp_ddr_test_rw_access(void)
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010022{
23 uint32_t saved_value = mmio_read_32(STM32MP_DDR_BASE);
24
25 mmio_write_32(STM32MP_DDR_BASE, DDR_PATTERN);
26
27 if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
Yann Gautier41330b22023-09-18 09:40:37 +020028 return STM32MP_DDR_BASE;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010029 }
30
31 mmio_write_32(STM32MP_DDR_BASE, saved_value);
32
Yann Gautier41330b22023-09-18 09:40:37 +020033 return 0UL;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010034}
35
36/*******************************************************************************
37 * This function tests the DDR data bus wiring.
38 * This is inspired from the Data Bus Test algorithm written by Michael Barr
39 * in "Programming Embedded Systems in C and C++" book.
40 * resources.oreilly.com/examples/9781565923546/blob/master/Chapter6/
41 * File: memtest.c - This source code belongs to Public Domain.
42 * Returns 0 if success, and address value else.
43 ******************************************************************************/
Yann Gautier41330b22023-09-18 09:40:37 +020044uintptr_t stm32mp_ddr_test_data_bus(void)
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010045{
46 uint32_t pattern;
47
48 for (pattern = 1U; pattern != 0U; pattern <<= 1U) {
49 mmio_write_32(STM32MP_DDR_BASE, pattern);
50
51 if (mmio_read_32(STM32MP_DDR_BASE) != pattern) {
Yann Gautier41330b22023-09-18 09:40:37 +020052 return STM32MP_DDR_BASE;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010053 }
54 }
55
Yann Gautier41330b22023-09-18 09:40:37 +020056 return 0UL;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010057}
58
59/*******************************************************************************
60 * This function tests the DDR address 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 * size: size in bytes of the DDR memory device.
66 * Returns 0 if success, and address value else.
67 ******************************************************************************/
Yann Gautier41330b22023-09-18 09:40:37 +020068uintptr_t stm32mp_ddr_test_addr_bus(size_t size)
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010069{
Yann Gautier41330b22023-09-18 09:40:37 +020070 size_t addressmask = size - 1U;
71 size_t offset;
72 size_t testoffset = 0U;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010073
74 /* Write the default pattern at each of the power-of-two offsets. */
75 for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
76 offset <<= 1U) {
Yann Gautier41330b22023-09-18 09:40:37 +020077 mmio_write_32(STM32MP_DDR_BASE + offset, DDR_PATTERN);
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010078 }
79
80 /* Check for address bits stuck high. */
Yann Gautier41330b22023-09-18 09:40:37 +020081 mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_ANTIPATTERN);
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010082
83 for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
84 offset <<= 1U) {
Yann Gautier41330b22023-09-18 09:40:37 +020085 if (mmio_read_32(STM32MP_DDR_BASE + offset) != DDR_PATTERN) {
86 return STM32MP_DDR_BASE + offset;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010087 }
88 }
89
Yann Gautier41330b22023-09-18 09:40:37 +020090 mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_PATTERN);
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010091
92 /* Check for address bits stuck low or shorted. */
93 for (testoffset = sizeof(uint32_t); (testoffset & addressmask) != 0U;
94 testoffset <<= 1U) {
Yann Gautier41330b22023-09-18 09:40:37 +020095 mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_ANTIPATTERN);
Nicolas Le Bayon620ce332021-03-02 11:19:36 +010096
97 if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
98 return STM32MP_DDR_BASE;
99 }
100
101 for (offset = sizeof(uint32_t); (offset & addressmask) != 0U;
102 offset <<= 1) {
Yann Gautier41330b22023-09-18 09:40:37 +0200103 if ((mmio_read_32(STM32MP_DDR_BASE + offset) != DDR_PATTERN) &&
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100104 (offset != testoffset)) {
Yann Gautier41330b22023-09-18 09:40:37 +0200105 return STM32MP_DDR_BASE + offset;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100106 }
107 }
108
Yann Gautier41330b22023-09-18 09:40:37 +0200109 mmio_write_32(STM32MP_DDR_BASE + testoffset, DDR_PATTERN);
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100110 }
111
Yann Gautier41330b22023-09-18 09:40:37 +0200112 return 0UL;
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100113}
114
115/*******************************************************************************
116 * This function checks the DDR size. It has to be run with Data Cache off.
117 * This test is run before data have been put in DDR, and is only done for
118 * cold boot. The DDR data can then be overwritten, and it is not useful to
119 * restore its content.
120 * Returns DDR computed size.
121 ******************************************************************************/
Yann Gautier41330b22023-09-18 09:40:37 +0200122size_t stm32mp_ddr_check_size(void)
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100123{
Yann Gautier41330b22023-09-18 09:40:37 +0200124 size_t offset = sizeof(uint32_t);
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100125
126 mmio_write_32(STM32MP_DDR_BASE, DDR_PATTERN);
127
128 while (offset < STM32MP_DDR_MAX_SIZE) {
129 mmio_write_32(STM32MP_DDR_BASE + offset, DDR_ANTIPATTERN);
130 dsb();
131
132 if (mmio_read_32(STM32MP_DDR_BASE) != DDR_PATTERN) {
133 break;
134 }
135
136 offset <<= 1U;
137 }
138
Nicolas Le Bayon620ce332021-03-02 11:19:36 +0100139 return offset;
140}