blob: 51f67033ae32026d973e8944685839587c855e5e [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Dave Liuc7057b52008-03-26 22:49:44 +080011 */
12
13#include <common.h>
14#include <command.h>
15#include <part.h>
16#include <sata.h>
17
Kim Phillips088f1b12012-10-29 13:34:31 +000018static int sata_curr_device = -1;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020019block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
Dave Liuc7057b52008-03-26 22:49:44 +080020
Mike Frysingercf7e3992009-01-27 16:12:21 -050021int __sata_initialize(void)
Dave Liuc7057b52008-03-26 22:49:44 +080022{
23 int rc;
24 int i;
25
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020026 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
Dave Liuc7057b52008-03-26 22:49:44 +080027 memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
28 sata_dev_desc[i].if_type = IF_TYPE_SATA;
29 sata_dev_desc[i].dev = i;
30 sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
31 sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
32 sata_dev_desc[i].lba = 0;
33 sata_dev_desc[i].blksz = 512;
Egbert Eich0472fbf2013-04-09 21:11:56 +000034 sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
Dave Liuc7057b52008-03-26 22:49:44 +080035 sata_dev_desc[i].block_read = sata_read;
36 sata_dev_desc[i].block_write = sata_write;
37
38 rc = init_sata(i);
Stefano Babic71cadda2012-02-22 00:24:37 +000039 if (!rc) {
40 rc = scan_sata(i);
41 if (!rc && (sata_dev_desc[i].lba > 0) &&
42 (sata_dev_desc[i].blksz > 0))
43 init_part(&sata_dev_desc[i]);
44 }
Dave Liuc7057b52008-03-26 22:49:44 +080045 }
Mike Frysinger569460e2009-06-14 21:35:22 -040046 sata_curr_device = 0;
Dave Liuc7057b52008-03-26 22:49:44 +080047 return rc;
48}
Mike Frysingercf7e3992009-01-27 16:12:21 -050049int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
Dave Liuc7057b52008-03-26 22:49:44 +080050
Nikita Kiryanovd957c282014-11-21 12:47:24 +020051__weak int __sata_stop(void)
52{
53 int i, err = 0;
54
55 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++)
56 err |= reset_sata(i);
57
58 if (err)
59 printf("Could not reset some SATA devices\n");
60
61 return err;
62}
63int sata_stop(void) __attribute__((weak, alias("__sata_stop")));
64
Matthew McClintockdf3fc522011-05-24 05:31:19 +000065#ifdef CONFIG_PARTITIONS
Dave Liuc7057b52008-03-26 22:49:44 +080066block_dev_desc_t *sata_get_dev(int dev)
67{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
Dave Liuc7057b52008-03-26 22:49:44 +080069}
Matthew McClintockdf3fc522011-05-24 05:31:19 +000070#endif
Dave Liuc7057b52008-03-26 22:49:44 +080071
Kim Phillips088f1b12012-10-29 13:34:31 +000072static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Dave Liuc7057b52008-03-26 22:49:44 +080073{
74 int rc = 0;
75
Nikita Kiryanovd957c282014-11-21 12:47:24 +020076 if (argc == 2 && strcmp(argv[1], "stop") == 0)
77 return sata_stop();
78
79 if (argc == 2 && strcmp(argv[1], "init") == 0) {
80 if (sata_curr_device != -1)
81 sata_stop();
82
Mike Frysingercf7e3992009-01-27 16:12:21 -050083 return sata_initialize();
Nikita Kiryanovd957c282014-11-21 12:47:24 +020084 }
Mike Frysingercf7e3992009-01-27 16:12:21 -050085
86 /* If the user has not yet run `sata init`, do it now */
Mike Frysinger569460e2009-06-14 21:35:22 -040087 if (sata_curr_device == -1)
Mike Frysingercf7e3992009-01-27 16:12:21 -050088 if (sata_initialize())
89 return 1;
90
Dave Liuc7057b52008-03-26 22:49:44 +080091 switch (argc) {
92 case 0:
93 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +000094 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +080095 case 2:
96 if (strncmp(argv[1],"inf", 3) == 0) {
97 int i;
98 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
Dave Liuc7057b52008-03-26 22:49:44 +0800100 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
101 continue;
102 printf ("SATA device %d: ", i);
103 dev_print(&sata_dev_desc[i]);
104 }
105 return 0;
106 } else if (strncmp(argv[1],"dev", 3) == 0) {
Mike Frysinger569460e2009-06-14 21:35:22 -0400107 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
Dave Liuc7057b52008-03-26 22:49:44 +0800108 puts("\nno SATA devices available\n");
109 return 1;
110 }
Mike Frysinger569460e2009-06-14 21:35:22 -0400111 printf("\nSATA device %d: ", sata_curr_device);
112 dev_print(&sata_dev_desc[sata_curr_device]);
Dave Liuc7057b52008-03-26 22:49:44 +0800113 return 0;
114 } else if (strncmp(argv[1],"part",4) == 0) {
115 int dev, ok;
116
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200117 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
Dave Liuc7057b52008-03-26 22:49:44 +0800118 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
119 ++ok;
120 if (dev)
121 putc ('\n');
122 print_part(&sata_dev_desc[dev]);
123 }
124 }
125 if (!ok) {
126 puts("\nno SATA devices available\n");
127 rc ++;
128 }
129 return rc;
130 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000131 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800132 case 3:
133 if (strncmp(argv[1], "dev", 3) == 0) {
134 int dev = (int)simple_strtoul(argv[2], NULL, 10);
135
136 printf("\nSATA device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200137 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
Dave Liuc7057b52008-03-26 22:49:44 +0800138 puts ("unknown device\n");
139 return 1;
140 }
141 dev_print(&sata_dev_desc[dev]);
142
143 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
144 return 1;
145
Mike Frysinger569460e2009-06-14 21:35:22 -0400146 sata_curr_device = dev;
Dave Liuc7057b52008-03-26 22:49:44 +0800147
148 puts("... is now current device\n");
149
150 return 0;
151 } else if (strncmp(argv[1], "part", 4) == 0) {
152 int dev = (int)simple_strtoul(argv[2], NULL, 10);
153
154 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
155 print_part(&sata_dev_desc[dev]);
156 } else {
157 printf("\nSATA device %d not available\n", dev);
158 rc = 1;
159 }
160 return rc;
161 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000162 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800163
164 default: /* at least 4 args */
165 if (strcmp(argv[1], "read") == 0) {
166 ulong addr = simple_strtoul(argv[2], NULL, 16);
167 ulong cnt = simple_strtoul(argv[4], NULL, 16);
168 ulong n;
169 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
170
171 printf("\nSATA read: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400172 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800173
Mike Frysinger569460e2009-06-14 21:35:22 -0400174 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800175
176 /* flush cache after read */
Mike Frysinger569460e2009-06-14 21:35:22 -0400177 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
Dave Liuc7057b52008-03-26 22:49:44 +0800178
179 printf("%ld blocks read: %s\n",
180 n, (n==cnt) ? "OK" : "ERROR");
181 return (n == cnt) ? 0 : 1;
182 } else if (strcmp(argv[1], "write") == 0) {
183 ulong addr = simple_strtoul(argv[2], NULL, 16);
184 ulong cnt = simple_strtoul(argv[4], NULL, 16);
185 ulong n;
186
187 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
188
189 printf("\nSATA write: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400190 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800191
Mike Frysinger569460e2009-06-14 21:35:22 -0400192 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800193
194 printf("%ld blocks written: %s\n",
195 n, (n == cnt) ? "OK" : "ERROR");
196 return (n == cnt) ? 0 : 1;
197 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000198 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800199 }
200
201 return rc;
202 }
203}
204
205U_BOOT_CMD(
206 sata, 5, 1, do_sata,
Peter Tyser2fb26042009-01-27 18:03:12 -0600207 "SATA sub system",
Fabio Estevam85dafbb2013-02-20 14:35:35 +0000208 "init - init SATA sub system\n"
Nikita Kiryanovd957c282014-11-21 12:47:24 +0200209 "sata stop - disable SATA sub system\n"
Dave Liuc7057b52008-03-26 22:49:44 +0800210 "sata info - show available SATA devices\n"
211 "sata device [dev] - show or set current device\n"
212 "sata part [dev] - print partition table\n"
213 "sata read addr blk# cnt\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200214 "sata write addr blk# cnt"
215);