libata.c 2.9 KB

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