blob: 2de1111998fe6f5987480cceef503ca84c2188e2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vadim Bendebury9ad557b2012-12-05 14:46:42 +00002/*
3 * Copyright (c) 2012 The Chromium OS Authors.
Vadim Bendebury9ad557b2012-12-05 14:46:42 +00004 */
5
6/*
7 * IO space access commands.
8 */
9
10#include <common.h>
11#include <command.h>
Simon Glass4e4bf942022-07-31 12:28:48 -060012#include <display_options.h>
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000013#include <asm/io.h>
14
Simon Glass9da37762019-09-25 08:56:25 -060015/* Display values from last command */
16static ulong last_addr, last_size;
17static ulong last_length = 0x40;
18static ulong base_address;
19
20#define DISP_LINE_LEN 16
21
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000022/*
23 * IO Display
24 *
25 * Syntax:
26 * iod{.b, .w, .l} {addr}
27 */
Simon Glass09140112020-05-10 11:40:03 -060028int do_io_iod(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000029{
Simon Glass9da37762019-09-25 08:56:25 -060030 ulong addr, length, bytes;
31 u8 buf[DISP_LINE_LEN];
32 int size, todo;
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000033
Simon Glass9da37762019-09-25 08:56:25 -060034 /*
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 Bendebury9ad557b2012-12-05 14:46:42 +000043 return CMD_RET_USAGE;
44
Simon Glass9da37762019-09-25 08:56:25 -060045 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 Bendebury9ad557b2012-12-05 14:46:42 +000053
Simon Glass9da37762019-09-25 08:56:25 -060054 /* Address is specified since argc > 1 */
Simon Glass7e5f4602021-07-24 09:03:29 -060055 addr = hextoul(argv[1], NULL);
Simon Glass9da37762019-09-25 08:56:25 -060056 addr += base_address;
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000057
Simon Glass9da37762019-09-25 08:56:25 -060058 /*
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 Glass7e5f4602021-07-24 09:03:29 -060063 length = hextoul(argv[2], NULL);
Simon Glass9da37762019-09-25 08:56:25 -060064 }
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000065
Simon Glass9da37762019-09-25 08:56:25 -060066 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 Bendebury9ad557b2012-12-05 14:46:42 +000090
91 return 0;
92}
93
Simon Glass09140112020-05-10 11:40:03 -060094int do_io_iow(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000095{
Tom Rini9398b8c2017-05-10 15:20:13 -040096 ulong addr, val;
97 int size;
Vadim Bendebury9ad557b2012-12-05 14:46:42 +000098
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 Glass7e5f4602021-07-24 09:03:29 -0600106 addr = hextoul(argv[1], NULL);
107 val = hextoul(argv[2], NULL);
Vadim Bendebury9ad557b2012-12-05 14:46:42 +0000108
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 Glass9da37762019-09-25 08:56:25 -0600120U_BOOT_CMD(iod, 3, 1, do_io_iod,
Bin Mengd6418192014-10-20 16:14:53 +0800121 "IO space display", "[.b, .w, .l] address");
Vadim Bendebury9ad557b2012-12-05 14:46:42 +0000122
123U_BOOT_CMD(iow, 3, 0, do_io_iow,
Bin Mengd6418192014-10-20 16:14:53 +0800124 "IO space modify",
125 "[.b, .w, .l] address value");