cmd_sata.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <command.h>
  27. #include <part.h>
  28. #include <sata.h>
  29. static int sata_curr_device = -1;
  30. block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
  31. int __sata_initialize(void)
  32. {
  33. int rc;
  34. int i;
  35. for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
  36. memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
  37. sata_dev_desc[i].if_type = IF_TYPE_SATA;
  38. sata_dev_desc[i].dev = i;
  39. sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  40. sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
  41. sata_dev_desc[i].lba = 0;
  42. sata_dev_desc[i].blksz = 512;
  43. sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
  44. sata_dev_desc[i].block_read = sata_read;
  45. sata_dev_desc[i].block_write = sata_write;
  46. rc = init_sata(i);
  47. if (!rc) {
  48. rc = scan_sata(i);
  49. if (!rc && (sata_dev_desc[i].lba > 0) &&
  50. (sata_dev_desc[i].blksz > 0))
  51. init_part(&sata_dev_desc[i]);
  52. }
  53. }
  54. sata_curr_device = 0;
  55. return rc;
  56. }
  57. int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
  58. #ifdef CONFIG_PARTITIONS
  59. block_dev_desc_t *sata_get_dev(int dev)
  60. {
  61. return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
  62. }
  63. #endif
  64. static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  65. {
  66. int rc = 0;
  67. if (argc == 2 && strcmp(argv[1], "init") == 0)
  68. return sata_initialize();
  69. /* If the user has not yet run `sata init`, do it now */
  70. if (sata_curr_device == -1)
  71. if (sata_initialize())
  72. return 1;
  73. switch (argc) {
  74. case 0:
  75. case 1:
  76. return CMD_RET_USAGE;
  77. case 2:
  78. if (strncmp(argv[1],"inf", 3) == 0) {
  79. int i;
  80. putc('\n');
  81. for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
  82. if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
  83. continue;
  84. printf ("SATA device %d: ", i);
  85. dev_print(&sata_dev_desc[i]);
  86. }
  87. return 0;
  88. } else if (strncmp(argv[1],"dev", 3) == 0) {
  89. if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
  90. puts("\nno SATA devices available\n");
  91. return 1;
  92. }
  93. printf("\nSATA device %d: ", sata_curr_device);
  94. dev_print(&sata_dev_desc[sata_curr_device]);
  95. return 0;
  96. } else if (strncmp(argv[1],"part",4) == 0) {
  97. int dev, ok;
  98. for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
  99. if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  100. ++ok;
  101. if (dev)
  102. putc ('\n');
  103. print_part(&sata_dev_desc[dev]);
  104. }
  105. }
  106. if (!ok) {
  107. puts("\nno SATA devices available\n");
  108. rc ++;
  109. }
  110. return rc;
  111. }
  112. return CMD_RET_USAGE;
  113. case 3:
  114. if (strncmp(argv[1], "dev", 3) == 0) {
  115. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  116. printf("\nSATA device %d: ", dev);
  117. if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
  118. puts ("unknown device\n");
  119. return 1;
  120. }
  121. dev_print(&sata_dev_desc[dev]);
  122. if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
  123. return 1;
  124. sata_curr_device = dev;
  125. puts("... is now current device\n");
  126. return 0;
  127. } else if (strncmp(argv[1], "part", 4) == 0) {
  128. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  129. if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
  130. print_part(&sata_dev_desc[dev]);
  131. } else {
  132. printf("\nSATA device %d not available\n", dev);
  133. rc = 1;
  134. }
  135. return rc;
  136. }
  137. return CMD_RET_USAGE;
  138. default: /* at least 4 args */
  139. if (strcmp(argv[1], "read") == 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 read: device %d block # %ld, count %ld ... ",
  145. sata_curr_device, blk, cnt);
  146. n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
  147. /* flush cache after read */
  148. flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
  149. printf("%ld blocks read: %s\n",
  150. n, (n==cnt) ? "OK" : "ERROR");
  151. return (n == cnt) ? 0 : 1;
  152. } else if (strcmp(argv[1], "write") == 0) {
  153. ulong addr = simple_strtoul(argv[2], NULL, 16);
  154. ulong cnt = simple_strtoul(argv[4], NULL, 16);
  155. ulong n;
  156. lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
  157. printf("\nSATA write: device %d block # %ld, count %ld ... ",
  158. sata_curr_device, blk, cnt);
  159. n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
  160. printf("%ld blocks written: %s\n",
  161. n, (n == cnt) ? "OK" : "ERROR");
  162. return (n == cnt) ? 0 : 1;
  163. } else {
  164. return CMD_RET_USAGE;
  165. }
  166. return rc;
  167. }
  168. }
  169. U_BOOT_CMD(
  170. sata, 5, 1, do_sata,
  171. "SATA sub system",
  172. "init - init SATA sub system\n"
  173. "sata info - show available SATA devices\n"
  174. "sata device [dev] - show or set current device\n"
  175. "sata part [dev] - print partition table\n"
  176. "sata read addr blk# cnt\n"
  177. "sata write addr blk# cnt"
  178. );