cmd_sata.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
  18. int __sata_initialize(void)
  19. {
  20. int rc;
  21. int i;
  22. for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
  23. memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
  24. sata_dev_desc[i].if_type = IF_TYPE_SATA;
  25. sata_dev_desc[i].dev = i;
  26. sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  27. sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
  28. sata_dev_desc[i].lba = 0;
  29. sata_dev_desc[i].blksz = 512;
  30. sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
  31. sata_dev_desc[i].block_read = sata_read;
  32. sata_dev_desc[i].block_write = sata_write;
  33. rc = init_sata(i);
  34. if (!rc) {
  35. rc = scan_sata(i);
  36. if (!rc && (sata_dev_desc[i].lba > 0) &&
  37. (sata_dev_desc[i].blksz > 0))
  38. init_part(&sata_dev_desc[i]);
  39. }
  40. }
  41. sata_curr_device = 0;
  42. return rc;
  43. }
  44. int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
  45. #ifdef CONFIG_PARTITIONS
  46. block_dev_desc_t *sata_get_dev(int dev)
  47. {
  48. return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
  49. }
  50. #endif
  51. static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  52. {
  53. int rc = 0;
  54. if (argc == 2 && strcmp(argv[1], "init") == 0)
  55. return sata_initialize();
  56. /* If the user has not yet run `sata init`, do it now */
  57. if (sata_curr_device == -1)
  58. if (sata_initialize())
  59. return 1;
  60. switch (argc) {
  61. case 0:
  62. case 1:
  63. return CMD_RET_USAGE;
  64. case 2:
  65. if (strncmp(argv[1],"inf", 3) == 0) {
  66. int i;
  67. putc('\n');
  68. for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
  69. if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
  70. continue;
  71. printf ("SATA device %d: ", i);
  72. dev_print(&sata_dev_desc[i]);
  73. }
  74. return 0;
  75. } else if (strncmp(argv[1],"dev", 3) == 0) {
  76. if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
  77. puts("\nno SATA devices available\n");
  78. return 1;
  79. }
  80. printf("\nSATA device %d: ", sata_curr_device);
  81. dev_print(&sata_dev_desc[sata_curr_device]);
  82. return 0;
  83. } else if (strncmp(argv[1],"part",4) == 0) {
  84. int dev, ok;
  85. for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
  86. if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  87. ++ok;
  88. if (dev)
  89. putc ('\n');
  90. print_part(&sata_dev_desc[dev]);
  91. }
  92. }
  93. if (!ok) {
  94. puts("\nno SATA devices available\n");
  95. rc ++;
  96. }
  97. return rc;
  98. }
  99. return CMD_RET_USAGE;
  100. case 3:
  101. if (strncmp(argv[1], "dev", 3) == 0) {
  102. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  103. printf("\nSATA device %d: ", dev);
  104. if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
  105. puts ("unknown device\n");
  106. return 1;
  107. }
  108. dev_print(&sata_dev_desc[dev]);
  109. if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
  110. return 1;
  111. sata_curr_device = dev;
  112. puts("... is now current device\n");
  113. return 0;
  114. } else if (strncmp(argv[1], "part", 4) == 0) {
  115. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  116. if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  117. print_part(&sata_dev_desc[dev]);
  118. } else {
  119. printf("\nSATA device %d not available\n", dev);
  120. rc = 1;
  121. }
  122. return rc;
  123. }
  124. return CMD_RET_USAGE;
  125. default: /* at least 4 args */
  126. if (strcmp(argv[1], "read") == 0) {
  127. ulong addr = simple_strtoul(argv[2], NULL, 16);
  128. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  129. ulong n;
  130. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  131. printf("\nSATA read: device %d block # %ld, count %ld ... ",
  132. sata_curr_device, blk, cnt);
  133. n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
  134. /* flush cache after read */
  135. flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
  136. printf("%ld blocks read: %s\n",
  137. n, (n==cnt) ? "OK" : "ERROR");
  138. return (n == cnt) ? 0 : 1;
  139. } else if (strcmp(argv[1], "write") == 0) {
  140. ulong addr = simple_strtoul(argv[2], NULL, 16);
  141. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  142. ulong n;
  143. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  144. printf("\nSATA write: device %d block # %ld, count %ld ... ",
  145. sata_curr_device, blk, cnt);
  146. n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
  147. printf("%ld blocks written: %s\n",
  148. n, (n == cnt) ? "OK" : "ERROR");
  149. return (n == cnt) ? 0 : 1;
  150. } else {
  151. return CMD_RET_USAGE;
  152. }
  153. return rc;
  154. }
  155. }
  156. U_BOOT_CMD(
  157. sata, 5, 1, do_sata,
  158. "SATA sub system",
  159. "init - init SATA sub system\n"
  160. "sata info - show available SATA devices\n"
  161. "sata device [dev] - show or set current device\n"
  162. "sata part [dev] - print partition table\n"
  163. "sata read addr blk# cnt\n"
  164. "sata write addr blk# cnt"
  165. );