Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Memory Functions |
| 9 | * |
| 10 | * Copied from FADS ROM, Dan Malek (dmalek@jlc.net) |
| 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 14 | #include <console.h> |
Simon Glass | 399ed9a | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 15 | #include <bootretry.h> |
Simon Glass | dec3c01 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 16 | #include <cli.h> |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 17 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 18 | #include <console.h> |
Simon Glass | a606ffc | 2019-12-28 10:44:40 -0700 | [diff] [blame] | 19 | #include <flash.h> |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 20 | #include <hash.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 21 | #include <log.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 22 | #include <mapmem.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 23 | #include <rand.h> |
Sergei Poselenov | 474dbb8 | 2008-04-09 16:09:41 +0200 | [diff] [blame] | 24 | #include <watchdog.h> |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 25 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Simon Glass | eacd14f | 2012-11-30 13:01:20 +0000 | [diff] [blame] | 27 | #include <linux/compiler.h> |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 28 | #include <linux/ctype.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 29 | #include <linux/delay.h> |
Simon Glass | eacd14f | 2012-11-30 13:01:20 +0000 | [diff] [blame] | 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 32 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 33 | #ifndef CONFIG_SYS_MEMTEST_SCRATCH |
| 34 | #define CONFIG_SYS_MEMTEST_SCRATCH 0 |
| 35 | #endif |
| 36 | |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 37 | /* Create a compile-time value */ |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 38 | #ifdef MEM_SUPPORT_64BIT_DATA |
| 39 | #define SUPPORT_64BIT_DATA 1 |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 40 | #define HELP_Q ", .q" |
| 41 | #else |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 42 | #define SUPPORT_64BIT_DATA 0 |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 43 | #define HELP_Q "" |
| 44 | #endif |
| 45 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 46 | static int mod_mem(struct cmd_tbl *, int, int, int, char * const []); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 47 | |
| 48 | /* Display values from last command. |
| 49 | * Memory modify remembered values are different from display memory. |
| 50 | */ |
Scott Wood | 3ec0288 | 2015-03-19 09:43:12 -0700 | [diff] [blame] | 51 | static ulong dp_last_addr, dp_last_size; |
| 52 | static ulong dp_last_length = 0x40; |
| 53 | static ulong mm_last_addr, mm_last_size; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 54 | |
| 55 | static ulong base_address = 0; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 56 | #ifdef CONFIG_CMD_MEM_SEARCH |
| 57 | static ulong dp_last_ms_length; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 58 | static u8 search_buf[64]; |
| 59 | static uint search_len; |
| 60 | #endif |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 61 | |
| 62 | /* Memory Display |
| 63 | * |
| 64 | * Syntax: |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 65 | * md{.b, .w, .l, .q} {addr} {len} |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 66 | */ |
| 67 | #define DISP_LINE_LEN 16 |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 68 | static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc, |
| 69 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 70 | { |
Tuomas Tynkkynen | 1b72520 | 2017-10-10 21:59:42 +0300 | [diff] [blame] | 71 | ulong addr, length, bytes; |
| 72 | const void *buf; |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 73 | int size; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 74 | int rc = 0; |
| 75 | |
| 76 | /* We use the last specified parameters, unless new ones are |
| 77 | * entered. |
| 78 | */ |
| 79 | addr = dp_last_addr; |
| 80 | size = dp_last_size; |
| 81 | length = dp_last_length; |
| 82 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 83 | if (argc < 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 84 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 85 | |
| 86 | if ((flag & CMD_FLAG_REPEAT) == 0) { |
| 87 | /* New command specified. Check for a size specification. |
| 88 | * Defaults to long if no or incorrect specification. |
| 89 | */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 90 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
| 91 | return 1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 92 | |
| 93 | /* Address is specified since argc > 1 |
| 94 | */ |
| 95 | addr = simple_strtoul(argv[1], NULL, 16); |
| 96 | addr += base_address; |
| 97 | |
| 98 | /* If another parameter, it is the length to display. |
| 99 | * Length is the number of objects, not number of bytes. |
| 100 | */ |
| 101 | if (argc > 2) |
| 102 | length = simple_strtoul(argv[2], NULL, 16); |
| 103 | } |
| 104 | |
Tuomas Tynkkynen | 1b72520 | 2017-10-10 21:59:42 +0300 | [diff] [blame] | 105 | bytes = size * length; |
| 106 | buf = map_sysmem(addr, bytes); |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 107 | |
Tuomas Tynkkynen | 1b72520 | 2017-10-10 21:59:42 +0300 | [diff] [blame] | 108 | /* Print the lines. */ |
| 109 | print_buffer(addr, buf, size, length, DISP_LINE_LEN / size); |
| 110 | addr += bytes; |
| 111 | unmap_sysmem(buf); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 112 | |
| 113 | dp_last_addr = addr; |
| 114 | dp_last_length = length; |
| 115 | dp_last_size = size; |
| 116 | return (rc); |
| 117 | } |
| 118 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 119 | static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc, |
| 120 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 121 | { |
| 122 | return mod_mem (cmdtp, 1, flag, argc, argv); |
| 123 | } |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 124 | |
| 125 | static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc, |
| 126 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 127 | { |
| 128 | return mod_mem (cmdtp, 0, flag, argc, argv); |
| 129 | } |
| 130 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 131 | static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc, |
| 132 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 133 | { |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 134 | ulong writeval; /* 64-bit if SUPPORT_64BIT_DATA */ |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 135 | ulong addr, count; |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 136 | int size; |
Simon Glass | 3c4a287 | 2015-03-05 12:25:18 -0700 | [diff] [blame] | 137 | void *buf, *start; |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 138 | ulong bytes; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 139 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 140 | if ((argc < 3) || (argc > 4)) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 141 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 142 | |
| 143 | /* Check for size specification. |
| 144 | */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 145 | if ((size = cmd_get_data_size(argv[0], 4)) < 1) |
| 146 | return 1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 147 | |
| 148 | /* Address is specified since argc > 1 |
| 149 | */ |
| 150 | addr = simple_strtoul(argv[1], NULL, 16); |
| 151 | addr += base_address; |
| 152 | |
| 153 | /* Get the value to write. |
| 154 | */ |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 155 | if (SUPPORT_64BIT_DATA) |
| 156 | writeval = simple_strtoull(argv[2], NULL, 16); |
| 157 | else |
| 158 | writeval = simple_strtoul(argv[2], NULL, 16); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 159 | |
| 160 | /* Count ? */ |
| 161 | if (argc == 4) { |
| 162 | count = simple_strtoul(argv[3], NULL, 16); |
| 163 | } else { |
| 164 | count = 1; |
| 165 | } |
| 166 | |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 167 | bytes = size * count; |
Simon Glass | 3c4a287 | 2015-03-05 12:25:18 -0700 | [diff] [blame] | 168 | start = map_sysmem(addr, bytes); |
| 169 | buf = start; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 170 | while (count-- > 0) { |
| 171 | if (size == 4) |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 172 | *((u32 *)buf) = (u32)writeval; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 173 | else if (SUPPORT_64BIT_DATA && size == 8) |
| 174 | *((ulong *)buf) = writeval; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 175 | else if (size == 2) |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 176 | *((u16 *)buf) = (u16)writeval; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 177 | else |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 178 | *((u8 *)buf) = (u8)writeval; |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 179 | buf += size; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 180 | } |
Simon Glass | 3c4a287 | 2015-03-05 12:25:18 -0700 | [diff] [blame] | 181 | unmap_sysmem(start); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Joel Johnson | db5a97e | 2020-01-29 09:17:18 -0700 | [diff] [blame] | 185 | #ifdef CONFIG_CMD_MX_CYCLIC |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 186 | static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc, |
| 187 | char *const argv[]) |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 188 | { |
| 189 | int i; |
| 190 | ulong count; |
| 191 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 192 | if (argc < 4) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 193 | return CMD_RET_USAGE; |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 194 | |
| 195 | count = simple_strtoul(argv[3], NULL, 10); |
| 196 | |
| 197 | for (;;) { |
| 198 | do_mem_md (NULL, 0, 3, argv); |
| 199 | |
| 200 | /* delay for <count> ms... */ |
| 201 | for (i=0; i<count; i++) |
Simon Glass | 0db4b94 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 202 | udelay(1000); |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 203 | |
| 204 | /* check for ctrl-c to abort... */ |
| 205 | if (ctrlc()) { |
| 206 | puts("Abort\n"); |
| 207 | return 0; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 214 | static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc, |
| 215 | char *const argv[]) |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 216 | { |
| 217 | int i; |
| 218 | ulong count; |
| 219 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 220 | if (argc < 4) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 221 | return CMD_RET_USAGE; |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 222 | |
| 223 | count = simple_strtoul(argv[3], NULL, 10); |
| 224 | |
| 225 | for (;;) { |
| 226 | do_mem_mw (NULL, 0, 3, argv); |
| 227 | |
| 228 | /* delay for <count> ms... */ |
| 229 | for (i=0; i<count; i++) |
Simon Glass | 0db4b94 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 230 | udelay(1000); |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 231 | |
| 232 | /* check for ctrl-c to abort... */ |
| 233 | if (ctrlc()) { |
| 234 | puts("Abort\n"); |
| 235 | return 0; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
Joel Johnson | db5a97e | 2020-01-29 09:17:18 -0700 | [diff] [blame] | 241 | #endif /* CONFIG_CMD_MX_CYCLIC */ |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 242 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 243 | static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc, |
| 244 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 245 | { |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 246 | ulong addr1, addr2, count, ngood, bytes; |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 247 | int size; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 248 | int rcode = 0; |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 249 | const char *type; |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 250 | const void *buf1, *buf2, *base; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 251 | ulong word1, word2; /* 64-bit if SUPPORT_64BIT_DATA */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 252 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 253 | if (argc != 4) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 254 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 255 | |
| 256 | /* Check for size specification. |
| 257 | */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 258 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
| 259 | return 1; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 260 | type = size == 8 ? "double word" : |
| 261 | size == 4 ? "word" : |
| 262 | size == 2 ? "halfword" : "byte"; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 263 | |
| 264 | addr1 = simple_strtoul(argv[1], NULL, 16); |
| 265 | addr1 += base_address; |
| 266 | |
| 267 | addr2 = simple_strtoul(argv[2], NULL, 16); |
| 268 | addr2 += base_address; |
| 269 | |
| 270 | count = simple_strtoul(argv[3], NULL, 16); |
| 271 | |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 272 | bytes = size * count; |
| 273 | base = buf1 = map_sysmem(addr1, bytes); |
| 274 | buf2 = map_sysmem(addr2, bytes); |
Mike Frysinger | a66afbd | 2012-01-20 09:07:22 +0000 | [diff] [blame] | 275 | for (ngood = 0; ngood < count; ++ngood) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 276 | if (size == 4) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 277 | word1 = *(u32 *)buf1; |
| 278 | word2 = *(u32 *)buf2; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 279 | } else if (SUPPORT_64BIT_DATA && size == 8) { |
| 280 | word1 = *(ulong *)buf1; |
| 281 | word2 = *(ulong *)buf2; |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 282 | } else if (size == 2) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 283 | word1 = *(u16 *)buf1; |
| 284 | word2 = *(u16 *)buf2; |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 285 | } else { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 286 | word1 = *(u8 *)buf1; |
| 287 | word2 = *(u8 *)buf2; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 288 | } |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 289 | if (word1 != word2) { |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 290 | ulong offset = buf1 - base; |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 291 | printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n", |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 292 | type, (ulong)(addr1 + offset), size, word1, |
| 293 | type, (ulong)(addr2 + offset), size, word2); |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 294 | rcode = 1; |
| 295 | break; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 296 | } |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 297 | |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 298 | buf1 += size; |
| 299 | buf2 += size; |
Stefan Roese | d4ef82b | 2010-09-13 11:10:34 +0200 | [diff] [blame] | 300 | |
| 301 | /* reset watchdog from time to time */ |
Mike Frysinger | a66afbd | 2012-01-20 09:07:22 +0000 | [diff] [blame] | 302 | if ((ngood % (64 << 10)) == 0) |
Stefan Roese | d4ef82b | 2010-09-13 11:10:34 +0200 | [diff] [blame] | 303 | WATCHDOG_RESET(); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 304 | } |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 305 | unmap_sysmem(buf1); |
| 306 | unmap_sysmem(buf2); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 307 | |
Mike Frysinger | 62a7ea7 | 2012-01-20 09:07:21 +0000 | [diff] [blame] | 308 | printf("Total of %ld %s(s) were the same\n", ngood, type); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 309 | return rcode; |
| 310 | } |
| 311 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 312 | static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc, |
| 313 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 314 | { |
Fabio Estevam | b03b0c8 | 2016-12-15 16:00:13 -0200 | [diff] [blame] | 315 | ulong addr, dest, count; |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 316 | void *src, *dst; |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 317 | int size; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 318 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 319 | if (argc != 4) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 320 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 321 | |
| 322 | /* Check for size specification. |
| 323 | */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 324 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
| 325 | return 1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 326 | |
| 327 | addr = simple_strtoul(argv[1], NULL, 16); |
| 328 | addr += base_address; |
| 329 | |
| 330 | dest = simple_strtoul(argv[2], NULL, 16); |
| 331 | dest += base_address; |
| 332 | |
| 333 | count = simple_strtoul(argv[3], NULL, 16); |
| 334 | |
| 335 | if (count == 0) { |
| 336 | puts ("Zero length ???\n"); |
| 337 | return 1; |
| 338 | } |
| 339 | |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 340 | src = map_sysmem(addr, count * size); |
| 341 | dst = map_sysmem(dest, count * size); |
| 342 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 343 | #ifdef CONFIG_MTD_NOR_FLASH |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 344 | /* check if we are copying to Flash */ |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 345 | if (addr2info((ulong)dst)) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 346 | int rc; |
| 347 | |
wdenk | 42c0547 | 2004-03-23 22:14:11 +0000 | [diff] [blame] | 348 | puts ("Copy to Flash... "); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 349 | |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 350 | rc = flash_write((char *)src, (ulong)dst, count * size); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 351 | if (rc != 0) { |
Simon Glass | a606ffc | 2019-12-28 10:44:40 -0700 | [diff] [blame] | 352 | flash_perror(rc); |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 353 | unmap_sysmem(src); |
| 354 | unmap_sysmem(dst); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 355 | return (1); |
| 356 | } |
| 357 | puts ("done\n"); |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 358 | unmap_sysmem(src); |
| 359 | unmap_sysmem(dst); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 360 | return 0; |
| 361 | } |
wdenk | 7a428cc | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 362 | #endif |
| 363 | |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 364 | memcpy(dst, src, count * size); |
Masahiro Yamada | 787b0c5 | 2014-10-23 17:46:24 +0900 | [diff] [blame] | 365 | |
Philippe Reynes | b5f461a | 2019-12-02 17:33:22 +0100 | [diff] [blame] | 366 | unmap_sysmem(src); |
| 367 | unmap_sysmem(dst); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 371 | #ifdef CONFIG_CMD_MEM_SEARCH |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 372 | static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc, |
| 373 | char *const argv[]) |
| 374 | { |
| 375 | ulong addr, length, bytes, offset; |
| 376 | u8 *ptr, *end, *buf; |
| 377 | bool quiet = false; |
| 378 | ulong last_pos; /* Offset of last match in 'size' units*/ |
| 379 | ulong last_addr; /* Address of last displayed line */ |
| 380 | int limit = 10; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 381 | int used_len; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 382 | int count; |
| 383 | int size; |
| 384 | int i; |
| 385 | |
| 386 | /* We use the last specified parameters, unless new ones are entered */ |
| 387 | addr = dp_last_addr; |
| 388 | size = dp_last_size; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 389 | length = dp_last_ms_length; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 390 | |
| 391 | if (argc < 3) |
| 392 | return CMD_RET_USAGE; |
| 393 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 394 | if (!(flag & CMD_FLAG_REPEAT)) { |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 395 | /* |
| 396 | * Check for a size specification. |
| 397 | * Defaults to long if no or incorrect specification. |
| 398 | */ |
| 399 | size = cmd_get_data_size(argv[0], 4); |
| 400 | if (size < 0 && size != -2 /* string */) |
| 401 | return 1; |
| 402 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 403 | argc--; |
| 404 | argv++; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 405 | while (argc && *argv[0] == '-') { |
| 406 | int ch = argv[0][1]; |
| 407 | |
| 408 | if (ch == 'q') |
| 409 | quiet = true; |
| 410 | else if (ch == 'l' && isxdigit(argv[0][2])) |
| 411 | limit = simple_strtoul(argv[0] + 2, NULL, 16); |
| 412 | else |
| 413 | return CMD_RET_USAGE; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 414 | argc--; |
| 415 | argv++; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* Address is specified since argc > 1 */ |
| 419 | addr = simple_strtoul(argv[0], NULL, 16); |
| 420 | addr += base_address; |
| 421 | |
| 422 | /* Length is the number of objects, not number of bytes */ |
| 423 | length = simple_strtoul(argv[1], NULL, 16); |
| 424 | |
| 425 | /* Read the bytes to search for */ |
| 426 | end = search_buf + sizeof(search_buf); |
| 427 | for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) { |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 428 | if (MEM_SUPPORT_64BIT_DATA && size == 8) { |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 429 | u64 val = simple_strtoull(argv[i], NULL, 16); |
| 430 | |
| 431 | *(u64 *)ptr = val; |
| 432 | } else if (size == -2) { /* string */ |
| 433 | int len = min(strlen(argv[i]), |
| 434 | (size_t)(end - ptr)); |
| 435 | |
| 436 | memcpy(ptr, argv[i], len); |
| 437 | ptr += len; |
| 438 | continue; |
| 439 | } else { |
| 440 | u32 val = simple_strtoul(argv[i], NULL, 16); |
| 441 | |
| 442 | switch (size) { |
| 443 | case 1: |
| 444 | *ptr = val; |
| 445 | break; |
| 446 | case 2: |
| 447 | *(u16 *)ptr = val; |
| 448 | break; |
| 449 | case 4: |
| 450 | *(u32 *)ptr = val; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | ptr += size; |
| 455 | } |
| 456 | search_len = ptr - search_buf; |
| 457 | } |
| 458 | |
| 459 | /* Do the search */ |
| 460 | if (size == -2) |
| 461 | size = 1; |
| 462 | bytes = size * length; |
| 463 | buf = map_sysmem(addr, bytes); |
| 464 | last_pos = 0; |
| 465 | last_addr = 0; |
| 466 | count = 0; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 467 | for (offset = 0; |
| 468 | offset < bytes && offset <= bytes - search_len && count < limit; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 469 | offset += size) { |
| 470 | void *ptr = buf + offset; |
| 471 | |
| 472 | if (!memcmp(ptr, search_buf, search_len)) { |
| 473 | uint align = (addr + offset) & 0xf; |
| 474 | ulong match = addr + offset; |
| 475 | |
| 476 | if (!count || (last_addr & ~0xf) != (match & ~0xf)) { |
| 477 | if (!quiet) { |
| 478 | if (count) |
| 479 | printf("--\n"); |
| 480 | print_buffer(match - align, ptr - align, |
| 481 | size, |
| 482 | ALIGN(search_len + align, |
| 483 | 16) / size, 0); |
| 484 | } |
| 485 | last_addr = match; |
| 486 | last_pos = offset / size; |
| 487 | } |
| 488 | count++; |
| 489 | } |
| 490 | } |
| 491 | if (!quiet) { |
| 492 | printf("%d match%s", count, count == 1 ? "" : "es"); |
| 493 | if (count == limit) |
| 494 | printf(" (repeat command to check for more)"); |
| 495 | printf("\n"); |
| 496 | } |
| 497 | env_set_hex("memmatches", count); |
| 498 | env_set_hex("memaddr", last_addr); |
| 499 | env_set_hex("mempos", last_pos); |
| 500 | |
| 501 | unmap_sysmem(buf); |
| 502 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 503 | used_len = offset / size; |
| 504 | dp_last_addr = addr + used_len; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 505 | dp_last_size = size; |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 506 | dp_last_ms_length = length < used_len ? 0 : length - used_len; |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 507 | |
| 508 | return count ? 0 : CMD_RET_FAILURE; |
| 509 | } |
| 510 | #endif |
| 511 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 512 | static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc, |
| 513 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 514 | { |
| 515 | if (argc > 1) { |
| 516 | /* Set new base address. |
| 517 | */ |
| 518 | base_address = simple_strtoul(argv[1], NULL, 16); |
| 519 | } |
| 520 | /* Print the current base address. |
| 521 | */ |
| 522 | printf("Base Address: 0x%08lx\n", base_address); |
| 523 | return 0; |
| 524 | } |
| 525 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 526 | static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc, |
| 527 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 528 | { |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 529 | ulong addr, length, i, bytes; |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 530 | int size; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 531 | volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */ |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 532 | volatile u32 *longp; |
| 533 | volatile u16 *shortp; |
| 534 | volatile u8 *cp; |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 535 | const void *buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 536 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 537 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 538 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 539 | |
Robert P. J. Day | 21c9b78 | 2013-02-03 02:29:54 +0000 | [diff] [blame] | 540 | /* |
| 541 | * Check for a size specification. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 542 | * Defaults to long if no or incorrect specification. |
| 543 | */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 544 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
| 545 | return 1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 546 | |
| 547 | /* Address is always specified. |
| 548 | */ |
| 549 | addr = simple_strtoul(argv[1], NULL, 16); |
| 550 | |
| 551 | /* Length is the number of objects, not number of bytes. |
| 552 | */ |
| 553 | length = simple_strtoul(argv[2], NULL, 16); |
| 554 | |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 555 | bytes = size * length; |
| 556 | buf = map_sysmem(addr, bytes); |
| 557 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 558 | /* We want to optimize the loops to run as fast as possible. |
| 559 | * If we have only one object, just run infinite loops. |
| 560 | */ |
| 561 | if (length == 1) { |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 562 | if (SUPPORT_64BIT_DATA && size == 8) { |
| 563 | llp = (ulong *)buf; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 564 | for (;;) |
| 565 | i = *llp; |
| 566 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 567 | if (size == 4) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 568 | longp = (u32 *)buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 569 | for (;;) |
| 570 | i = *longp; |
| 571 | } |
| 572 | if (size == 2) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 573 | shortp = (u16 *)buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 574 | for (;;) |
| 575 | i = *shortp; |
| 576 | } |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 577 | cp = (u8 *)buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 578 | for (;;) |
| 579 | i = *cp; |
| 580 | } |
| 581 | |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 582 | if (SUPPORT_64BIT_DATA && size == 8) { |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 583 | for (;;) { |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 584 | llp = (ulong *)buf; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 585 | i = length; |
| 586 | while (i-- > 0) |
| 587 | *llp++; |
| 588 | } |
| 589 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 590 | if (size == 4) { |
| 591 | for (;;) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 592 | longp = (u32 *)buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 593 | i = length; |
| 594 | while (i-- > 0) |
Marek Vasut | 7d33582 | 2011-09-26 02:26:06 +0200 | [diff] [blame] | 595 | *longp++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | if (size == 2) { |
| 599 | for (;;) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 600 | shortp = (u16 *)buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 601 | i = length; |
| 602 | while (i-- > 0) |
Marek Vasut | 7d33582 | 2011-09-26 02:26:06 +0200 | [diff] [blame] | 603 | *shortp++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | for (;;) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 607 | cp = (u8 *)buf; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 608 | i = length; |
| 609 | while (i-- > 0) |
Marek Vasut | 7d33582 | 2011-09-26 02:26:06 +0200 | [diff] [blame] | 610 | *cp++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 611 | } |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 612 | unmap_sysmem(buf); |
Simon Glass | 50d9eb9 | 2013-06-11 11:14:35 -0700 | [diff] [blame] | 613 | |
| 614 | return 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 615 | } |
| 616 | |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 617 | #ifdef CONFIG_LOOPW |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 618 | static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc, |
| 619 | char *const argv[]) |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 620 | { |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 621 | ulong addr, length, i, bytes; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 622 | int size; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 623 | volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */ |
| 624 | ulong data; /* 64-bit if SUPPORT_64BIT_DATA */ |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 625 | volatile u32 *longp; |
| 626 | volatile u16 *shortp; |
| 627 | volatile u8 *cp; |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 628 | void *buf; |
wdenk | 7dd1329 | 2004-07-11 20:04:51 +0000 | [diff] [blame] | 629 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 630 | if (argc < 4) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 631 | return CMD_RET_USAGE; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 632 | |
Robert P. J. Day | 21c9b78 | 2013-02-03 02:29:54 +0000 | [diff] [blame] | 633 | /* |
| 634 | * Check for a size specification. |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 635 | * Defaults to long if no or incorrect specification. |
| 636 | */ |
| 637 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
| 638 | return 1; |
| 639 | |
| 640 | /* Address is always specified. |
| 641 | */ |
| 642 | addr = simple_strtoul(argv[1], NULL, 16); |
| 643 | |
| 644 | /* Length is the number of objects, not number of bytes. |
| 645 | */ |
| 646 | length = simple_strtoul(argv[2], NULL, 16); |
| 647 | |
| 648 | /* data to write */ |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 649 | if (SUPPORT_64BIT_DATA) |
| 650 | data = simple_strtoull(argv[3], NULL, 16); |
| 651 | else |
| 652 | data = simple_strtoul(argv[3], NULL, 16); |
wdenk | 7dd1329 | 2004-07-11 20:04:51 +0000 | [diff] [blame] | 653 | |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 654 | bytes = size * length; |
| 655 | buf = map_sysmem(addr, bytes); |
| 656 | |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 657 | /* We want to optimize the loops to run as fast as possible. |
| 658 | * If we have only one object, just run infinite loops. |
| 659 | */ |
| 660 | if (length == 1) { |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 661 | if (SUPPORT_64BIT_DATA && size == 8) { |
| 662 | llp = (ulong *)buf; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 663 | for (;;) |
| 664 | *llp = data; |
| 665 | } |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 666 | if (size == 4) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 667 | longp = (u32 *)buf; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 668 | for (;;) |
| 669 | *longp = data; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 670 | } |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 671 | if (size == 2) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 672 | shortp = (u16 *)buf; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 673 | for (;;) |
| 674 | *shortp = data; |
| 675 | } |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 676 | cp = (u8 *)buf; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 677 | for (;;) |
| 678 | *cp = data; |
| 679 | } |
| 680 | |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 681 | if (SUPPORT_64BIT_DATA && size == 8) { |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 682 | for (;;) { |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 683 | llp = (ulong *)buf; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 684 | i = length; |
| 685 | while (i-- > 0) |
| 686 | *llp++ = data; |
| 687 | } |
| 688 | } |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 689 | if (size == 4) { |
| 690 | for (;;) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 691 | longp = (u32 *)buf; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 692 | i = length; |
| 693 | while (i-- > 0) |
| 694 | *longp++ = data; |
| 695 | } |
| 696 | } |
| 697 | if (size == 2) { |
| 698 | for (;;) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 699 | shortp = (u16 *)buf; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 700 | i = length; |
| 701 | while (i-- > 0) |
| 702 | *shortp++ = data; |
| 703 | } |
| 704 | } |
| 705 | for (;;) { |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 706 | cp = (u8 *)buf; |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 707 | i = length; |
| 708 | while (i-- > 0) |
| 709 | *cp++ = data; |
| 710 | } |
| 711 | } |
| 712 | #endif /* CONFIG_LOOPW */ |
| 713 | |
Tom Rini | a5f0270 | 2013-03-12 10:07:19 -0400 | [diff] [blame] | 714 | #ifdef CONFIG_CMD_MEMTEST |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 715 | static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr, |
| 716 | vu_long *dummy) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 717 | { |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 718 | vu_long *addr; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 719 | ulong errs = 0; |
| 720 | ulong val, readback; |
| 721 | int j; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 722 | vu_long offset; |
| 723 | vu_long test_offset; |
| 724 | vu_long pattern; |
| 725 | vu_long temp; |
| 726 | vu_long anti_pattern; |
| 727 | vu_long num_words; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 728 | static const ulong bitpattern[] = { |
| 729 | 0x00000001, /* single bit */ |
| 730 | 0x00000003, /* two adjacent bits */ |
| 731 | 0x00000007, /* three adjacent bits */ |
| 732 | 0x0000000F, /* four adjacent bits */ |
| 733 | 0x00000005, /* two non-adjacent bits */ |
| 734 | 0x00000015, /* three non-adjacent bits */ |
| 735 | 0x00000055, /* four non-adjacent bits */ |
| 736 | 0xaaaaaaaa, /* alternating 1/0 */ |
| 737 | }; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 738 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 739 | num_words = (end_addr - start_addr) / sizeof(vu_long); |
Simon Glass | 5107525 | 2013-02-24 17:33:20 +0000 | [diff] [blame] | 740 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 741 | /* |
| 742 | * Data line test: write a pattern to the first |
| 743 | * location, write the 1's complement to a 'parking' |
| 744 | * address (changes the state of the data bus so a |
| 745 | * floating bus doesn't give a false OK), and then |
| 746 | * read the value back. Note that we read it back |
| 747 | * into a variable because the next time we read it, |
| 748 | * it might be right (been there, tough to explain to |
| 749 | * the quality guys why it prints a failure when the |
| 750 | * "is" and "should be" are obviously the same in the |
| 751 | * error message). |
| 752 | * |
| 753 | * Rather than exhaustively testing, we test some |
| 754 | * patterns by shifting '1' bits through a field of |
| 755 | * '0's and '0' bits through a field of '1's (i.e. |
| 756 | * pattern and ~pattern). |
| 757 | */ |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 758 | addr = buf; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 759 | for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) { |
| 760 | val = bitpattern[j]; |
| 761 | for (; val != 0; val <<= 1) { |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 762 | *addr = val; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 763 | *dummy = ~val; /* clear the test data off the bus */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 764 | readback = *addr; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 765 | if (readback != val) { |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 766 | printf("FAILURE (data line): " |
| 767 | "expected %08lx, actual %08lx\n", |
| 768 | val, readback); |
| 769 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 770 | if (ctrlc()) |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 771 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 772 | } |
| 773 | *addr = ~val; |
| 774 | *dummy = val; |
| 775 | readback = *addr; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 776 | if (readback != ~val) { |
| 777 | printf("FAILURE (data line): " |
| 778 | "Is %08lx, should be %08lx\n", |
| 779 | readback, ~val); |
| 780 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 781 | if (ctrlc()) |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 782 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 783 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 784 | } |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 785 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 786 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 787 | /* |
| 788 | * Based on code whose Original Author and Copyright |
| 789 | * information follows: Copyright (c) 1998 by Michael |
| 790 | * Barr. This software is placed into the public |
| 791 | * domain and may be used for any purpose. However, |
| 792 | * this notice must not be changed or removed and no |
| 793 | * warranty is either expressed or implied by its |
| 794 | * publication or distribution. |
| 795 | */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 796 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 797 | /* |
| 798 | * Address line test |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 799 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 800 | * Description: Test the address bus wiring in a |
| 801 | * memory region by performing a walking |
| 802 | * 1's test on the relevant bits of the |
| 803 | * address and checking for aliasing. |
| 804 | * This test will find single-bit |
| 805 | * address failures such as stuck-high, |
| 806 | * stuck-low, and shorted pins. The base |
| 807 | * address and size of the region are |
| 808 | * selected by the caller. |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 809 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 810 | * Notes: For best results, the selected base |
| 811 | * address should have enough LSB 0's to |
| 812 | * guarantee single address bit changes. |
| 813 | * For example, to test a 64-Kbyte |
| 814 | * region, select a base address on a |
| 815 | * 64-Kbyte boundary. Also, select the |
| 816 | * region size as a power-of-two if at |
| 817 | * all possible. |
| 818 | * |
| 819 | * Returns: 0 if the test succeeds, 1 if the test fails. |
| 820 | */ |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 821 | pattern = (vu_long) 0xaaaaaaaa; |
| 822 | anti_pattern = (vu_long) 0x55555555; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 823 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 824 | debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words); |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 825 | /* |
| 826 | * Write the default pattern at each of the |
| 827 | * power-of-two offsets. |
| 828 | */ |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 829 | for (offset = 1; offset < num_words; offset <<= 1) |
| 830 | addr[offset] = pattern; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * Check for address bits stuck high. |
| 834 | */ |
| 835 | test_offset = 0; |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 836 | addr[test_offset] = anti_pattern; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 837 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 838 | for (offset = 1; offset < num_words; offset <<= 1) { |
| 839 | temp = addr[offset]; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 840 | if (temp != pattern) { |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 841 | printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:" |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 842 | " expected 0x%.8lx, actual 0x%.8lx\n", |
David Feng | f40b221 | 2014-02-12 16:10:08 +0800 | [diff] [blame] | 843 | start_addr + offset*sizeof(vu_long), |
| 844 | pattern, temp); |
Paul Gortmaker | cb47b4f | 2009-10-02 18:18:33 -0400 | [diff] [blame] | 845 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 846 | if (ctrlc()) |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 847 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 848 | } |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 849 | } |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 850 | addr[test_offset] = pattern; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 851 | WATCHDOG_RESET(); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 852 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 853 | /* |
| 854 | * Check for addr bits stuck low or shorted. |
| 855 | */ |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 856 | for (test_offset = 1; test_offset < num_words; test_offset <<= 1) { |
| 857 | addr[test_offset] = anti_pattern; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 858 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 859 | for (offset = 1; offset < num_words; offset <<= 1) { |
| 860 | temp = addr[offset]; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 861 | if ((temp != pattern) && (offset != test_offset)) { |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 862 | printf("\nFAILURE: Address bit stuck low or" |
| 863 | " shorted @ 0x%.8lx: expected 0x%.8lx," |
| 864 | " actual 0x%.8lx\n", |
David Feng | f40b221 | 2014-02-12 16:10:08 +0800 | [diff] [blame] | 865 | start_addr + offset*sizeof(vu_long), |
| 866 | pattern, temp); |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 867 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 868 | if (ctrlc()) |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 869 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 870 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 871 | } |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 872 | addr[test_offset] = pattern; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 873 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 874 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 875 | /* |
| 876 | * Description: Test the integrity of a physical |
| 877 | * memory device by performing an |
| 878 | * increment/decrement test over the |
| 879 | * entire region. In the process every |
| 880 | * storage bit in the device is tested |
| 881 | * as a zero and a one. The base address |
| 882 | * and the size of the region are |
| 883 | * selected by the caller. |
| 884 | * |
| 885 | * Returns: 0 if the test succeeds, 1 if the test fails. |
| 886 | */ |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 887 | num_words++; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 888 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 889 | /* |
| 890 | * Fill memory with a known pattern. |
| 891 | */ |
| 892 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { |
| 893 | WATCHDOG_RESET(); |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 894 | addr[offset] = pattern; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 895 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 896 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 897 | /* |
| 898 | * Check each location and invert it for the second pass. |
| 899 | */ |
| 900 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { |
| 901 | WATCHDOG_RESET(); |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 902 | temp = addr[offset]; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 903 | if (temp != pattern) { |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 904 | printf("\nFAILURE (read/write) @ 0x%.8lx:" |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 905 | " expected 0x%.8lx, actual 0x%.8lx)\n", |
David Feng | f40b221 | 2014-02-12 16:10:08 +0800 | [diff] [blame] | 906 | start_addr + offset*sizeof(vu_long), |
| 907 | pattern, temp); |
Paul Gortmaker | cb47b4f | 2009-10-02 18:18:33 -0400 | [diff] [blame] | 908 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 909 | if (ctrlc()) |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 910 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 913 | anti_pattern = ~pattern; |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 914 | addr[offset] = anti_pattern; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Check each location for the inverted pattern and zero it. |
| 919 | */ |
| 920 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { |
| 921 | WATCHDOG_RESET(); |
| 922 | anti_pattern = ~pattern; |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 923 | temp = addr[offset]; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 924 | if (temp != anti_pattern) { |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 925 | printf("\nFAILURE (read/write): @ 0x%.8lx:" |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 926 | " expected 0x%.8lx, actual 0x%.8lx)\n", |
David Feng | f40b221 | 2014-02-12 16:10:08 +0800 | [diff] [blame] | 927 | start_addr + offset*sizeof(vu_long), |
| 928 | anti_pattern, temp); |
Paul Gortmaker | cb47b4f | 2009-10-02 18:18:33 -0400 | [diff] [blame] | 929 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 930 | if (ctrlc()) |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 931 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 932 | } |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 933 | addr[offset] = 0; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 934 | } |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 935 | |
Rasmus Villemoes | abd06c9 | 2016-01-07 11:36:04 +0100 | [diff] [blame] | 936 | return errs; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Stefan Roese | fbfd5a1 | 2020-03-05 07:21:32 +0100 | [diff] [blame] | 939 | static int compare_regions(volatile unsigned long *bufa, |
| 940 | volatile unsigned long *bufb, size_t count) |
| 941 | { |
| 942 | volatile unsigned long *p1 = bufa; |
| 943 | volatile unsigned long *p2 = bufb; |
| 944 | int errs = 0; |
| 945 | size_t i; |
| 946 | |
| 947 | for (i = 0; i < count; i++, p1++, p2++) { |
| 948 | if (*p1 != *p2) { |
| 949 | printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n", |
| 950 | (unsigned long)*p1, (unsigned long)*p2, |
| 951 | *p1 ^ *p2, __ffs(*p1 ^ *p2), |
| 952 | (unsigned long)(i * sizeof(unsigned long))); |
| 953 | errs++; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | return errs; |
| 958 | } |
| 959 | |
| 960 | static ulong test_bitflip_comparison(volatile unsigned long *bufa, |
| 961 | volatile unsigned long *bufb, size_t count) |
| 962 | { |
| 963 | volatile unsigned long *p1 = bufa; |
| 964 | volatile unsigned long *p2 = bufb; |
| 965 | unsigned int j, k; |
| 966 | unsigned long q; |
| 967 | size_t i; |
| 968 | int max; |
| 969 | int errs = 0; |
| 970 | |
| 971 | max = sizeof(unsigned long) * 8; |
| 972 | for (k = 0; k < max; k++) { |
| 973 | q = 0x00000001L << k; |
| 974 | for (j = 0; j < 8; j++) { |
| 975 | WATCHDOG_RESET(); |
| 976 | q = ~q; |
| 977 | p1 = (volatile unsigned long *)bufa; |
| 978 | p2 = (volatile unsigned long *)bufb; |
| 979 | for (i = 0; i < count; i++) |
| 980 | *p1++ = *p2++ = (i % 2) == 0 ? q : ~q; |
| 981 | |
| 982 | errs += compare_regions(bufa, bufb, count); |
| 983 | } |
| 984 | |
| 985 | if (ctrlc()) |
| 986 | return -1UL; |
| 987 | } |
| 988 | |
| 989 | return errs; |
| 990 | } |
| 991 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 992 | static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr, |
| 993 | vu_long pattern, int iteration) |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 994 | { |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 995 | vu_long *end; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 996 | vu_long *addr; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 997 | ulong errs = 0; |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 998 | ulong incr, length; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 999 | ulong val, readback; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1000 | |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1001 | /* Alternate the pattern */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1002 | incr = 1; |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1003 | if (iteration & 1) { |
| 1004 | incr = -incr; |
| 1005 | /* |
| 1006 | * Flip the pattern each time to make lots of zeros and |
| 1007 | * then, the next time, lots of ones. We decrement |
| 1008 | * the "negative" patterns and increment the "positive" |
| 1009 | * patterns to preserve this feature. |
| 1010 | */ |
| 1011 | if (pattern & 0x80000000) |
| 1012 | pattern = -pattern; /* complement & increment */ |
| 1013 | else |
| 1014 | pattern = ~pattern; |
| 1015 | } |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1016 | length = (end_addr - start_addr) / sizeof(ulong); |
| 1017 | end = buf + length; |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1018 | printf("\rPattern %08lX Writing..." |
| 1019 | "%12s" |
| 1020 | "\b\b\b\b\b\b\b\b\b\b", |
| 1021 | pattern, ""); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1022 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1023 | for (addr = buf, val = pattern; addr < end; addr++) { |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1024 | WATCHDOG_RESET(); |
| 1025 | *addr = val; |
| 1026 | val += incr; |
| 1027 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1028 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1029 | puts("Reading..."); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1030 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1031 | for (addr = buf, val = pattern; addr < end; addr++) { |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1032 | WATCHDOG_RESET(); |
| 1033 | readback = *addr; |
| 1034 | if (readback != val) { |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1035 | ulong offset = addr - buf; |
| 1036 | |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1037 | printf("\nMem error @ 0x%08X: " |
| 1038 | "found %08lX, expected %08lX\n", |
David Feng | f40b221 | 2014-02-12 16:10:08 +0800 | [diff] [blame] | 1039 | (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)), |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1040 | readback, val); |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1041 | errs++; |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 1042 | if (ctrlc()) |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1043 | return -1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1044 | } |
Simon Glass | 130103e | 2013-02-24 17:33:18 +0000 | [diff] [blame] | 1045 | val += incr; |
| 1046 | } |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1047 | |
Rasmus Villemoes | abd06c9 | 2016-01-07 11:36:04 +0100 | [diff] [blame] | 1048 | return errs; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Perform a memory test. A more complete alternative test can be |
| 1053 | * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until |
| 1054 | * interrupted by ctrl-c or by a failure of one of the sub-tests. |
| 1055 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1056 | static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1057 | char *const argv[]) |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1058 | { |
Simon Glass | 5107525 | 2013-02-24 17:33:20 +0000 | [diff] [blame] | 1059 | ulong start, end; |
Michal Simek | 9eaf644 | 2020-05-04 13:54:40 +0200 | [diff] [blame] | 1060 | vu_long scratch_space; |
| 1061 | vu_long *buf, *dummy = &scratch_space; |
Tom Rini | b918088 | 2015-04-07 09:38:54 -0400 | [diff] [blame] | 1062 | ulong iteration_limit = 0; |
Stefan Roese | 32265fc | 2020-03-05 07:21:29 +0100 | [diff] [blame] | 1063 | ulong count = 0; |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1064 | ulong errs = 0; /* number of errors, or -1 if interrupted */ |
Pavel Machek | d319f59 | 2015-04-01 13:50:41 +0200 | [diff] [blame] | 1065 | ulong pattern = 0; |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1066 | int iteration; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1067 | |
Pavel Machek | d319f59 | 2015-04-01 13:50:41 +0200 | [diff] [blame] | 1068 | start = CONFIG_SYS_MEMTEST_START; |
| 1069 | end = CONFIG_SYS_MEMTEST_END; |
| 1070 | |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1071 | if (argc > 1) |
Pavel Machek | d319f59 | 2015-04-01 13:50:41 +0200 | [diff] [blame] | 1072 | if (strict_strtoul(argv[1], 16, &start) < 0) |
| 1073 | return CMD_RET_USAGE; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (argc > 2) |
Pavel Machek | d319f59 | 2015-04-01 13:50:41 +0200 | [diff] [blame] | 1076 | if (strict_strtoul(argv[2], 16, &end) < 0) |
| 1077 | return CMD_RET_USAGE; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (argc > 3) |
Pavel Machek | d319f59 | 2015-04-01 13:50:41 +0200 | [diff] [blame] | 1080 | if (strict_strtoul(argv[3], 16, &pattern) < 0) |
| 1081 | return CMD_RET_USAGE; |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (argc > 4) |
Pavel Machek | d319f59 | 2015-04-01 13:50:41 +0200 | [diff] [blame] | 1084 | if (strict_strtoul(argv[4], 16, &iteration_limit) < 0) |
| 1085 | return CMD_RET_USAGE; |
| 1086 | |
| 1087 | if (end < start) { |
| 1088 | printf("Refusing to do empty test\n"); |
| 1089 | return -1; |
| 1090 | } |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1091 | |
Michal Simek | 45f8e33 | 2016-02-24 08:36:02 +0100 | [diff] [blame] | 1092 | printf("Testing %08lx ... %08lx:\n", start, end); |
Simon Glass | 5107525 | 2013-02-24 17:33:20 +0000 | [diff] [blame] | 1093 | debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__, |
| 1094 | start, end); |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1095 | |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1096 | buf = map_sysmem(start, end - start); |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1097 | for (iteration = 0; |
| 1098 | !iteration_limit || iteration < iteration_limit; |
| 1099 | iteration++) { |
| 1100 | if (ctrlc()) { |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1101 | errs = -1UL; |
| 1102 | break; |
| 1103 | } |
| 1104 | |
| 1105 | printf("Iteration: %6d\r", iteration + 1); |
| 1106 | debug("\n"); |
Stefan Roese | 1eb8b1e | 2020-03-05 07:21:31 +0100 | [diff] [blame] | 1107 | if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) { |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1108 | errs = mem_test_alt(buf, start, end, dummy); |
Stefan Roese | fbfd5a1 | 2020-03-05 07:21:32 +0100 | [diff] [blame] | 1109 | if (errs == -1UL) |
| 1110 | break; |
| 1111 | count += errs; |
| 1112 | errs = test_bitflip_comparison(buf, |
| 1113 | buf + (end - start) / 2, |
| 1114 | (end - start) / |
| 1115 | sizeof(unsigned long)); |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1116 | } else { |
| 1117 | errs = mem_test_quick(buf, start, end, pattern, |
| 1118 | iteration); |
| 1119 | } |
| 1120 | if (errs == -1UL) |
| 1121 | break; |
Stefan Roese | 32265fc | 2020-03-05 07:21:29 +0100 | [diff] [blame] | 1122 | count += errs; |
Simon Glass | ad02a01 | 2013-02-28 17:47:14 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Stefan Roese | c1d47db | 2020-03-05 07:21:30 +0100 | [diff] [blame] | 1125 | unmap_sysmem((void *)buf); |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (errs == -1UL) { |
Simon Glass | eb4598b | 2013-02-24 17:33:19 +0000 | [diff] [blame] | 1128 | /* Memory test was aborted - write a newline to finish off */ |
| 1129 | putc('\n'); |
Simon Glass | 57ee443 | 2013-02-24 17:33:17 +0000 | [diff] [blame] | 1130 | } |
Stefan Roese | 32265fc | 2020-03-05 07:21:29 +0100 | [diff] [blame] | 1131 | printf("Tested %d iteration(s) with %lu errors.\n", iteration, count); |
Simon Glass | 8f2c758 | 2013-02-24 17:33:16 +0000 | [diff] [blame] | 1132 | |
Stefan Roese | 32265fc | 2020-03-05 07:21:29 +0100 | [diff] [blame] | 1133 | return errs != 0; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1134 | } |
Wolfgang Denk | 9d00928 | 2013-03-08 10:51:32 +0000 | [diff] [blame] | 1135 | #endif /* CONFIG_CMD_MEMTEST */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* Modify memory. |
| 1138 | * |
| 1139 | * Syntax: |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 1140 | * mm{.b, .w, .l, .q} {addr} |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1141 | */ |
| 1142 | static int |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1143 | mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc, |
| 1144 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1145 | { |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 1146 | ulong addr; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 1147 | ulong i; /* 64-bit if SUPPORT_64BIT_DATA */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 1148 | int nbytes, size; |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 1149 | void *ptr = NULL; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1150 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 1151 | if (argc != 2) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1152 | return CMD_RET_USAGE; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1153 | |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 1154 | bootretry_reset_cmd_timeout(); /* got a good command to get here */ |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1155 | /* We use the last specified parameters, unless new ones are |
| 1156 | * entered. |
| 1157 | */ |
| 1158 | addr = mm_last_addr; |
| 1159 | size = mm_last_size; |
| 1160 | |
| 1161 | if ((flag & CMD_FLAG_REPEAT) == 0) { |
| 1162 | /* New command specified. Check for a size specification. |
| 1163 | * Defaults to long if no or incorrect specification. |
| 1164 | */ |
wdenk | 874ac26 | 2003-07-24 23:38:38 +0000 | [diff] [blame] | 1165 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
| 1166 | return 1; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1167 | |
| 1168 | /* Address is specified since argc > 1 |
| 1169 | */ |
| 1170 | addr = simple_strtoul(argv[1], NULL, 16); |
| 1171 | addr += base_address; |
| 1172 | } |
wdenk | 381669a | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 1173 | |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1174 | /* Print the address, followed by value. Then accept input for |
| 1175 | * the next value. A non-converted value exits. |
| 1176 | */ |
| 1177 | do { |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 1178 | ptr = map_sysmem(addr, size); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1179 | printf("%08lx:", addr); |
| 1180 | if (size == 4) |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 1181 | printf(" %08x", *((u32 *)ptr)); |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 1182 | else if (SUPPORT_64BIT_DATA && size == 8) |
| 1183 | printf(" %0lx", *((ulong *)ptr)); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1184 | else if (size == 2) |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 1185 | printf(" %04x", *((u16 *)ptr)); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1186 | else |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 1187 | printf(" %02x", *((u8 *)ptr)); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1188 | |
Simon Glass | be6aafc | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 1189 | nbytes = cli_readline(" ? "); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1190 | if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) { |
| 1191 | /* <CR> pressed as only input, don't modify current |
| 1192 | * location and move to next. "-" pressed will go back. |
| 1193 | */ |
| 1194 | if (incrflag) |
| 1195 | addr += nbytes ? -size : size; |
| 1196 | nbytes = 1; |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 1197 | /* good enough to not time out */ |
| 1198 | bootretry_reset_cmd_timeout(); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1199 | } |
| 1200 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 1201 | else if (nbytes == -2) { |
| 1202 | break; /* timed out, exit the command */ |
| 1203 | } |
| 1204 | #endif |
| 1205 | else { |
| 1206 | char *endp; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 1207 | if (SUPPORT_64BIT_DATA) |
| 1208 | i = simple_strtoull(console_buffer, &endp, 16); |
| 1209 | else |
| 1210 | i = simple_strtoul(console_buffer, &endp, 16); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1211 | nbytes = endp - console_buffer; |
| 1212 | if (nbytes) { |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1213 | /* good enough to not time out |
| 1214 | */ |
Simon Glass | 09007c4 | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 1215 | bootretry_reset_cmd_timeout(); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1216 | if (size == 4) |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 1217 | *((u32 *)ptr) = i; |
Simon Glass | d1c117b | 2020-06-02 19:26:46 -0600 | [diff] [blame] | 1218 | else if (SUPPORT_64BIT_DATA && size == 8) |
| 1219 | *((ulong *)ptr) = i; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1220 | else if (size == 2) |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 1221 | *((u16 *)ptr) = i; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1222 | else |
York Sun | d8a405d | 2014-02-12 15:55:35 -0800 | [diff] [blame] | 1223 | *((u8 *)ptr) = i; |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1224 | if (incrflag) |
| 1225 | addr += size; |
| 1226 | } |
| 1227 | } |
| 1228 | } while (nbytes); |
Simon Glass | e6d0ca2 | 2013-02-24 17:33:15 +0000 | [diff] [blame] | 1229 | if (ptr) |
| 1230 | unmap_sysmem(ptr); |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1231 | |
| 1232 | mm_last_addr = addr; |
| 1233 | mm_last_size = size; |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Mike Frysinger | 321ab9e | 2010-12-21 14:19:51 -0500 | [diff] [blame] | 1237 | #ifdef CONFIG_CMD_CRC32 |
| 1238 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1239 | static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1240 | char *const argv[]) |
wdenk | 3863585 | 2002-08-27 05:55:31 +0000 | [diff] [blame] | 1241 | { |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 1242 | int flags = 0; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1243 | int ac; |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 1244 | char * const *av; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1245 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 1246 | if (argc < 3) |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 1247 | return CMD_RET_USAGE; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1248 | |
| 1249 | av = argv + 1; |
| 1250 | ac = argc - 1; |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 1251 | #ifdef CONFIG_CRC32_VERIFY |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1252 | if (strcmp(*av, "-v") == 0) { |
Joe Hershberger | 5fcee9a | 2015-05-05 12:23:53 -0500 | [diff] [blame] | 1253 | flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1254 | av++; |
| 1255 | ac--; |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1256 | } |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 1257 | #endif |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1258 | |
Simon Glass | 0bbd76f | 2013-02-24 20:30:22 +0000 | [diff] [blame] | 1259 | return hash_command("crc32", flags, cmdtp, flag, ac, av); |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1260 | } |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1261 | |
Robin Getz | 93d6cb0 | 2009-07-27 00:07:59 -0400 | [diff] [blame] | 1262 | #endif |
| 1263 | |
Jean-Jacques Hiblot | d3f0937 | 2019-07-02 14:23:26 +0200 | [diff] [blame] | 1264 | #ifdef CONFIG_CMD_RANDOM |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1265 | static int do_random(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1266 | char *const argv[]) |
Jean-Jacques Hiblot | d3f0937 | 2019-07-02 14:23:26 +0200 | [diff] [blame] | 1267 | { |
| 1268 | unsigned long addr, len; |
| 1269 | unsigned long seed; // NOT INITIALIZED ON PURPOSE |
| 1270 | unsigned int *buf, *start; |
| 1271 | unsigned char *buf8; |
| 1272 | unsigned int i; |
| 1273 | |
Eugeniy Paltsev | 82accda | 2020-03-20 19:38:17 +0300 | [diff] [blame] | 1274 | if (argc < 3 || argc > 4) |
| 1275 | return CMD_RET_USAGE; |
Jean-Jacques Hiblot | d3f0937 | 2019-07-02 14:23:26 +0200 | [diff] [blame] | 1276 | |
| 1277 | len = simple_strtoul(argv[2], NULL, 16); |
| 1278 | addr = simple_strtoul(argv[1], NULL, 16); |
| 1279 | |
| 1280 | if (argc == 4) { |
| 1281 | seed = simple_strtoul(argv[3], NULL, 16); |
| 1282 | if (seed == 0) { |
| 1283 | printf("The seed cannot be 0. Using 0xDEADBEEF.\n"); |
| 1284 | seed = 0xDEADBEEF; |
| 1285 | } |
| 1286 | } else { |
| 1287 | seed = get_timer(0) ^ rand(); |
| 1288 | } |
| 1289 | |
| 1290 | srand(seed); |
| 1291 | start = map_sysmem(addr, len); |
| 1292 | buf = start; |
| 1293 | for (i = 0; i < (len / 4); i++) |
| 1294 | *buf++ = rand(); |
| 1295 | |
| 1296 | buf8 = (unsigned char *)buf; |
| 1297 | for (i = 0; i < (len % 4); i++) |
| 1298 | *buf8++ = rand() & 0xFF; |
| 1299 | |
| 1300 | unmap_sysmem(start); |
| 1301 | printf("%lu bytes filled with random data\n", len); |
Eugeniy Paltsev | 82accda | 2020-03-20 19:38:17 +0300 | [diff] [blame] | 1302 | |
| 1303 | return CMD_RET_SUCCESS; |
Jean-Jacques Hiblot | d3f0937 | 2019-07-02 14:23:26 +0200 | [diff] [blame] | 1304 | } |
| 1305 | #endif |
| 1306 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1307 | /**************************************************/ |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1308 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1309 | md, 3, 1, do_mem_md, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1310 | "memory display", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1311 | "[.b, .w, .l" HELP_Q "] address [# of objects]" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1312 | ); |
| 1313 | |
| 1314 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1315 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1316 | mm, 2, 1, do_mem_mm, |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1317 | "memory modify (auto-incrementing address)", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1318 | "[.b, .w, .l" HELP_Q "] address" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1319 | ); |
| 1320 | |
| 1321 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1322 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1323 | nm, 2, 1, do_mem_nm, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1324 | "memory modify (constant address)", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1325 | "[.b, .w, .l" HELP_Q "] address" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1326 | ); |
| 1327 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1328 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1329 | mw, 4, 1, do_mem_mw, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1330 | "memory write (fill)", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1331 | "[.b, .w, .l" HELP_Q "] address value [count]" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1332 | ); |
| 1333 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1334 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1335 | cp, 4, 1, do_mem_cp, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1336 | "memory copy", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1337 | "[.b, .w, .l" HELP_Q "] source target count" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1338 | ); |
| 1339 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1340 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1341 | cmp, 4, 1, do_mem_cmp, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1342 | "memory compare", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1343 | "[.b, .w, .l" HELP_Q "] addr1 addr2 count" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1344 | ); |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 1345 | |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 1346 | #ifdef CONFIG_CMD_MEM_SEARCH |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 1347 | /**************************************************/ |
| 1348 | U_BOOT_CMD( |
| 1349 | ms, 255, 1, do_mem_search, |
| 1350 | "memory search", |
| 1351 | "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..." |
Simon Glass | 573c0fa | 2020-07-28 19:41:14 -0600 | [diff] [blame] | 1352 | " -q = quiet, -l<val> = match limit" |
Simon Glass | 19038de | 2020-06-02 19:26:49 -0600 | [diff] [blame] | 1353 | ); |
| 1354 | #endif |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1355 | |
Mike Frysinger | 321ab9e | 2010-12-21 14:19:51 -0500 | [diff] [blame] | 1356 | #ifdef CONFIG_CMD_CRC32 |
| 1357 | |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 1358 | #ifndef CONFIG_CRC32_VERIFY |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1359 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1360 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1361 | crc32, 4, 1, do_mem_crc, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1362 | "checksum calculation", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1363 | "address count [addr]\n - compute CRC32 checksum [save at addr]" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1364 | ); |
| 1365 | |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 1366 | #else /* CONFIG_CRC32_VERIFY */ |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1367 | |
| 1368 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1369 | crc32, 5, 1, do_mem_crc, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1370 | "checksum calculation", |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1371 | "address count [addr]\n - compute CRC32 checksum [save at addr]\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1372 | "-v address count crc\n - verify crc of memory area" |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1373 | ); |
| 1374 | |
Daniel Thompson | a9e2c67 | 2017-05-19 17:26:58 +0100 | [diff] [blame] | 1375 | #endif /* CONFIG_CRC32_VERIFY */ |
wdenk | 6203e40 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 1376 | |
Mike Frysinger | 321ab9e | 2010-12-21 14:19:51 -0500 | [diff] [blame] | 1377 | #endif |
| 1378 | |
Simon Glass | eacd14f | 2012-11-30 13:01:20 +0000 | [diff] [blame] | 1379 | #ifdef CONFIG_CMD_MEMINFO |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 1380 | static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 1381 | char *const argv[]) |
Simon Glass | eacd14f | 2012-11-30 13:01:20 +0000 | [diff] [blame] | 1382 | { |
Simon Glass | 320a5d4 | 2019-11-14 12:57:44 -0700 | [diff] [blame] | 1383 | puts("DRAM: "); |
| 1384 | print_size(gd->ram_size, "\n"); |
Simon Glass | eacd14f | 2012-11-30 13:01:20 +0000 | [diff] [blame] | 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | #endif |
| 1389 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1390 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1391 | base, 2, 1, do_mem_base, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1392 | "print or set address offset", |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1393 | "\n - print address offset for memory commands\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1394 | "base off\n - set address offset for memory commands to 'off'" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1395 | ); |
| 1396 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1397 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1398 | loop, 3, 1, do_mem_loop, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1399 | "infinite loop on address range", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1400 | "[.b, .w, .l" HELP_Q "] address number_of_objects" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1401 | ); |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 1402 | |
| 1403 | #ifdef CONFIG_LOOPW |
| 1404 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1405 | loopw, 4, 1, do_mem_loopw, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1406 | "infinite write loop on address range", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1407 | "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write" |
wdenk | 6451936 | 2004-07-11 17:40:54 +0000 | [diff] [blame] | 1408 | ); |
| 1409 | #endif /* CONFIG_LOOPW */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1410 | |
Wolfgang Denk | 9d00928 | 2013-03-08 10:51:32 +0000 | [diff] [blame] | 1411 | #ifdef CONFIG_CMD_MEMTEST |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 1412 | U_BOOT_CMD( |
Dirk Eibach | b265989 | 2008-12-16 14:51:56 +0100 | [diff] [blame] | 1413 | mtest, 5, 1, do_mem_mtest, |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 1414 | "simple RAM read/write test", |
| 1415 | "[start [end [pattern [iterations]]]]" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1416 | ); |
Wolfgang Denk | 9d00928 | 2013-03-08 10:51:32 +0000 | [diff] [blame] | 1417 | #endif /* CONFIG_CMD_MEMTEST */ |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 1418 | |
Joel Johnson | db5a97e | 2020-01-29 09:17:18 -0700 | [diff] [blame] | 1419 | #ifdef CONFIG_CMD_MX_CYCLIC |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 1420 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1421 | mdc, 4, 1, do_mem_mdc, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1422 | "memory display cyclic", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1423 | "[.b, .w, .l" HELP_Q "] address count delay(ms)" |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 1424 | ); |
| 1425 | |
| 1426 | U_BOOT_CMD( |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 1427 | mwc, 4, 1, do_mem_mwc, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 1428 | "memory write cyclic", |
Simon Glass | 683e39e | 2020-06-02 19:26:45 -0600 | [diff] [blame] | 1429 | "[.b, .w, .l" HELP_Q "] address value delay(ms)" |
stroese | fb5e0ac | 2004-12-16 17:42:39 +0000 | [diff] [blame] | 1430 | ); |
Joel Johnson | db5a97e | 2020-01-29 09:17:18 -0700 | [diff] [blame] | 1431 | #endif /* CONFIG_CMD_MX_CYCLIC */ |
Simon Glass | eacd14f | 2012-11-30 13:01:20 +0000 | [diff] [blame] | 1432 | |
| 1433 | #ifdef CONFIG_CMD_MEMINFO |
| 1434 | U_BOOT_CMD( |
| 1435 | meminfo, 3, 1, do_mem_info, |
| 1436 | "display memory information", |
| 1437 | "" |
| 1438 | ); |
| 1439 | #endif |
Jean-Jacques Hiblot | d3f0937 | 2019-07-02 14:23:26 +0200 | [diff] [blame] | 1440 | |
| 1441 | #ifdef CONFIG_CMD_RANDOM |
| 1442 | U_BOOT_CMD( |
| 1443 | random, 4, 0, do_random, |
| 1444 | "fill memory with random pattern", |
| 1445 | "<addr> <len> [<seed>]\n" |
| 1446 | " - Fill 'len' bytes of memory starting at 'addr' with random data\n" |
| 1447 | ); |
| 1448 | #endif |