sata.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2000-2005, DENX Software Engineering
  3. * Wolfgang Denk <wd@denx.de>
  4. * Copyright (C) Procsys. All rights reserved.
  5. * Mushtaq Khan <mushtaq_k@procsys.com>
  6. * <mushtaqk_921@yahoo.co.in>
  7. * Copyright (C) 2008 Freescale Semiconductor, Inc.
  8. * Dave Liu <daveliu@freescale.com>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <command.h>
  14. #include <part.h>
  15. #include <sata.h>
  16. static int sata_curr_device = -1;
  17. static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  18. {
  19. int rc = 0;
  20. if (argc == 2 && strcmp(argv[1], "stop") == 0)
  21. return sata_stop();
  22. if (argc == 2 && strcmp(argv[1], "init") == 0) {
  23. if (sata_curr_device != -1)
  24. sata_stop();
  25. return sata_initialize();
  26. }
  27. /* If the user has not yet run `sata init`, do it now */
  28. if (sata_curr_device == -1)
  29. if (sata_initialize())
  30. return 1;
  31. switch (argc) {
  32. case 0:
  33. case 1:
  34. return CMD_RET_USAGE;
  35. case 2:
  36. if (strncmp(argv[1], "inf", 3) == 0) {
  37. blk_list_devices(IF_TYPE_SATA);
  38. return 0;
  39. } else if (strncmp(argv[1], "dev", 3) == 0) {
  40. if (blk_print_device_num(IF_TYPE_SATA,
  41. sata_curr_device)) {
  42. printf("\nno SATA devices available\n");
  43. return CMD_RET_FAILURE;
  44. }
  45. return 0;
  46. } else if (strncmp(argv[1], "part", 4) == 0) {
  47. if (blk_list_part(IF_TYPE_SATA))
  48. puts("\nno SATA devices available\n");
  49. return 0;
  50. }
  51. return CMD_RET_USAGE;
  52. case 3:
  53. if (strncmp(argv[1], "dev", 3) == 0) {
  54. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  55. if (!blk_show_device(IF_TYPE_SATA, dev)) {
  56. sata_curr_device = dev;
  57. printf("... is now current device\n");
  58. } else {
  59. return CMD_RET_FAILURE;
  60. }
  61. return 0;
  62. } else if (strncmp(argv[1], "part", 4) == 0) {
  63. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  64. if (blk_print_part_devnum(IF_TYPE_SATA, dev)) {
  65. printf("\nSATA device %d not available\n",
  66. dev);
  67. return CMD_RET_FAILURE;
  68. }
  69. return rc;
  70. }
  71. return CMD_RET_USAGE;
  72. default: /* at least 4 args */
  73. if (strcmp(argv[1], "read") == 0) {
  74. ulong addr = simple_strtoul(argv[2], NULL, 16);
  75. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  76. ulong n;
  77. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  78. printf("\nSATA read: device %d block # %ld, count %ld ... ",
  79. sata_curr_device, blk, cnt);
  80. n = blk_read_devnum(IF_TYPE_SATA, sata_curr_device, blk,
  81. cnt, (ulong *)addr);
  82. printf("%ld blocks read: %s\n",
  83. n, (n==cnt) ? "OK" : "ERROR");
  84. return (n == cnt) ? 0 : 1;
  85. } else if (strcmp(argv[1], "write") == 0) {
  86. ulong addr = simple_strtoul(argv[2], NULL, 16);
  87. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  88. ulong n;
  89. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  90. printf("\nSATA write: device %d block # %ld, count %ld ... ",
  91. sata_curr_device, blk, cnt);
  92. n = blk_write_devnum(IF_TYPE_SATA, sata_curr_device,
  93. blk, cnt, (ulong *)addr);
  94. printf("%ld blocks written: %s\n",
  95. n, (n == cnt) ? "OK" : "ERROR");
  96. return (n == cnt) ? 0 : 1;
  97. } else {
  98. return CMD_RET_USAGE;
  99. }
  100. return rc;
  101. }
  102. }
  103. U_BOOT_CMD(
  104. sata, 5, 1, do_sata,
  105. "SATA sub system",
  106. "init - init SATA sub system\n"
  107. "sata stop - disable SATA sub system\n"
  108. "sata info - show available SATA devices\n"
  109. "sata device [dev] - show or set current device\n"
  110. "sata part [dev] - print partition table\n"
  111. "sata read addr blk# cnt\n"
  112. "sata write addr blk# cnt"
  113. );