cmd_portio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * (C) Copyright 2003
  3. * Marc Singer, elf@buici.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Port I/O Functions
  25. *
  26. * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #if (CONFIG_COMMANDS & CFG_CMD_PORTIO)
  31. extern int cmd_get_data_size (char *arg, int default_size);
  32. /* Display values from last command.
  33. * Memory modify remembered values are different from display memory.
  34. */
  35. static uint in_last_addr, in_last_size;
  36. static uint out_last_addr, out_last_size, out_last_value;
  37. int do_portio_out (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  38. {
  39. uint addr = out_last_addr;
  40. uint size = out_last_size;
  41. uint value = out_last_value;
  42. if (argc != 3) {
  43. printf ("Usage:\n%s\n", cmdtp->usage);
  44. return 1;
  45. }
  46. if ((flag & CMD_FLAG_REPEAT) == 0) {
  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], 1);
  51. addr = simple_strtoul (argv[1], NULL, 16);
  52. value = simple_strtoul (argv[2], NULL, 16);
  53. }
  54. #if defined (CONFIG_X86)
  55. {
  56. unsigned short port = addr;
  57. switch (size) {
  58. default:
  59. case 1:
  60. {
  61. unsigned char ch = value;
  62. __asm__ volatile ("out %0, %%dx"::"a" (ch), "d" (port));
  63. }
  64. break;
  65. case 2:
  66. {
  67. unsigned short w = value;
  68. __asm__ volatile ("out %0, %%dx"::"a" (w), "d" (port));
  69. }
  70. break;
  71. case 4:
  72. __asm__ volatile ("out %0, %%dx"::"a" (value), "d" (port));
  73. break;
  74. }
  75. }
  76. #endif /* CONFIG_X86 */
  77. out_last_addr = addr;
  78. out_last_size = size;
  79. out_last_value = value;
  80. return 0;
  81. }
  82. cmd_tbl_t U_BOOT_CMD(PORTIO_OUT) = MK_CMD_ENTRY(
  83. "out", 3, 1, do_portio_out,
  84. "out - write datum to IO port\n",
  85. "[.b, .w, .l] port value\n - output to IO port\n"
  86. );
  87. int do_portio_in (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  88. {
  89. uint addr = in_last_addr;
  90. uint size = in_last_size;
  91. if (argc != 2) {
  92. printf ("Usage:\n%s\n", cmdtp->usage);
  93. return 1;
  94. }
  95. if ((flag & CMD_FLAG_REPEAT) == 0) {
  96. /* New command specified. Check for a size specification.
  97. * Defaults to long if no or incorrect specification.
  98. */
  99. size = cmd_get_data_size (argv[0], 1);
  100. addr = simple_strtoul (argv[1], NULL, 16);
  101. }
  102. #if defined (CONFIG_X86)
  103. {
  104. unsigned short port = addr;
  105. switch (size) {
  106. default:
  107. case 1:
  108. {
  109. unsigned char ch;
  110. __asm__ volatile ("in %%dx, %0":"=a" (ch):"d" (port));
  111. printf (" %02x\n", ch);
  112. }
  113. break;
  114. case 2:
  115. {
  116. unsigned short w;
  117. __asm__ volatile ("in %%dx, %0":"=a" (w):"d" (port));
  118. printf (" %04x\n", w);
  119. }
  120. break;
  121. case 4:
  122. {
  123. unsigned long l;
  124. __asm__ volatile ("in %%dx, %0":"=a" (l):"d" (port));
  125. printf (" %08lx\n", l);
  126. }
  127. break;
  128. }
  129. }
  130. #endif /* CONFIG_X86 */
  131. in_last_addr = addr;
  132. in_last_size = size;
  133. return 0;
  134. }
  135. cmd_tbl_t U_BOOT_CMD(PORTIO_IN) = MK_CMD_ENTRY(
  136. "in", 2, 1, do_portio_in,
  137. "in - read data from an IO port\n",
  138. "[.b, .w, .l] port\n"
  139. " - read datum from IO port\n"
  140. );
  141. #endif /* CFG_CMD_PORTIO */