io.c 2.4 KB

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