SPEAr : Adding basic SPEAr architecture support.
[oweals/u-boot.git] / common / cmd_sata.c
1 /*
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
31 int sata_curr_device = -1;
32 block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
33
34 int __sata_initialize(void)
35 {
36         int rc;
37         int i;
38
39         for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; i++) {
40                 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         sata_curr_device = 0;
56         return rc;
57 }
58 int sata_initialize(void) __attribute__((weak,alias("__sata_initialize")));
59
60 block_dev_desc_t *sata_get_dev(int dev)
61 {
62         return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
63 }
64
65 int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
66 {
67         int rc = 0;
68
69         if (argc == 2 && strcmp(argv[1], "init") == 0)
70                 return sata_initialize();
71
72         /* If the user has not yet run `sata init`, do it now */
73         if (sata_curr_device == -1)
74                 if (sata_initialize())
75                         return 1;
76
77         switch (argc) {
78         case 0:
79         case 1:
80                 cmd_usage(cmdtp);
81                 return 1;
82         case 2:
83                 if (strncmp(argv[1],"inf", 3) == 0) {
84                         int i;
85                         putc('\n');
86                         for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
87                                 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
88                                         continue;
89                                 printf ("SATA device %d: ", i);
90                                 dev_print(&sata_dev_desc[i]);
91                         }
92                         return 0;
93                 } else if (strncmp(argv[1],"dev", 3) == 0) {
94                         if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
95                                 puts("\nno SATA devices available\n");
96                                 return 1;
97                         }
98                         printf("\nSATA device %d: ", sata_curr_device);
99                         dev_print(&sata_dev_desc[sata_curr_device]);
100                         return 0;
101                 } else if (strncmp(argv[1],"part",4) == 0) {
102                         int dev, ok;
103
104                         for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
105                                 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
106                                         ++ok;
107                                         if (dev)
108                                                 putc ('\n');
109                                         print_part(&sata_dev_desc[dev]);
110                                 }
111                         }
112                         if (!ok) {
113                                 puts("\nno SATA devices available\n");
114                                 rc ++;
115                         }
116                         return rc;
117                 }
118                 cmd_usage(cmdtp);
119                 return 1;
120         case 3:
121                 if (strncmp(argv[1], "dev", 3) == 0) {
122                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
123
124                         printf("\nSATA device %d: ", dev);
125                         if (dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
126                                 puts ("unknown device\n");
127                                 return 1;
128                         }
129                         dev_print(&sata_dev_desc[dev]);
130
131                         if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
132                                 return 1;
133
134                         sata_curr_device = dev;
135
136                         puts("... is now current device\n");
137
138                         return 0;
139                 } else if (strncmp(argv[1], "part", 4) == 0) {
140                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
141
142                         if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
143                                 print_part(&sata_dev_desc[dev]);
144                         } else {
145                                 printf("\nSATA device %d not available\n", dev);
146                                 rc = 1;
147                         }
148                         return rc;
149                 }
150                 cmd_usage(cmdtp);
151                 return 1;
152
153         default: /* at least 4 args */
154                 if (strcmp(argv[1], "read") == 0) {
155                         ulong addr = simple_strtoul(argv[2], NULL, 16);
156                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
157                         ulong n;
158                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
159
160                         printf("\nSATA read: device %d block # %ld, count %ld ... ",
161                                 sata_curr_device, blk, cnt);
162
163                         n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
164
165                         /* flush cache after read */
166                         flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
167
168                         printf("%ld blocks read: %s\n",
169                                 n, (n==cnt) ? "OK" : "ERROR");
170                         return (n == cnt) ? 0 : 1;
171                 } else if (strcmp(argv[1], "write") == 0) {
172                         ulong addr = simple_strtoul(argv[2], NULL, 16);
173                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
174                         ulong n;
175
176                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
177
178                         printf("\nSATA write: device %d block # %ld, count %ld ... ",
179                                 sata_curr_device, blk, cnt);
180
181                         n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
182
183                         printf("%ld blocks written: %s\n",
184                                 n, (n == cnt) ? "OK" : "ERROR");
185                         return (n == cnt) ? 0 : 1;
186                 } else {
187                         cmd_usage(cmdtp);
188                         rc = 1;
189                 }
190
191                 return rc;
192         }
193 }
194
195 U_BOOT_CMD(
196         sata, 5, 1, do_sata,
197         "SATA sub system",
198         "sata init - init SATA sub system\n"
199         "sata info - show available SATA devices\n"
200         "sata device [dev] - show or set current device\n"
201         "sata part [dev] - print partition table\n"
202         "sata read addr blk# cnt\n"
203         "sata write addr blk# cnt"
204 );