sata.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. rc = sata_initialize();
  30. if (rc == -1)
  31. return rc;
  32. sata_curr_device = rc;
  33. }
  34. switch (argc) {
  35. case 0:
  36. case 1:
  37. return CMD_RET_USAGE;
  38. case 2:
  39. if (strncmp(argv[1], "inf", 3) == 0) {
  40. blk_list_devices(IF_TYPE_SATA);
  41. return 0;
  42. } else if (strncmp(argv[1], "dev", 3) == 0) {
  43. if (blk_print_device_num(IF_TYPE_SATA,
  44. sata_curr_device)) {
  45. printf("\nno SATA devices available\n");
  46. return CMD_RET_FAILURE;
  47. }
  48. return 0;
  49. } else if (strncmp(argv[1], "part", 4) == 0) {
  50. if (blk_list_part(IF_TYPE_SATA))
  51. puts("\nno SATA devices available\n");
  52. return 0;
  53. }
  54. return CMD_RET_USAGE;
  55. case 3:
  56. if (strncmp(argv[1], "dev", 3) == 0) {
  57. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  58. if (!blk_show_device(IF_TYPE_SATA, dev)) {
  59. sata_curr_device = dev;
  60. printf("... is now current device\n");
  61. } else {
  62. return CMD_RET_FAILURE;
  63. }
  64. return 0;
  65. } else if (strncmp(argv[1], "part", 4) == 0) {
  66. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  67. if (blk_print_part_devnum(IF_TYPE_SATA, dev)) {
  68. printf("\nSATA device %d not available\n",
  69. dev);
  70. return CMD_RET_FAILURE;
  71. }
  72. return rc;
  73. }
  74. return CMD_RET_USAGE;
  75. default: /* at least 4 args */
  76. if (strcmp(argv[1], "read") == 0) {
  77. ulong addr = simple_strtoul(argv[2], NULL, 16);
  78. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  79. ulong n;
  80. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  81. printf("\nSATA read: device %d block # %ld, count %ld ... ",
  82. sata_curr_device, blk, cnt);
  83. n = blk_read_devnum(IF_TYPE_SATA, sata_curr_device, blk,
  84. cnt, (ulong *)addr);
  85. printf("%ld blocks read: %s\n",
  86. n, (n==cnt) ? "OK" : "ERROR");
  87. return (n == cnt) ? 0 : 1;
  88. } else if (strcmp(argv[1], "write") == 0) {
  89. ulong addr = simple_strtoul(argv[2], NULL, 16);
  90. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  91. ulong n;
  92. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  93. printf("\nSATA write: device %d block # %ld, count %ld ... ",
  94. sata_curr_device, blk, cnt);
  95. n = blk_write_devnum(IF_TYPE_SATA, sata_curr_device,
  96. blk, cnt, (ulong *)addr);
  97. printf("%ld blocks written: %s\n",
  98. n, (n == cnt) ? "OK" : "ERROR");
  99. return (n == cnt) ? 0 : 1;
  100. } else {
  101. return CMD_RET_USAGE;
  102. }
  103. return rc;
  104. }
  105. }
  106. U_BOOT_CMD(
  107. sata, 5, 1, do_sata,
  108. "SATA sub system",
  109. "init - init SATA sub system\n"
  110. "sata stop - disable SATA sub system\n"
  111. "sata info - show available SATA devices\n"
  112. "sata device [dev] - show or set current device\n"
  113. "sata part [dev] - print partition table\n"
  114. "sata read addr blk# cnt\n"
  115. "sata write addr blk# cnt"
  116. );