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 ((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 block_dev_desc_t *sata_get_dev(int dev)
68 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
71 int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
75 if (argc == 2 && strcmp(argv[1], "init") == 0)
76 return sata_initialize();
78 /* If the user has not yet run `sata init`, do it now */
79 if (sata_curr_device == -1)
80 if (sata_initialize())
89 if (strncmp(argv[1],"inf", 3) == 0) {
92 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
93 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
95 printf ("SATA device %d: ", i);
96 dev_print(&sata_dev_desc[i]);
99 } else if (strncmp(argv[1],"dev", 3) == 0) {
100 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
101 puts("\nno SATA devices available\n");
104 printf("\nSATA device %d: ", sata_curr_device);
105 dev_print(&sata_dev_desc[sata_curr_device]);
107 } else if (strncmp(argv[1],"part",4) == 0) {
110 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
111 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
115 print_part(&sata_dev_desc[dev]);
119 puts("\nno SATA devices available\n");
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);
159 default: /* at least 4 args */
160 if (strcmp(argv[1], "read") == 0) {
161 ulong addr = simple_strtoul(argv[2], NULL, 16);
162 ulong cnt = simple_strtoul(argv[4], NULL, 16);
164 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
166 printf("\nSATA read: device %d block # %ld, count %ld ... ",
167 sata_curr_device, blk, cnt);
169 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
171 /* flush cache after read */
172 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
174 printf("%ld blocks read: %s\n",
175 n, (n==cnt) ? "OK" : "ERROR");
176 return (n == cnt) ? 0 : 1;
177 } else if (strcmp(argv[1], "write") == 0) {
178 ulong addr = simple_strtoul(argv[2], NULL, 16);
179 ulong cnt = simple_strtoul(argv[4], NULL, 16);
182 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
184 printf("\nSATA write: device %d block # %ld, count %ld ... ",
185 sata_curr_device, blk, cnt);
187 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
189 printf("%ld blocks written: %s\n",
190 n, (n == cnt) ? "OK" : "ERROR");
191 return (n == cnt) ? 0 : 1;
204 "sata init - init SATA sub system\n"
205 "sata info - show available SATA devices\n"
206 "sata device [dev] - show or set current device\n"
207 "sata part [dev] - print partition table\n"
208 "sata read addr blk# cnt\n"
209 "sata write addr blk# cnt"