blob: 7b1703fe4133df3866f023cdd45d7219026be791 [file] [log] [blame]
Dave Liuc7057b52008-03-26 22:49:44 +08001/*
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>
9 *
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.
14 *
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.
19 *
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,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <command.h>
28#include <part.h>
29#include <sata.h>
30
Mike Frysinger569460e2009-06-14 21:35:22 -040031int sata_curr_device = -1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020032block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
Dave Liuc7057b52008-03-26 22:49:44 +080033
Mike Frysingercf7e3992009-01-27 16:12:21 -050034int __sata_initialize(void)
Dave Liuc7057b52008-03-26 22:49:44 +080035{
36 int rc;
37 int i;
38
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020039 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
Dave Liuc7057b52008-03-26 22:49:44 +080040 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;
49
50 rc = init_sata(i);
51 rc = scan_sata(i);
52 if ((sata_dev_desc[i].lba > 0) && (sata_dev_desc[i].blksz > 0))
53 init_part(&sata_dev_desc[i]);
54 }
Mike Frysinger569460e2009-06-14 21:35:22 -040055 sata_curr_device = 0;
Dave Liuc7057b52008-03-26 22:49:44 +080056 return rc;
57}
Mike Frysingercf7e3992009-01-27 16:12:21 -050058int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
Dave Liuc7057b52008-03-26 22:49:44 +080059
Matthew McClintockdf3fc522011-05-24 05:31:19 +000060#ifdef CONFIG_PARTITIONS
Dave Liuc7057b52008-03-26 22:49:44 +080061block_dev_desc_t *sata_get_dev(int dev)
62{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020063 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
Dave Liuc7057b52008-03-26 22:49:44 +080064}
Matthew McClintockdf3fc522011-05-24 05:31:19 +000065#endif
Dave Liuc7057b52008-03-26 22:49:44 +080066
Wolfgang Denk54841ab2010-06-28 22:00:46 +020067int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Dave Liuc7057b52008-03-26 22:49:44 +080068{
69 int rc = 0;
70
Mike Frysingercf7e3992009-01-27 16:12:21 -050071 if (argc == 2 && strcmp(argv[1], "init") == 0)
72 return sata_initialize();
73
74 /* If the user has not yet run `sata init`, do it now */
Mike Frysinger569460e2009-06-14 21:35:22 -040075 if (sata_curr_device == -1)
Mike Frysingercf7e3992009-01-27 16:12:21 -050076 if (sata_initialize())
77 return 1;
78
Dave Liuc7057b52008-03-26 22:49:44 +080079 switch (argc) {
80 case 0:
81 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +000082 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +080083 case 2:
84 if (strncmp(argv[1],"inf", 3) == 0) {
85 int i;
86 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020087 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
Dave Liuc7057b52008-03-26 22:49:44 +080088 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
89 continue;
90 printf ("SATA device %d: ", i);
91 dev_print(&sata_dev_desc[i]);
92 }
93 return 0;
94 } else if (strncmp(argv[1],"dev", 3) == 0) {
Mike Frysinger569460e2009-06-14 21:35:22 -040095 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
Dave Liuc7057b52008-03-26 22:49:44 +080096 puts("\nno SATA devices available\n");
97 return 1;
98 }
Mike Frysinger569460e2009-06-14 21:35:22 -040099 printf("\nSATA device %d: ", sata_curr_device);
100 dev_print(&sata_dev_desc[sata_curr_device]);
Dave Liuc7057b52008-03-26 22:49:44 +0800101 return 0;
102 } else if (strncmp(argv[1],"part",4) == 0) {
103 int dev, ok;
104
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
Dave Liuc7057b52008-03-26 22:49:44 +0800106 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
107 ++ok;
108 if (dev)
109 putc ('\n');
110 print_part(&sata_dev_desc[dev]);
111 }
112 }
113 if (!ok) {
114 puts("\nno SATA devices available\n");
115 rc ++;
116 }
117 return rc;
118 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000119 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800120 case 3:
121 if (strncmp(argv[1], "dev", 3) == 0) {
122 int dev = (int)simple_strtoul(argv[2], NULL, 10);
123
124 printf("\nSATA device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200125 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
Dave Liuc7057b52008-03-26 22:49:44 +0800126 puts ("unknown device\n");
127 return 1;
128 }
129 dev_print(&sata_dev_desc[dev]);
130
131 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
132 return 1;
133
Mike Frysinger569460e2009-06-14 21:35:22 -0400134 sata_curr_device = dev;
Dave Liuc7057b52008-03-26 22:49:44 +0800135
136 puts("... is now current device\n");
137
138 return 0;
139 } else if (strncmp(argv[1], "part", 4) == 0) {
140 int dev = (int)simple_strtoul(argv[2], NULL, 10);
141
142 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
143 print_part(&sata_dev_desc[dev]);
144 } else {
145 printf("\nSATA device %d not available\n", dev);
146 rc = 1;
147 }
148 return rc;
149 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000150 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800151
152 default: /* at least 4 args */
153 if (strcmp(argv[1], "read") == 0) {
154 ulong addr = simple_strtoul(argv[2], NULL, 16);
155 ulong cnt = simple_strtoul(argv[4], NULL, 16);
156 ulong n;
157 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
158
159 printf("\nSATA read: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400160 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800161
Mike Frysinger569460e2009-06-14 21:35:22 -0400162 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800163
164 /* flush cache after read */
Mike Frysinger569460e2009-06-14 21:35:22 -0400165 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
Dave Liuc7057b52008-03-26 22:49:44 +0800166
167 printf("%ld blocks read: %s\n",
168 n, (n==cnt) ? "OK" : "ERROR");
169 return (n == cnt) ? 0 : 1;
170 } else if (strcmp(argv[1], "write") == 0) {
171 ulong addr = simple_strtoul(argv[2], NULL, 16);
172 ulong cnt = simple_strtoul(argv[4], NULL, 16);
173 ulong n;
174
175 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
176
177 printf("\nSATA write: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400178 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800179
Mike Frysinger569460e2009-06-14 21:35:22 -0400180 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800181
182 printf("%ld blocks written: %s\n",
183 n, (n == cnt) ? "OK" : "ERROR");
184 return (n == cnt) ? 0 : 1;
185 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000186 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800187 }
188
189 return rc;
190 }
191}
192
193U_BOOT_CMD(
194 sata, 5, 1, do_sata,
Peter Tyser2fb26042009-01-27 18:03:12 -0600195 "SATA sub system",
196 "sata init - init SATA sub system\n"
Dave Liuc7057b52008-03-26 22:49:44 +0800197 "sata info - show available SATA devices\n"
198 "sata device [dev] - show or set current device\n"
199 "sata part [dev] - print partition table\n"
200 "sata read addr blk# cnt\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200201 "sata write addr blk# cnt"
202);