blob: 8d57285d053d27489080f4e7c4e74f659d92f26a [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
Kim Phillips088f1b12012-10-29 13:34:31 +000031static int 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);
Stefano Babic71cadda2012-02-22 00:24:37 +000051 if (!rc) {
52 rc = scan_sata(i);
53 if (!rc && (sata_dev_desc[i].lba > 0) &&
54 (sata_dev_desc[i].blksz > 0))
55 init_part(&sata_dev_desc[i]);
56 }
Dave Liuc7057b52008-03-26 22:49:44 +080057 }
Mike Frysinger569460e2009-06-14 21:35:22 -040058 sata_curr_device = 0;
Dave Liuc7057b52008-03-26 22:49:44 +080059 return rc;
60}
Mike Frysingercf7e3992009-01-27 16:12:21 -050061int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
Dave Liuc7057b52008-03-26 22:49:44 +080062
Matthew McClintockdf3fc522011-05-24 05:31:19 +000063#ifdef CONFIG_PARTITIONS
Dave Liuc7057b52008-03-26 22:49:44 +080064block_dev_desc_t *sata_get_dev(int dev)
65{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
Dave Liuc7057b52008-03-26 22:49:44 +080067}
Matthew McClintockdf3fc522011-05-24 05:31:19 +000068#endif
Dave Liuc7057b52008-03-26 22:49:44 +080069
Kim Phillips088f1b12012-10-29 13:34:31 +000070static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Dave Liuc7057b52008-03-26 22:49:44 +080071{
72 int rc = 0;
73
Mike Frysingercf7e3992009-01-27 16:12:21 -050074 if (argc == 2 && strcmp(argv[1], "init") == 0)
75 return sata_initialize();
76
77 /* If the user has not yet run `sata init`, do it now */
Mike Frysinger569460e2009-06-14 21:35:22 -040078 if (sata_curr_device == -1)
Mike Frysingercf7e3992009-01-27 16:12:21 -050079 if (sata_initialize())
80 return 1;
81
Dave Liuc7057b52008-03-26 22:49:44 +080082 switch (argc) {
83 case 0:
84 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +000085 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +080086 case 2:
87 if (strncmp(argv[1],"inf", 3) == 0) {
88 int i;
89 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020090 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
Dave Liuc7057b52008-03-26 22:49:44 +080091 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
92 continue;
93 printf ("SATA device %d: ", i);
94 dev_print(&sata_dev_desc[i]);
95 }
96 return 0;
97 } else if (strncmp(argv[1],"dev", 3) == 0) {
Mike Frysinger569460e2009-06-14 21:35:22 -040098 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
Dave Liuc7057b52008-03-26 22:49:44 +080099 puts("\nno SATA devices available\n");
100 return 1;
101 }
Mike Frysinger569460e2009-06-14 21:35:22 -0400102 printf("\nSATA device %d: ", sata_curr_device);
103 dev_print(&sata_dev_desc[sata_curr_device]);
Dave Liuc7057b52008-03-26 22:49:44 +0800104 return 0;
105 } else if (strncmp(argv[1],"part",4) == 0) {
106 int dev, ok;
107
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200108 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
Dave Liuc7057b52008-03-26 22:49:44 +0800109 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
110 ++ok;
111 if (dev)
112 putc ('\n');
113 print_part(&sata_dev_desc[dev]);
114 }
115 }
116 if (!ok) {
117 puts("\nno SATA devices available\n");
118 rc ++;
119 }
120 return rc;
121 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000122 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800123 case 3:
124 if (strncmp(argv[1], "dev", 3) == 0) {
125 int dev = (int)simple_strtoul(argv[2], NULL, 10);
126
127 printf("\nSATA device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200128 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
Dave Liuc7057b52008-03-26 22:49:44 +0800129 puts ("unknown device\n");
130 return 1;
131 }
132 dev_print(&sata_dev_desc[dev]);
133
134 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
135 return 1;
136
Mike Frysinger569460e2009-06-14 21:35:22 -0400137 sata_curr_device = dev;
Dave Liuc7057b52008-03-26 22:49:44 +0800138
139 puts("... is now current device\n");
140
141 return 0;
142 } else if (strncmp(argv[1], "part", 4) == 0) {
143 int dev = (int)simple_strtoul(argv[2], NULL, 10);
144
145 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
146 print_part(&sata_dev_desc[dev]);
147 } else {
148 printf("\nSATA device %d not available\n", dev);
149 rc = 1;
150 }
151 return rc;
152 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000153 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800154
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);
159 ulong n;
160 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
161
162 printf("\nSATA read: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400163 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800164
Mike Frysinger569460e2009-06-14 21:35:22 -0400165 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800166
167 /* flush cache after read */
Mike Frysinger569460e2009-06-14 21:35:22 -0400168 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
Dave Liuc7057b52008-03-26 22:49:44 +0800169
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);
176 ulong n;
177
178 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
179
180 printf("\nSATA write: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400181 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800182
Mike Frysinger569460e2009-06-14 21:35:22 -0400183 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800184
185 printf("%ld blocks written: %s\n",
186 n, (n == cnt) ? "OK" : "ERROR");
187 return (n == cnt) ? 0 : 1;
188 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000189 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800190 }
191
192 return rc;
193 }
194}
195
196U_BOOT_CMD(
197 sata, 5, 1, do_sata,
Peter Tyser2fb26042009-01-27 18:03:12 -0600198 "SATA sub system",
Fabio Estevam85dafbb2013-02-20 14:35:35 +0000199 "init - init SATA sub system\n"
Dave Liuc7057b52008-03-26 22:49:44 +0800200 "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"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200204 "sata write addr blk# cnt"
205);