2 * Copyright (C) 2000-2005, DENX Software Engineering
3 * Wolfgang Denk <wd@denx.de>
4 * Copyright (C) Procsys. All rights reserved.
5 * Mushtaq Khan <mushtaq_k@procsys.com>
6 * <mushtaqk_921@yahoo.co.in>
7 * Copyright (C) 2008 Freescale Semiconductor, Inc.
8 * Dave Liu <daveliu@freescale.com>
10 * SPDX-License-Identifier: GPL-2.0+
18 static int sata_curr_device = -1;
19 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
21 int __sata_initialize(void)
26 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
27 memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
28 sata_dev_desc[i].if_type = IF_TYPE_SATA;
29 sata_dev_desc[i].dev = i;
30 sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
31 sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
32 sata_dev_desc[i].lba = 0;
33 sata_dev_desc[i].blksz = 512;
34 sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
35 sata_dev_desc[i].block_read = sata_read;
36 sata_dev_desc[i].block_write = sata_write;
41 if (!rc && (sata_dev_desc[i].lba > 0) &&
42 (sata_dev_desc[i].blksz > 0))
43 init_part(&sata_dev_desc[i]);
49 int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
51 #ifdef CONFIG_PARTITIONS
52 block_dev_desc_t *sata_get_dev(int dev)
54 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
58 static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
62 if (argc == 2 && strcmp(argv[1], "init") == 0)
63 return sata_initialize();
65 /* If the user has not yet run `sata init`, do it now */
66 if (sata_curr_device == -1)
67 if (sata_initialize())
75 if (strncmp(argv[1],"inf", 3) == 0) {
78 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
79 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
81 printf ("SATA device %d: ", i);
82 dev_print(&sata_dev_desc[i]);
85 } else if (strncmp(argv[1],"dev", 3) == 0) {
86 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
87 puts("\nno SATA devices available\n");
90 printf("\nSATA device %d: ", sata_curr_device);
91 dev_print(&sata_dev_desc[sata_curr_device]);
93 } else if (strncmp(argv[1],"part",4) == 0) {
96 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
97 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
101 print_part(&sata_dev_desc[dev]);
105 puts("\nno SATA devices available\n");
110 return CMD_RET_USAGE;
112 if (strncmp(argv[1], "dev", 3) == 0) {
113 int dev = (int)simple_strtoul(argv[2], NULL, 10);
115 printf("\nSATA device %d: ", dev);
116 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
117 puts ("unknown device\n");
120 dev_print(&sata_dev_desc[dev]);
122 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
125 sata_curr_device = dev;
127 puts("... is now current device\n");
130 } else if (strncmp(argv[1], "part", 4) == 0) {
131 int dev = (int)simple_strtoul(argv[2], NULL, 10);
133 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
134 print_part(&sata_dev_desc[dev]);
136 printf("\nSATA device %d not available\n", dev);
141 return CMD_RET_USAGE;
143 default: /* at least 4 args */
144 if (strcmp(argv[1], "read") == 0) {
145 ulong addr = simple_strtoul(argv[2], NULL, 16);
146 ulong cnt = simple_strtoul(argv[4], NULL, 16);
148 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
150 printf("\nSATA read: device %d block # %ld, count %ld ... ",
151 sata_curr_device, blk, cnt);
153 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
155 /* flush cache after read */
156 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
158 printf("%ld blocks read: %s\n",
159 n, (n==cnt) ? "OK" : "ERROR");
160 return (n == cnt) ? 0 : 1;
161 } else if (strcmp(argv[1], "write") == 0) {
162 ulong addr = simple_strtoul(argv[2], NULL, 16);
163 ulong cnt = simple_strtoul(argv[4], NULL, 16);
166 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
168 printf("\nSATA write: device %d block # %ld, count %ld ... ",
169 sata_curr_device, blk, cnt);
171 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
173 printf("%ld blocks written: %s\n",
174 n, (n == cnt) ? "OK" : "ERROR");
175 return (n == cnt) ? 0 : 1;
177 return CMD_RET_USAGE;
187 "init - init SATA sub system\n"
188 "sata info - show available SATA devices\n"
189 "sata device [dev] - show or set current device\n"
190 "sata part [dev] - print partition table\n"
191 "sata read addr blk# cnt\n"
192 "sata write addr blk# cnt"