io.c 2.4 KB

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