libata.c 2.9 KB

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