]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_sata.c
Unified codebase for TX28, TX48, TX51, TX53
[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 (!rc && (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 #ifdef CONFIG_PARTITIONS
67 block_dev_desc_t *sata_get_dev(int dev)
68 {
69         return (dev < CONFIG_SYS_SATA_MAX_DEVICE) ? &sata_dev_desc[dev] : NULL;
70 }
71 #endif
72
73 int do_sata(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
74 {
75         int rc = 0;
76
77         if (argc == 2 && strcmp(argv[1], "init") == 0)
78                 return sata_initialize();
79
80         /* If the user has not yet run `sata init`, do it now */
81         if (sata_curr_device == -1)
82                 if (sata_initialize())
83                         return 1;
84
85         switch (argc) {
86         case 0:
87         case 1:
88                 return CMD_RET_USAGE;
89         case 2:
90                 if (strncmp(argv[1],"inf", 3) == 0) {
91                         int i;
92                         putc('\n');
93                         for (i = 0; i < CONFIG_SYS_SATA_MAX_DEVICE; ++i) {
94                                 if (sata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
95                                         continue;
96                                 printf ("SATA device %d: ", i);
97                                 dev_print(&sata_dev_desc[i]);
98                         }
99                         return 0;
100                 } else if (strncmp(argv[1],"dev", 3) == 0) {
101                         if ((sata_curr_device < 0) || (sata_curr_device >= CONFIG_SYS_SATA_MAX_DEVICE)) {
102                                 puts("\nno SATA devices available\n");
103                                 return 1;
104                         }
105                         printf("\nSATA device %d: ", sata_curr_device);
106                         dev_print(&sata_dev_desc[sata_curr_device]);
107                         return 0;
108                 } else if (strncmp(argv[1],"part",4) == 0) {
109                         int dev, ok;
110
111                         for (ok = 0, dev = 0; dev < CONFIG_SYS_SATA_MAX_DEVICE; ++dev) {
112                                 if (sata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
113                                         ++ok;
114                                         if (dev)
115                                                 putc ('\n');
116                                         print_part(&sata_dev_desc[dev]);
117                                 }
118                         }
119                         if (!ok) {
120                                 puts("\nno SATA devices available\n");
121                                 rc ++;
122                         }
123                         return rc;
124                 }
125                 return CMD_RET_USAGE;
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                 return CMD_RET_USAGE;
157
158         default: /* at least 4 args */
159                 if (strcmp(argv[1], "read") == 0) {
160                         ulong addr = simple_strtoul(argv[2], NULL, 16);
161                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
162                         ulong n;
163                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
164
165                         printf("\nSATA read: device %d block # %ld, count %ld ... ",
166                                 sata_curr_device, blk, cnt);
167
168                         n = sata_read(sata_curr_device, blk, cnt, (u32 *)addr);
169
170                         /* flush cache after read */
171                         flush_cache(addr, cnt * sata_dev_desc[sata_curr_device].blksz);
172
173                         printf("%ld blocks read: %s\n",
174                                 n, (n==cnt) ? "OK" : "ERROR");
175                         return (n == cnt) ? 0 : 1;
176                 } else if (strcmp(argv[1], "write") == 0) {
177                         ulong addr = simple_strtoul(argv[2], NULL, 16);
178                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
179                         ulong n;
180
181                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
182
183                         printf("\nSATA write: device %d block # %ld, count %ld ... ",
184                                 sata_curr_device, blk, cnt);
185
186                         n = sata_write(sata_curr_device, blk, cnt, (u32 *)addr);
187
188                         printf("%ld blocks written: %s\n",
189                                 n, (n == cnt) ? "OK" : "ERROR");
190                         return (n == cnt) ? 0 : 1;
191                 } else {
192                         return CMD_RET_USAGE;
193                 }
194
195                 return rc;
196         }
197 }
198
199 U_BOOT_CMD(
200         sata, 5, 1, do_sata,
201         "SATA sub system",
202         "sata init - init SATA sub system\n"
203         "sata info - show available SATA devices\n"
204         "sata device [dev] - show or set current device\n"
205         "sata part [dev] - print partition table\n"
206         "sata read addr blk# cnt\n"
207         "sata write addr blk# cnt"
208 );