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