blob: dd6f1d9a5bf4cfd7cf4d8e41e81d916f0c0e1d96 [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
31int 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
34int sata_initialize(void)
35{
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 }
55 curr_device = 0;
56 return rc;
57}
58
59block_dev_desc_t *sata_get_dev(int dev)
60{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020061 return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
Dave Liuc7057b52008-03-26 22:49:44 +080062}
63
64int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
65{
66 int rc = 0;
67
68 switch (argc) {
69 case 0:
70 case 1:
71 printf("Usage:\n%s\n", cmdtp->usage);
72 return 1;
73 case 2:
74 if (strncmp(argv[1],"inf", 3) == 0) {
75 int i;
76 putc('\n');
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020077 for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
Dave Liuc7057b52008-03-26 22:49:44 +080078 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
79 continue;
80 printf ("SATA device %d: ", i);
81 dev_print(&sata_dev_desc[i]);
82 }
83 return 0;
84 } else if (strncmp(argv[1],"dev", 3) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020085 if ((curr_device < 0) || (curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
Dave Liuc7057b52008-03-26 22:49:44 +080086 puts("\nno SATA devices available\n");
87 return 1;
88 }
89 printf("\nSATA device %d: ", curr_device);
90 dev_print(&sata_dev_desc[curr_device]);
91 return 0;
92 } else if (strncmp(argv[1],"part",4) == 0) {
93 int dev, ok;
94
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020095 for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
Dave Liuc7057b52008-03-26 22:49:44 +080096 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
97 ++ok;
98 if (dev)
99 putc ('\n');
100 print_part(&sata_dev_desc[dev]);
101 }
102 }
103 if (!ok) {
104 puts("\nno SATA devices available\n");
105 rc ++;
106 }
107 return rc;
108 }
109 printf("Usage:\n%s\n", cmdtp->usage);
110 return 1;
111 case 3:
112 if (strncmp(argv[1], "dev", 3) == 0) {
113 int dev = (int)simple_strtoul(argv[2], NULL, 10);
114
115 printf("\nSATA device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200116 if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
Dave Liuc7057b52008-03-26 22:49:44 +0800117 puts ("unknown device\n");
118 return 1;
119 }
120 dev_print(&sata_dev_desc[dev]);
121
122 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
123 return 1;
124
125 curr_device = dev;
126
127 puts("... is now current device\n");
128
129 return 0;
130 } else if (strncmp(argv[1], "part", 4) == 0) {
131 int dev = (int)simple_strtoul(argv[2], NULL, 10);
132
133 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
134 print_part(&sata_dev_desc[dev]);
135 } else {
136 printf("\nSATA device %d not available\n", dev);
137 rc = 1;
138 }
139 return rc;
140 }
141 printf ("Usage:\n%s\n", cmdtp->usage);
142 return 1;
143
144 default: /* at least 4 args */
145 if (strcmp(argv[1], "read") == 0) {
146 ulong addr = simple_strtoul(argv[2], NULL, 16);
147 ulong cnt = simple_strtoul(argv[4], NULL, 16);
148 ulong n;
149 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
150
151 printf("\nSATA read: device %d block # %ld, count %ld ... ",
152 curr_device, blk, cnt);
153
154 n = sata_read(curr_device, blk, cnt, (u32 *)addr);
155
156 /* flush cache after read */
157 flush_cache(addr, cnt * sata_dev_desc[curr_device].blksz);
158
159 printf("%ld blocks read: %s\n",
160 n, (n==cnt) ? "OK" : "ERROR");
161 return (n == cnt) ? 0 : 1;
162 } else if (strcmp(argv[1], "write") == 0) {
163 ulong addr = simple_strtoul(argv[2], NULL, 16);
164 ulong cnt = simple_strtoul(argv[4], NULL, 16);
165 ulong n;
166
167 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
168
169 printf("\nSATA write: device %d block # %ld, count %ld ... ",
170 curr_device, blk, cnt);
171
172 n = sata_write(curr_device, blk, cnt, (u32 *)addr);
173
174 printf("%ld blocks written: %s\n",
175 n, (n == cnt) ? "OK" : "ERROR");
176 return (n == cnt) ? 0 : 1;
177 } else {
178 printf("Usage:\n%s\n", cmdtp->usage);
179 rc = 1;
180 }
181
182 return rc;
183 }
184}
185
186U_BOOT_CMD(
187 sata, 5, 1, do_sata,
188 "sata - SATA sub system\n",
189 "sata info - show available SATA devices\n"
190 "sata device [dev] - show or set current device\n"
191 "sata part [dev] - print partition table\n"
192 "sata read addr blk# cnt\n"
193 "sata write addr blk# cnt\n");