blob: 8b1f19ce4744e9dc9d4dbeb510957d1131582fd0 [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;
Simon Glass4101f682016-02-29 15:25:34 -070019struct blk_desc sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
Dave Liuc7057b52008-03-26 22:49:44 +080020
Simon Glass4101f682016-02-29 15:25:34 -070021static unsigned long sata_bread(struct blk_desc *block_dev, lbaint_t start,
Stephen Warren7c4213f2015-12-07 11:38:48 -070022 lbaint_t blkcnt, void *dst)
23{
24 return sata_read(block_dev->dev, start, blkcnt, dst);
25}
26
Simon Glass4101f682016-02-29 15:25:34 -070027static unsigned long sata_bwrite(struct blk_desc *block_dev, lbaint_t start,
Stephen Warren7c4213f2015-12-07 11:38:48 -070028 lbaint_t blkcnt, const void *buffer)
29{
30 return sata_write(block_dev->dev, start, blkcnt, buffer);
31}
32
Mike Frysingercf7e3992009-01-27 16:12:21 -050033int __sata_initialize(void)
Dave Liuc7057b52008-03-26 22:49:44 +080034{
35 int rc;
36 int i;
37
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
Simon Glass4101f682016-02-29 15:25:34 -070039 memset(&sata_dev_desc[i], 0, sizeof(struct blk_desc));
Dave Liuc7057b52008-03-26 22:49:44 +080040 sata_dev_desc[i].if_type = IF_TYPE_SATA;
41 sata_dev_desc[i].dev = i;
42 sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
43 sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
44 sata_dev_desc[i].lba = 0;
45 sata_dev_desc[i].blksz = 512;
Egbert Eich0472fbf2013-04-09 21:11:56 +000046 sata_dev_desc[i].log2blksz = LOG2(sata_dev_desc[i].blksz);
Stephen Warren7c4213f2015-12-07 11:38:48 -070047 sata_dev_desc[i].block_read = sata_bread;
48 sata_dev_desc[i].block_write = sata_bwrite;
Dave Liuc7057b52008-03-26 22:49:44 +080049
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))
Simon Glass3e8bd462016-02-29 15:25:48 -070055 part_init(&sata_dev_desc[i]);
Stefano Babic71cadda2012-02-22 00:24:37 +000056 }
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
Nikita Kiryanovd957c282014-11-21 12:47:24 +020063__weak int __sata_stop(void)
64{
65 int i, err = 0;
66
67 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++)
68 err |= reset_sata(i);
69
70 if (err)
71 printf("Could not reset some SATA devices\n");
72
73 return err;
74}
75int sata_stop(void) __attribute__((weak, alias("__sata_stop")));
76
Matthew McClintockdf3fc522011-05-24 05:31:19 +000077#ifdef CONFIG_PARTITIONS
Simon Glass4101f682016-02-29 15:25:34 -070078struct blk_desc *sata_get_dev(int dev)
Dave Liuc7057b52008-03-26 22:49:44 +080079{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020080 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
Dave Liuc7057b52008-03-26 22:49:44 +080081}
Matthew McClintockdf3fc522011-05-24 05:31:19 +000082#endif
Dave Liuc7057b52008-03-26 22:49:44 +080083
Kim Phillips088f1b12012-10-29 13:34:31 +000084static int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Dave Liuc7057b52008-03-26 22:49:44 +080085{
86 int rc = 0;
87
Nikita Kiryanovd957c282014-11-21 12:47:24 +020088 if (argc == 2 && strcmp(argv[1], "stop") == 0)
89 return sata_stop();
90
91 if (argc == 2 && strcmp(argv[1], "init") == 0) {
92 if (sata_curr_device != -1)
93 sata_stop();
94
Mike Frysingercf7e3992009-01-27 16:12:21 -050095 return sata_initialize();
Nikita Kiryanovd957c282014-11-21 12:47:24 +020096 }
Mike Frysingercf7e3992009-01-27 16:12:21 -050097
98 /* If the user has not yet run `sata init`, do it now */
Mike Frysinger569460e2009-06-14 21:35:22 -040099 if (sata_curr_device == -1)
Mike Frysingercf7e3992009-01-27 16:12:21 -0500100 if (sata_initialize())
101 return 1;
102
Dave Liuc7057b52008-03-26 22:49:44 +0800103 switch (argc) {
104 case 0:
105 case 1:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000106 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800107 case 2:
108 if (strncmp(argv[1],"inf", 3) == 0) {
109 int i;
110 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
Dave Liuc7057b52008-03-26 22:49:44 +0800112 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
113 continue;
114 printf ("SATA device %d: ", i);
115 dev_print(&sata_dev_desc[i]);
116 }
117 return 0;
118 } else if (strncmp(argv[1],"dev", 3) == 0) {
Mike Frysinger569460e2009-06-14 21:35:22 -0400119 if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
Dave Liuc7057b52008-03-26 22:49:44 +0800120 puts("\nno SATA devices available\n");
121 return 1;
122 }
Mike Frysinger569460e2009-06-14 21:35:22 -0400123 printf("\nSATA device %d: ", sata_curr_device);
124 dev_print(&sata_dev_desc[sata_curr_device]);
Dave Liuc7057b52008-03-26 22:49:44 +0800125 return 0;
126 } else if (strncmp(argv[1],"part",4) == 0) {
127 int dev, ok;
128
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200129 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
Dave Liuc7057b52008-03-26 22:49:44 +0800130 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
131 ++ok;
132 if (dev)
133 putc ('\n');
Simon Glass3e8bd462016-02-29 15:25:48 -0700134 part_print(&sata_dev_desc[dev]);
Dave Liuc7057b52008-03-26 22:49:44 +0800135 }
136 }
137 if (!ok) {
138 puts("\nno SATA devices available\n");
139 rc ++;
140 }
141 return rc;
142 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000143 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800144 case 3:
145 if (strncmp(argv[1], "dev", 3) == 0) {
146 int dev = (int)simple_strtoul(argv[2], NULL, 10);
147
148 printf("\nSATA device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200149 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
Dave Liuc7057b52008-03-26 22:49:44 +0800150 puts ("unknown device\n");
151 return 1;
152 }
153 dev_print(&sata_dev_desc[dev]);
154
155 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
156 return 1;
157
Mike Frysinger569460e2009-06-14 21:35:22 -0400158 sata_curr_device = dev;
Dave Liuc7057b52008-03-26 22:49:44 +0800159
160 puts("... is now current device\n");
161
162 return 0;
163 } else if (strncmp(argv[1], "part", 4) == 0) {
164 int dev = (int)simple_strtoul(argv[2], NULL, 10);
165
166 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700167 part_print(&sata_dev_desc[dev]);
Dave Liuc7057b52008-03-26 22:49:44 +0800168 } else {
169 printf("\nSATA device %d not available\n", dev);
170 rc = 1;
171 }
172 return rc;
173 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000174 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800175
176 default: /* at least 4 args */
177 if (strcmp(argv[1], "read") == 0) {
178 ulong addr = simple_strtoul(argv[2], NULL, 16);
179 ulong cnt = simple_strtoul(argv[4], NULL, 16);
180 ulong n;
181 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
182
183 printf("\nSATA read: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400184 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800185
Mike Frysinger569460e2009-06-14 21:35:22 -0400186 n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800187
188 /* flush cache after read */
Mike Frysinger569460e2009-06-14 21:35:22 -0400189 flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
Dave Liuc7057b52008-03-26 22:49:44 +0800190
191 printf("%ld blocks read: %s\n",
192 n, (n==cnt) ? "OK" : "ERROR");
193 return (n == cnt) ? 0 : 1;
194 } else if (strcmp(argv[1], "write") == 0) {
195 ulong addr = simple_strtoul(argv[2], NULL, 16);
196 ulong cnt = simple_strtoul(argv[4], NULL, 16);
197 ulong n;
198
199 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
200
201 printf("\nSATA write: device %d block # %ld, count %ld ... ",
Mike Frysinger569460e2009-06-14 21:35:22 -0400202 sata_curr_device, blk, cnt);
Dave Liuc7057b52008-03-26 22:49:44 +0800203
Mike Frysinger569460e2009-06-14 21:35:22 -0400204 n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
Dave Liuc7057b52008-03-26 22:49:44 +0800205
206 printf("%ld blocks written: %s\n",
207 n, (n == cnt) ? "OK" : "ERROR");
208 return (n == cnt) ? 0 : 1;
209 } else {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000210 return CMD_RET_USAGE;
Dave Liuc7057b52008-03-26 22:49:44 +0800211 }
212
213 return rc;
214 }
215}
216
217U_BOOT_CMD(
218 sata, 5, 1, do_sata,
Peter Tyser2fb26042009-01-27 18:03:12 -0600219 "SATA sub system",
Fabio Estevam85dafbb2013-02-20 14:35:35 +0000220 "init - init SATA sub system\n"
Nikita Kiryanovd957c282014-11-21 12:47:24 +0200221 "sata stop - disable SATA sub system\n"
Dave Liuc7057b52008-03-26 22:49:44 +0800222 "sata info - show available SATA devices\n"
223 "sata device [dev] - show or set current device\n"
224 "sata part [dev] - print partition table\n"
225 "sata read addr blk# cnt\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200226 "sata write addr blk# cnt"
227);