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 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 static int sata_curr_device = -1;
32 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
34 int __sata_initialize(void)
39 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
40 memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
41 sata_dev_desc[i].if_type = IF_TYPE_SATA;
42 sata_dev_desc[i].dev = i;
43 sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
44 sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
45 sata_dev_desc[i].lba = 0;
46 sata_dev_desc[i].blksz = 512;
47 sata_dev_desc[i].block_read = sata_read;
48 sata_dev_desc[i].block_write = sata_write;
53 if (!rc && (sata_dev_desc[i].lba > 0) &&
54 (sata_dev_desc[i].blksz > 0))
55 init_part(&sata_dev_desc[i]);
61 int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
63 #ifdef CONFIG_PARTITIONS
64 block_dev_desc_t *sata_get_dev(int dev)
66 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
70 static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
74 if (argc == 2 && strcmp(argv[1], "init") == 0)
75 return sata_initialize();
77 /* If the user has not yet run `sata init`, do it now */
78 if (sata_curr_device == -1)
79 if (sata_initialize())
87 if (strncmp(argv[1],"inf", 3) == 0) {
90 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
91 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
93 printf ("SATA device %d: ", i);
94 dev_print(&sata_dev_desc[i]);
97 } else if (strncmp(argv[1],"dev", 3) == 0) {
98 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
99 puts("\nno SATA devices available\n");
102 printf("\nSATA device %d: ", sata_curr_device);
103 dev_print(&sata_dev_desc[sata_curr_device]);
105 } else if (strncmp(argv[1],"part",4) == 0) {
108 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
109 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
113 print_part(&sata_dev_desc[dev]);
117 puts("\nno SATA devices available\n");
122 return CMD_RET_USAGE;
124 if (strncmp(argv[1], "dev", 3) == 0) {
125 int dev = (int)simple_strtoul(argv[2], NULL, 10);
127 printf("\nSATA device %d: ", dev);
128 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
129 puts ("unknown device\n");
132 dev_print(&sata_dev_desc[dev]);
134 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
137 sata_curr_device = dev;
139 puts("... is now current device\n");
142 } else if (strncmp(argv[1], "part", 4) == 0) {
143 int dev = (int)simple_strtoul(argv[2], NULL, 10);
145 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
146 print_part(&sata_dev_desc[dev]);
148 printf("\nSATA device %d not available\n", dev);
153 return CMD_RET_USAGE;
155 default: /* at least 4 args */
156 if (strcmp(argv[1], "read") == 0) {
157 ulong addr = simple_strtoul(argv[2], NULL, 16);
158 ulong cnt = simple_strtoul(argv[4], NULL, 16);
160 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
162 printf("\nSATA read: device %d block # %ld, count %ld ... ",
163 sata_curr_device, blk, cnt);
165 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
167 /* flush cache after read */
168 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
170 printf("%ld blocks read: %s\n",
171 n, (n==cnt) ? "OK" : "ERROR");
172 return (n == cnt) ? 0 : 1;
173 } else if (strcmp(argv[1], "write") == 0) {
174 ulong addr = simple_strtoul(argv[2], NULL, 16);
175 ulong cnt = simple_strtoul(argv[4], NULL, 16);
178 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
180 printf("\nSATA write: device %d block # %ld, count %ld ... ",
181 sata_curr_device, blk, cnt);
183 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
185 printf("%ld blocks written: %s\n",
186 n, (n == cnt) ? "OK" : "ERROR");
187 return (n == cnt) ? 0 : 1;
189 return CMD_RET_USAGE;
199 "sata init - init SATA sub system\n"
200 "sata info - show available SATA devices\n"
201 "sata device [dev] - show or set current device\n"
202 "sata part [dev] - print partition table\n"
203 "sata read addr blk# cnt\n"
204 "sata write addr blk# cnt"