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