sata.c 3.4 KB

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