Additional fix to readline_into_buffer() with CONFIG_CMDLINE_EDITING before relocating
[platform/kernel/u-boot.git] / common / cmd_onenand.c
1 /*
2  *  U-Boot command for OneNAND support
3  *
4  *  Copyright (C) 2005-2007 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <common.h>
13 #include <command.h>
14
15 #ifdef CONFIG_CMD_ONENAND
16
17 #include <linux/mtd/compat.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/onenand.h>
20
21 #include <asm/io.h>
22
23 extern struct mtd_info onenand_mtd;
24 extern struct onenand_chip onenand_chip;
25
26 int do_onenand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
27 {
28         int ret = 0;
29
30         switch (argc) {
31         case 0:
32         case 1:
33                 printf("Usage:\n%s\n", cmdtp->usage);
34                 return 1;
35
36         case 2:
37                 if (strncmp(argv[1], "open", 4) == 0) {
38                         onenand_init();
39                         return 0;
40                 }
41                 onenand_print_device_info(onenand_chip.device_id, 1);
42                 return 0;
43
44         default:
45                 /* At least 4 args */
46                 if (strncmp(argv[1], "erase", 5) == 0) {
47                         struct erase_info instr = {
48                                 .callback       = NULL,
49                         };
50                         ulong start, end;
51                         ulong block;
52                         char *endtail;
53
54                         if (strncmp(argv[2], "block", 5) == 0) {
55                                 start = simple_strtoul(argv[3], NULL, 10);
56                                 endtail = strchr(argv[3], '-');
57                                 end = simple_strtoul(endtail + 1, NULL, 10);
58                         } else {
59                                 start = simple_strtoul(argv[2], NULL, 10);
60                                 end = simple_strtoul(argv[3], NULL, 10);
61                                 start -= (unsigned long)onenand_chip.base;
62                                 end -= (unsigned long)onenand_chip.base;
63
64                                 start >>= onenand_chip.erase_shift;
65                                 end >>= onenand_chip.erase_shift;
66                                 /* Don't include the end block */
67                                 end--;
68                         }
69
70                         if (!end || end < 0)
71                                 end = start;
72
73                         printf("Erase block from %d to %d\n", start, end);
74
75                         for (block = start; block <= end; block++) {
76                                 instr.addr = block << onenand_chip.erase_shift;
77                                 instr.len = 1 << onenand_chip.erase_shift;
78                                 ret = onenand_erase(&onenand_mtd, &instr);
79                                 if (ret) {
80                                         printf("erase failed %d\n", block);
81                                         break;
82                                 }
83                         }
84
85                         return 0;
86                 }
87
88                 if (strncmp(argv[1], "read", 4) == 0) {
89                         ulong addr = simple_strtoul(argv[2], NULL, 16);
90                         ulong ofs = simple_strtoul(argv[3], NULL, 16);
91                         size_t len = simple_strtoul(argv[4], NULL, 16);
92                         size_t retlen = 0;
93                         int oob = strncmp(argv[1], "read.oob", 8) ? 0 : 1;
94
95                         ofs -= (unsigned long)onenand_chip.base;
96
97                         if (oob)
98                                 onenand_read_oob(&onenand_mtd, ofs, len,
99                                                  &retlen, (u_char *) addr);
100                         else
101                                 onenand_read(&onenand_mtd, ofs, len, &retlen,
102                                              (u_char *) addr);
103                         printf("Done\n");
104
105                         return 0;
106                 }
107
108                 if (strncmp(argv[1], "write", 5) == 0) {
109                         ulong addr = simple_strtoul(argv[2], NULL, 16);
110                         ulong ofs = simple_strtoul(argv[3], NULL, 16);
111                         size_t len = simple_strtoul(argv[4], NULL, 16);
112                         size_t retlen = 0;
113
114                         ofs -= (unsigned long)onenand_chip.base;
115
116                         onenand_write(&onenand_mtd, ofs, len, &retlen,
117                                       (u_char *) addr);
118                         printf("Done\n");
119
120                         return 0;
121                 }
122
123                 if (strncmp(argv[1], "block", 5) == 0) {
124                         ulong addr = simple_strtoul(argv[2], NULL, 16);
125                         ulong block = simple_strtoul(argv[3], NULL, 10);
126                         ulong page = simple_strtoul(argv[4], NULL, 10);
127                         size_t len = simple_strtol(argv[5], NULL, 10);
128                         size_t retlen = 0;
129                         ulong ofs;
130                         int oob = strncmp(argv[1], "block.oob", 9) ? 0 : 1;
131
132                         ofs = block << onenand_chip.erase_shift;
133                         if (page)
134                                 ofs += page << onenand_chip.page_shift;
135
136                         if (!len) {
137                                 if (oob)
138                                         len = 64;
139                                 else
140                                         len = 512;
141                         }
142
143                         if (oob)
144                                 onenand_read_oob(&onenand_mtd, ofs, len,
145                                                  &retlen, (u_char *) addr);
146                         else
147                                 onenand_read(&onenand_mtd, ofs, len, &retlen,
148                                              (u_char *) addr);
149                         return 0;
150                 }
151
152                 break;
153         }
154
155         return 0;
156 }
157
158 U_BOOT_CMD(
159         onenand,        6,      1,      do_onenand,
160         "onenand - OneNAND sub-system\n",
161         "info   - show available OneNAND devices\n"
162         "onenand read[.oob] addr ofs len - read data at ofs with len to addr\n"
163         "onenand write addr ofs len - write data at ofs with len from addr\n"
164         "onenand erase saddr eaddr - erase block start addr to end addr\n"
165         "onenand block[.oob] addr block [page] [len] - "
166                 "read data with (block [, page]) to addr"
167 );
168
169 #endif /* CONFIG_CMD_ONENAND */