2 * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
3 * Terry Lv <r65388@freescale.com>
5 * Copyright (C) 2000-2005, DENX Software Engineering
6 * Wolfgang Denk <wd@denx.de>
7 * Copyright (C) Procsys. All rights reserved.
8 * Mushtaq Khan <mushtaq_k@procsys.com>
9 * <mushtaqk_921@yahoo.co.in>
10 * Copyright (C) 2008 Freescale Semiconductor, Inc.
11 * Dave Liu <daveliu@freescale.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 int sata_curr_device = -1;
35 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
37 int __sata_initialize(void)
42 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
43 memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
44 sata_dev_desc[i].if_type = IF_TYPE_SATA;
45 sata_dev_desc[i].dev = i;
46 sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
47 sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
48 sata_dev_desc[i].lba = 0;
49 sata_dev_desc[i].blksz = 512;
50 sata_dev_desc[i].block_read = sata_read;
51 sata_dev_desc[i].block_write = sata_write;
56 if (!rc && (sata_dev_desc[i].lba > 0) &&
57 (sata_dev_desc[i].blksz > 0))
58 init_part(&sata_dev_desc[i]);
64 int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
66 #ifdef CONFIG_PARTITIONS
67 block_dev_desc_t *sata_get_dev(int dev)
69 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
73 int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
77 if (argc == 2 && strcmp(argv[1], "init") == 0)
78 return sata_initialize();
80 /* If the user has not yet run `sata init`, do it now */
81 if (sata_curr_device == -1)
82 if (sata_initialize())
90 if (strncmp(argv[1],"inf", 3) == 0) {
93 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
94 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
96 printf ("SATA device %d: ", i);
97 dev_print(&sata_dev_desc[i]);
100 } else if (strncmp(argv[1],"dev", 3) == 0) {
101 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
102 puts("\nno SATA devices available\n");
105 printf("\nSATA device %d: ", sata_curr_device);
106 dev_print(&sata_dev_desc[sata_curr_device]);
108 } else if (strncmp(argv[1],"part",4) == 0) {
111 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
112 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
116 print_part(&sata_dev_desc[dev]);
120 puts("\nno SATA devices available\n");
125 return CMD_RET_USAGE;
127 if (strncmp(argv[1], "dev", 3) == 0) {
128 int dev = (int)simple_strtoul(argv[2], NULL, 10);
130 printf("\nSATA device %d: ", dev);
131 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
132 puts ("unknown device\n");
135 dev_print(&sata_dev_desc[dev]);
137 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
140 sata_curr_device = dev;
142 puts("... is now current device\n");
145 } else if (strncmp(argv[1], "part", 4) == 0) {
146 int dev = (int)simple_strtoul(argv[2], NULL, 10);
148 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
149 print_part(&sata_dev_desc[dev]);
151 printf("\nSATA device %d not available\n", dev);
156 return CMD_RET_USAGE;
158 default: /* at least 4 args */
159 if (strcmp(argv[1], "read") == 0) {
160 ulong addr = simple_strtoul(argv[2], NULL, 16);
161 ulong cnt = simple_strtoul(argv[4], NULL, 16);
163 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
165 printf("\nSATA read: device %d block # %ld, count %ld ... ",
166 sata_curr_device, blk, cnt);
168 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
170 /* flush cache after read */
171 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
173 printf("%ld blocks read: %s\n",
174 n, (n==cnt) ? "OK" : "ERROR");
175 return (n == cnt) ? 0 : 1;
176 } else if (strcmp(argv[1], "write") == 0) {
177 ulong addr = simple_strtoul(argv[2], NULL, 16);
178 ulong cnt = simple_strtoul(argv[4], NULL, 16);
181 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
183 printf("\nSATA write: device %d block # %ld, count %ld ... ",
184 sata_curr_device, blk, cnt);
186 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
188 printf("%ld blocks written: %s\n",
189 n, (n == cnt) ? "OK" : "ERROR");
190 return (n == cnt) ? 0 : 1;
192 return CMD_RET_USAGE;
202 "sata init - init SATA sub system\n"
203 "sata info - show available SATA devices\n"
204 "sata device [dev] - show or set current device\n"
205 "sata part [dev] - print partition table\n"
206 "sata read addr blk# cnt\n"
207 "sata write addr blk# cnt"