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