cmd_sata.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. __weak int __sata_stop(void)
  46. {
  47. int i, err = 0;
  48. for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++)
  49. err |= reset_sata(i);
  50. if (err)
  51. printf("Could not reset some SATA devices\n");
  52. return err;
  53. }
  54. int sata_stop(void) __attribute__((weak, alias("__sata_stop")));
  55. #ifdef CONFIG_PARTITIONS
  56. block_dev_desc_t *sata_get_dev(int dev)
  57. {
  58. return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
  59. }
  60. #endif
  61. static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  62. {
  63. int rc = 0;
  64. if (argc == 2 && strcmp(argv[1], "stop") == 0)
  65. return sata_stop();
  66. if (argc == 2 && strcmp(argv[1], "init") == 0) {
  67. if (sata_curr_device != -1)
  68. sata_stop();
  69. return sata_initialize();
  70. }
  71. /* If the user has not yet run `sata init`, do it now */
  72. if (sata_curr_device == -1)
  73. if (sata_initialize())
  74. return 1;
  75. switch (argc) {
  76. case 0:
  77. case 1:
  78. return CMD_RET_USAGE;
  79. case 2:
  80. if (strncmp(argv[1],"inf", 3) == 0) {
  81. int i;
  82. putc('\n');
  83. for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
  84. if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
  85. continue;
  86. printf ("SATA device %d: ", i);
  87. dev_print(&sata_dev_desc[i]);
  88. }
  89. return 0;
  90. } else if (strncmp(argv[1],"dev", 3) == 0) {
  91. if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
  92. puts("\nno SATA devices available\n");
  93. return 1;
  94. }
  95. printf("\nSATA device %d: ", sata_curr_device);
  96. dev_print(&sata_dev_desc[sata_curr_device]);
  97. return 0;
  98. } else if (strncmp(argv[1],"part",4) == 0) {
  99. int dev, ok;
  100. for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
  101. if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  102. ++ok;
  103. if (dev)
  104. putc ('\n');
  105. print_part(&sata_dev_desc[dev]);
  106. }
  107. }
  108. if (!ok) {
  109. puts("\nno SATA devices available\n");
  110. rc ++;
  111. }
  112. return rc;
  113. }
  114. return CMD_RET_USAGE;
  115. case 3:
  116. if (strncmp(argv[1], "dev", 3) == 0) {
  117. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  118. printf("\nSATA device %d: ", dev);
  119. if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
  120. puts ("unknown device\n");
  121. return 1;
  122. }
  123. dev_print(&sata_dev_desc[dev]);
  124. if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
  125. return 1;
  126. sata_curr_device = dev;
  127. puts("... is now current device\n");
  128. return 0;
  129. } else if (strncmp(argv[1], "part", 4) == 0) {
  130. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  131. if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  132. print_part(&sata_dev_desc[dev]);
  133. } else {
  134. printf("\nSATA device %d not available\n", dev);
  135. rc = 1;
  136. }
  137. return rc;
  138. }
  139. return CMD_RET_USAGE;
  140. default: /* at least 4 args */
  141. if (strcmp(argv[1], "read") == 0) {
  142. ulong addr = simple_strtoul(argv[2], NULL, 16);
  143. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  144. ulong n;
  145. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  146. printf("\nSATA read: device %d block # %ld, count %ld ... ",
  147. sata_curr_device, blk, cnt);
  148. n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
  149. /* flush cache after read */
  150. flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
  151. printf("%ld blocks read: %s\n",
  152. n, (n==cnt) ? "OK" : "ERROR");
  153. return (n == cnt) ? 0 : 1;
  154. } else if (strcmp(argv[1], "write") == 0) {
  155. ulong addr = simple_strtoul(argv[2], NULL, 16);
  156. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  157. ulong n;
  158. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  159. printf("\nSATA write: device %d block # %ld, count %ld ... ",
  160. sata_curr_device, blk, cnt);
  161. n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
  162. printf("%ld blocks written: %s\n",
  163. n, (n == cnt) ? "OK" : "ERROR");
  164. return (n == cnt) ? 0 : 1;
  165. } else {
  166. return CMD_RET_USAGE;
  167. }
  168. return rc;
  169. }
  170. }
  171. U_BOOT_CMD(
  172. sata, 5, 1, do_sata,
  173. "SATA sub system",
  174. "init - init SATA sub system\n"
  175. "sata stop - disable SATA sub system\n"
  176. "sata info - show available SATA devices\n"
  177. "sata device [dev] - show or set current device\n"
  178. "sata part [dev] - print partition table\n"
  179. "sata read addr blk# cnt\n"
  180. "sata write addr blk# cnt"
  181. );