Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * IO space access commands. |
| 8 | */ |
| 9 | |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 11 | #include <display_options.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 12 | #include <vsprintf.h> |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 15 | /* Display values from last command */ |
| 16 | static ulong last_addr, last_size; |
| 17 | static ulong last_length = 0x40; |
| 18 | static ulong base_address; |
| 19 | |
| 20 | #define DISP_LINE_LEN 16 |
| 21 | |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 22 | /* |
| 23 | * IO Display |
| 24 | * |
| 25 | * Syntax: |
| 26 | * iod{.b, .w, .l} {addr} |
| 27 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 28 | int do_io_iod(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 29 | { |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 30 | ulong addr, length, bytes; |
| 31 | u8 buf[DISP_LINE_LEN]; |
| 32 | int size, todo; |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 33 | |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 34 | /* |
| 35 | * We use the last specified parameters, unless new ones are |
| 36 | * entered. |
| 37 | */ |
| 38 | addr = last_addr; |
| 39 | size = last_size; |
| 40 | length = last_length; |
| 41 | |
| 42 | if (argc < 2) |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 43 | return CMD_RET_USAGE; |
| 44 | |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 45 | if ((flag & CMD_FLAG_REPEAT) == 0) { |
| 46 | /* |
| 47 | * New command specified. Check for a size specification. |
| 48 | * Defaults to long if no or incorrect specification. |
| 49 | */ |
| 50 | size = cmd_get_data_size(argv[0], 4); |
| 51 | if (size < 0) |
| 52 | return 1; |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 53 | |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 54 | /* Address is specified since argc > 1 */ |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 55 | addr = hextoul(argv[1], NULL); |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 56 | addr += base_address; |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 57 | |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 58 | /* |
| 59 | * If another parameter, it is the length to display. |
| 60 | * Length is the number of objects, not number of bytes. |
| 61 | */ |
| 62 | if (argc > 2) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 63 | length = hextoul(argv[2], NULL); |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 64 | } |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 65 | |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 66 | bytes = size * length; |
| 67 | |
| 68 | /* Print the lines */ |
| 69 | for (; bytes > 0; addr += todo) { |
| 70 | u8 *ptr = buf; |
| 71 | int i; |
| 72 | |
| 73 | todo = min(bytes, (ulong)DISP_LINE_LEN); |
| 74 | for (i = 0; i < todo; i += size, ptr += size) { |
| 75 | if (size == 4) |
| 76 | *(u32 *)ptr = inl(addr + i); |
| 77 | else if (size == 2) |
| 78 | *(u16 *)ptr = inw(addr + i); |
| 79 | else |
| 80 | *ptr = inb(addr + i); |
| 81 | } |
| 82 | print_buffer(addr, buf, size, todo / size, |
| 83 | DISP_LINE_LEN / size); |
| 84 | bytes -= todo; |
| 85 | } |
| 86 | |
| 87 | last_addr = addr; |
| 88 | last_length = length; |
| 89 | last_size = size; |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 94 | int do_io_iow(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 95 | { |
Tom Rini | e7f8aa1 | 2017-05-10 15:20:13 -0400 | [diff] [blame] | 96 | ulong addr, val; |
| 97 | int size; |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 98 | |
| 99 | if (argc != 3) |
| 100 | return CMD_RET_USAGE; |
| 101 | |
| 102 | size = cmd_get_data_size(argv[0], 4); |
| 103 | if (size < 0) |
| 104 | return 1; |
| 105 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 106 | addr = hextoul(argv[1], NULL); |
| 107 | val = hextoul(argv[2], NULL); |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 108 | |
| 109 | if (size == 4) |
| 110 | outl((u32) val, addr); |
| 111 | else if (size == 2) |
| 112 | outw((u16) val, addr); |
| 113 | else |
| 114 | outb((u8) val, addr); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /**************************************************/ |
Simon Glass | d45fc8f | 2019-09-25 08:56:25 -0600 | [diff] [blame] | 120 | U_BOOT_CMD(iod, 3, 1, do_io_iod, |
Bin Meng | 49d0333 | 2014-10-20 16:14:53 +0800 | [diff] [blame] | 121 | "IO space display", "[.b, .w, .l] address"); |
Vadim Bendebury | 607eebb | 2012-12-05 14:46:42 +0000 | [diff] [blame] | 122 | |
| 123 | U_BOOT_CMD(iow, 3, 0, do_io_iow, |
Bin Meng | 49d0333 | 2014-10-20 16:14:53 +0800 | [diff] [blame] | 124 | "IO space modify", |
| 125 | "[.b, .w, .l] address value"); |