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