]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_sata.c
imported Freescale specific U-Boot additions for i.MX28,... release L2.6.31_10.08.01
[karo-tx-uboot.git] / common / cmd_sata.c
1 /*
2  * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
3  * Terry Lv <r65388@freescale.com>
4  *
5  * Copyright (C) 2000-2005, DENX Software Engineering
6  *              Wolfgang Denk <wd@denx.de>
7  * Copyright (C) Procsys. All rights reserved.
8  *              Mushtaq Khan <mushtaq_k@procsys.com>
9  *                      <mushtaqk_921@yahoo.co.in>
10  * Copyright (C) 2008 Freescale Semiconductor, Inc.
11  *              Dave Liu <daveliu@freescale.com>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include <common.h>
30 #include <command.h>
31 #include <part.h>
32 #include <sata.h>
33
34 int sata_curr_device = -1;
35 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
36
37 int __sata_initialize(void)
38 {
39         int rc;
40         int i;
41
42         for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
43                 memset(&sata_dev_desc[i], 0, sizeof(struct block_dev_desc));
44                 sata_dev_desc[i].if_type = IF_TYPE_SATA;
45                 sata_dev_desc[i].dev = i;
46                 sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
47                 sata_dev_desc[i].type = DEV_TYPE_HARDDISK;
48                 sata_dev_desc[i].lba = 0;
49                 sata_dev_desc[i].blksz = 512;
50                 sata_dev_desc[i].block_read = sata_read;
51                 sata_dev_desc[i].block_write = sata_write;
52
53                 rc = init_sata(i);
54                 if (!rc) {
55                         rc = scan_sata(i);
56                         if ((sata_dev_desc[i].lba > 0) &&
57                                 (sata_dev_desc[i].blksz > 0))
58                                 init_part(&sata_dev_desc[i]);
59                 }
60         }
61         sata_curr_device = 0;
62         return rc;
63 }
64 int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
65
66 block_dev_desc_t *sata_get_dev(int dev)
67 {
68         return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
69 }
70
71 int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
72 {
73         int rc = 0;
74
75         if (argc == 2 && strcmp(argv[1], "init") == 0)
76                 return sata_initialize();
77
78         /* If the user has not yet run `sata init`, do it now */
79         if (sata_curr_device == -1)
80                 if (sata_initialize())
81                         return 1;
82
83         switch (argc) {
84         case 0:
85         case 1:
86                 cmd_usage(cmdtp);
87                 return 1;
88         case 2:
89                 if (strncmp(argv[1],"inf", 3) == 0) {
90                         int i;
91                         putc('\n');
92                         for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
93                                 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
94                                         continue;
95                                 printf ("SATA device %d: ", i);
96                                 dev_print(&sata_dev_desc[i]);
97                         }
98                         return 0;
99                 } else if (strncmp(argv[1],"dev", 3) == 0) {
100                         if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
101                                 puts("\nno SATA devices available\n");
102                                 return 1;
103                         }
104                         printf("\nSATA device %d: ", sata_curr_device);
105                         dev_print(&sata_dev_desc[sata_curr_device]);
106                         return 0;
107                 } else if (strncmp(argv[1],"part",4) == 0) {
108                         int dev, ok;
109
110                         for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
111                                 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
112                                         ++ok;
113                                         if (dev)
114                                                 putc ('\n');
115                                         print_part(&sata_dev_desc[dev]);
116                                 }
117                         }
118                         if (!ok) {
119                                 puts("\nno SATA devices available\n");
120                                 rc ++;
121                         }
122                         return rc;
123                 }
124                 cmd_usage(cmdtp);
125                 return 1;
126         case 3:
127                 if (strncmp(argv[1], "dev", 3) == 0) {
128                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
129
130                         printf("\nSATA device %d: ", dev);
131                         if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
132                                 puts ("unknown device\n");
133                                 return 1;
134                         }
135                         dev_print(&sata_dev_desc[dev]);
136
137                         if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
138                                 return 1;
139
140                         sata_curr_device = dev;
141
142                         puts("... is now current device\n");
143
144                         return 0;
145                 } else if (strncmp(argv[1], "part", 4) == 0) {
146                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
147
148                         if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
149                                 print_part(&sata_dev_desc[dev]);
150                         } else {
151                                 printf("\nSATA device %d not available\n", dev);
152                                 rc = 1;
153                         }
154                         return rc;
155                 }
156                 cmd_usage(cmdtp);
157                 return 1;
158
159         default: /* at least 4 args */
160                 if (strcmp(argv[1], "read") == 0) {
161                         ulong addr = simple_strtoul(argv[2], NULL, 16);
162                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
163                         ulong n;
164                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
165
166                         printf("\nSATA read: device %d block # %ld, count %ld ... ",
167                                 sata_curr_device, blk, cnt);
168
169                         n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
170
171                         /* flush cache after read */
172                         flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
173
174                         printf("%ld blocks read: %s\n",
175                                 n, (n==cnt) ? "OK" : "ERROR");
176                         return (n == cnt) ? 0 : 1;
177                 } else if (strcmp(argv[1], "write") == 0) {
178                         ulong addr = simple_strtoul(argv[2], NULL, 16);
179                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
180                         ulong n;
181
182                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
183
184                         printf("\nSATA write: device %d block # %ld, count %ld ... ",
185                                 sata_curr_device, blk, cnt);
186
187                         n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
188
189                         printf("%ld blocks written: %s\n",
190                                 n, (n == cnt) ? "OK" : "ERROR");
191                         return (n == cnt) ? 0 : 1;
192                 } else {
193                         cmd_usage(cmdtp);
194                         rc = 1;
195                 }
196
197                 return rc;
198         }
199 }
200
201 U_BOOT_CMD(
202         sata, 5, 1, do_sata,
203         "SATA sub system",
204         "sata init - init SATA sub system\n"
205         "sata info - show available SATA devices\n"
206         "sata device [dev] - show or set current device\n"
207         "sata part [dev] - print partition table\n"
208         "sata read addr blk# cnt\n"
209         "sata write addr blk# cnt"
210 );