libata.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-19 lizhirui porting to ls2k
  9. */
  10. #include <rtthread.h>
  11. #include <ata_interface.h>
  12. #include <libata.h>
  13. u64 ata_id_n_sectors(u16 *id)
  14. {
  15. if (ata_id_has_lba(id)) {
  16. if (ata_id_has_lba48(id))
  17. return ata_id_u64(id, ATA_ID_LBA48_SECTORS);
  18. else
  19. return ata_id_u32(id, ATA_ID_LBA_SECTORS);
  20. } else {
  21. return 0;
  22. }
  23. }
  24. u32 ata_dev_classify(u32 sig)
  25. {
  26. u8 lbam, lbah;
  27. lbam = (sig >> 16) & 0xff;
  28. lbah = (sig >> 24) & 0xff;
  29. if (((lbam == 0) && (lbah == 0)) ||
  30. ((lbam == 0x3c) && (lbah == 0xc3)))
  31. return ATA_DEV_ATA;
  32. if ((lbam == 0x14) && (lbah == 0xeb))
  33. return ATA_DEV_ATAPI;
  34. if ((lbam == 0x69) && (lbah == 0x96))
  35. return ATA_DEV_PMP;
  36. return ATA_DEV_UNKNOWN;
  37. }
  38. static void ata_id_string(const u16 *id, unsigned char *s,
  39. unsigned int ofs, unsigned int len)
  40. {
  41. unsigned int c;
  42. while (len > 0) {
  43. c = id[ofs] >> 8;
  44. *s = c;
  45. s++;
  46. c = id[ofs] & 0xff;
  47. *s = c;
  48. s++;
  49. ofs++;
  50. len -= 2;
  51. }
  52. }
  53. void ata_id_c_string(const u16 *id, unsigned char *s,
  54. unsigned int ofs, unsigned int len)
  55. {
  56. unsigned char *p;
  57. ata_id_string(id, s, ofs, len - 1);
  58. p = s + strnlen((char *)s, len - 1);
  59. while (p > s && p[-1] == ' ')
  60. p--;
  61. *p = '\0';
  62. }
  63. void ata_dump_id(u16 *id)
  64. {
  65. unsigned char serial[ATA_ID_SERNO_LEN + 1];
  66. unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
  67. unsigned char product[ATA_ID_PROD_LEN + 1];
  68. u64 n_sectors;
  69. /* Serial number */
  70. ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  71. printf("S/N: %s\n\r", serial);
  72. /* Firmware version */
  73. ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
  74. printf("Firmware version: %s\n\r", firmware);
  75. /* Product model */
  76. ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
  77. printf("Product model number: %s\n\r", product);
  78. /* Total sectors of device */
  79. n_sectors = ata_id_n_sectors(id);
  80. printf("Capablity: %lld sectors\n\r", n_sectors);
  81. printf ("id[49]: capabilities = 0x%04x\n"
  82. "id[53]: field valid = 0x%04x\n"
  83. "id[63]: mwdma = 0x%04x\n"
  84. "id[64]: pio = 0x%04x\n"
  85. "id[75]: queue depth = 0x%04x\n",
  86. id[49],
  87. id[53],
  88. id[63],
  89. id[64],
  90. id[75]);
  91. printf ("id[76]: sata capablity = 0x%04x\n"
  92. "id[78]: sata features supported = 0x%04x\n"
  93. "id[79]: sata features enable = 0x%04x\n",
  94. id[76],
  95. id[78],
  96. id[79]);
  97. printf ("id[80]: major version = 0x%04x\n"
  98. "id[81]: minor version = 0x%04x\n"
  99. "id[82]: command set supported 1 = 0x%04x\n"
  100. "id[83]: command set supported 2 = 0x%04x\n"
  101. "id[84]: command set extension = 0x%04x\n",
  102. id[80],
  103. id[81],
  104. id[82],
  105. id[83],
  106. id[84]);
  107. printf ("id[85]: command set enable 1 = 0x%04x\n"
  108. "id[86]: command set enable 2 = 0x%04x\n"
  109. "id[87]: command set default = 0x%04x\n"
  110. "id[88]: udma = 0x%04x\n"
  111. "id[93]: hardware reset result = 0x%04x\n",
  112. id[85],
  113. id[86],
  114. id[87],
  115. id[88],
  116. id[93]);
  117. }
  118. void ata_swap_buf_le16(u16 *buf, unsigned int buf_words)
  119. {
  120. unsigned int i;
  121. for (i = 0; i < buf_words; i++)
  122. {
  123. buf[i] = le16_to_cpu(buf[i]);
  124. }
  125. }