]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_pata.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / common / cmd_pata.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  * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
8  *              Terry Lv <r65388@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 <ata.h>
30 #include <pata.h>
31
32 int pata_curr_device = -1;
33 block_dev_desc_t pata_dev_desc[CONFIG_SYS_ATA_MAX_DEVICE];
34
35 int pata_initialize(void)
36 {
37         int rc;
38         int i;
39
40         for (i = 0; i < CONFIG_SYS_ATA_MAX_DEVICE; i++) {
41                 memset(&pata_dev_desc[i], 0, sizeof(struct block_dev_desc));
42                 pata_dev_desc[i].if_type = IF_TYPE_ATAPI;
43                 pata_dev_desc[i].dev = i;
44                 pata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
45                 pata_dev_desc[i].type = DEV_TYPE_HARDDISK;
46                 pata_dev_desc[i].lba = 0;
47                 pata_dev_desc[i].blksz = 512;
48                 pata_dev_desc[i].block_read = pata_read;
49                 pata_dev_desc[i].block_write = pata_write;
50
51                 rc = init_pata(i);
52                 rc = scan_pata(i);
53                 if ((pata_dev_desc[i].lba > 0) && (pata_dev_desc[i].blksz > 0))
54                         init_part(&pata_dev_desc[i]);
55         }
56         pata_curr_device = 0;
57         return rc;
58 }
59
60 block_dev_desc_t *pata_get_dev(int dev)
61 {
62         return (dev < CONFIG_SYS_ATA_MAX_DEVICE) ? &pata_dev_desc[dev] : NULL;
63 }
64
65 int do_pata(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 pata_initialize();
71
72         /* If the user has not yet run `pata init`, do it now */
73         if (pata_curr_device == -1)
74                 if (pata_initialize())
75                         return 1;
76
77         switch (argc) {
78         case 0:
79         case 1:
80                 printf("Usage:\n%s\n", cmdtp->usage);
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_ATA_MAX_DEVICE; ++i) {
87                                 if (pata_dev_desc[i].type == DEV_TYPE_UNKNOWN)
88                                         continue;
89                                 printf("PATA device %d: ", i);
90                                 dev_print(&pata_dev_desc[i]);
91                         }
92                         return 0;
93                 } else if (strncmp(argv[1], "dev", 3) == 0) {
94                         if ((pata_curr_device < 0) || \
95                                         (pata_curr_device >= CONFIG_SYS_ATA_MAX_DEVICE)) {
96                                 puts("\nno PATA devices available\n");
97                                 return 1;
98                         }
99                         printf("\nPATA device %d: ", pata_curr_device);
100                         dev_print(&pata_dev_desc[pata_curr_device]);
101                         return 0;
102                 } else if (strncmp(argv[1], "part", 4) == 0) {
103                         int dev, ok;
104
105                         for (ok = 0, dev = 0; dev < CONFIG_SYS_ATA_MAX_DEVICE; ++dev) {
106                                 if (pata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
107                                         ++ok;
108                                         if (dev)
109                                                 putc('\n');
110                                         print_part(&pata_dev_desc[dev]);
111                                 }
112                         }
113                         if (!ok) {
114                                 puts("\nno PATA devices available\n");
115                                 rc++;
116                         }
117                         return rc;
118                 }
119                 printf("Usage:\n%s\n", cmdtp->usage);
120                 return 1;
121         case 3:
122                 if (strncmp(argv[1], "dev", 3) == 0) {
123                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
124
125                         printf("\nPATA device %d: ", dev);
126                         if (dev >= CONFIG_SYS_ATA_MAX_DEVICE) {
127                                 puts("unknown device\n");
128                                 return 1;
129                         }
130                         dev_print(&pata_dev_desc[dev]);
131
132                         if (pata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
133                                 return 1;
134
135                         pata_curr_device = dev;
136
137                         puts("... is now current device\n");
138
139                         return 0;
140                 } else if (strncmp(argv[1], "part", 4) == 0) {
141                         int dev = (int)simple_strtoul(argv[2], NULL, 10);
142
143                         if (pata_dev_desc[dev].part_type != PART_TYPE_UNKNOWN) {
144                                 print_part(&pata_dev_desc[dev]);
145                         } else {
146                                 printf("\nPATA device %d not available\n", dev);
147                                 rc = 1;
148                         }
149                         return rc;
150                 }
151                 printf ("Usage:\n%s\n", cmdtp->usage);
152                 return 1;
153
154         default: /* at least 4 args */
155                 if (strcmp(argv[1], "read") == 0) {
156                         ulong addr = simple_strtoul(argv[2], NULL, 16);
157                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
158                         ulong n;
159                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
160
161                         printf("\nPATA read: device %d block # %ld, count %ld ... ",
162                                 pata_curr_device, blk, cnt);
163
164                         n = pata_read(pata_curr_device, blk, cnt, (u32 *)addr);
165
166                         /* flush cache after read */
167                         flush_cache(addr, cnt * pata_dev_desc[pata_curr_device].blksz);
168
169                         printf("%ld blocks read: %s\n",
170                                 n, (n == cnt) ? "OK" : "ERROR");
171                         return (n == cnt) ? 0 : 1;
172                 } else if (strcmp(argv[1], "write") == 0) {
173                         ulong addr = simple_strtoul(argv[2], NULL, 16);
174                         ulong cnt = simple_strtoul(argv[4], NULL, 16);
175                         ulong n;
176
177                         lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
178
179                         printf("\nPATA write: device %d block # %ld, count %ld ... ",
180                                 pata_curr_device, blk, cnt);
181
182                         n = pata_write(pata_curr_device, blk, cnt, (u32 *)addr);
183
184                         printf("%ld blocks written: %s\n",
185                                 n, (n == cnt) ? "OK" : "ERROR");
186                         return (n == cnt) ? 0 : 1;
187                 } else {
188                         printf("Usage:\n%s\n", cmdtp->usage);
189                         rc = 1;
190                 }
191
192                 return rc;
193         }
194 }
195
196 U_BOOT_CMD(
197         pata, 5, 1, do_pata,
198         "pata   - PATA sub system\n",
199         "pata info - show available PATA devices\n"
200         "pata device [dev] - show or set current device\n"
201         "pata part [dev] - print partition table\n"
202         "pata read addr blk# cnt\n"
203         "pata write addr blk# cnt\n");