blob: 27d6e39a18dbaa825e0ba5b7ab9092511d6b8381 [file] [log] [blame]
Haavard Skinnemoen21266a32008-05-16 11:10:34 +02001/*
2 * Command for accessing SPI flash.
3 *
4 * Copyright (C) 2008 Atmel Corporation
Mike Frysinger22f8b682009-10-09 17:12:44 -04005 * Licensed under the GPL-2 or later.
Haavard Skinnemoen21266a32008-05-16 11:10:34 +02006 */
Mike Frysinger22f8b682009-10-09 17:12:44 -04007
Haavard Skinnemoen21266a32008-05-16 11:10:34 +02008#include <common.h>
9#include <spi_flash.h>
10
11#include <asm/io.h>
12
13#ifndef CONFIG_SF_DEFAULT_SPEED
14# define CONFIG_SF_DEFAULT_SPEED 1000000
15#endif
16#ifndef CONFIG_SF_DEFAULT_MODE
17# define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
18#endif
19
20static struct spi_flash *flash;
21
Richard Retanubun605dbd12011-02-16 16:39:44 -050022
23/*
24 * This function computes the length argument for the erase command.
25 * The length on which the command is to operate can be given in two forms:
26 * 1. <cmd> offset len - operate on <'offset', 'len')
27 * 2. <cmd> offset +len - operate on <'offset', 'round_up(len)')
28 * If the second form is used and the length doesn't fall on the
29 * sector boundary, than it will be adjusted to the next sector boundary.
30 * If it isn't in the flash, the function will fail (return -1).
31 * Input:
32 * arg: length specification (i.e. both command arguments)
33 * Output:
34 * len: computed length for operation
35 * Return:
36 * 1: success
37 * -1: failure (bad format, bad address).
38 */
39static int sf_parse_len_arg(char *arg, ulong *len)
40{
41 char *ep;
42 char round_up_len; /* indicates if the "+length" form used */
43 ulong len_arg;
44
45 round_up_len = 0;
46 if (*arg == '+') {
47 round_up_len = 1;
48 ++arg;
49 }
50
51 len_arg = simple_strtoul(arg, &ep, 16);
52 if (ep == arg || *ep != '\0')
53 return -1;
54
55 if (round_up_len && flash->sector_size > 0)
Anton Staaf07a036a2011-09-02 13:45:28 +000056 *len = ROUND(len_arg, flash->sector_size);
Richard Retanubun605dbd12011-02-16 16:39:44 -050057 else
58 *len = len_arg;
59
60 return 1;
61}
62
Wolfgang Denk6262d0212010-06-28 22:00:46 +020063static int do_spi_flash_probe(int argc, char * const argv[])
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020064{
65 unsigned int bus = 0;
66 unsigned int cs;
67 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
68 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
69 char *endp;
70 struct spi_flash *new;
71
72 if (argc < 2)
Mike Frysinger153c3cd2011-04-12 02:21:31 -040073 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020074
75 cs = simple_strtoul(argv[1], &endp, 0);
76 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
Mike Frysinger153c3cd2011-04-12 02:21:31 -040077 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020078 if (*endp == ':') {
79 if (endp[1] == 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -040080 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020081
82 bus = cs;
83 cs = simple_strtoul(endp + 1, &endp, 0);
84 if (*endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -040085 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020086 }
87
88 if (argc >= 3) {
89 speed = simple_strtoul(argv[2], &endp, 0);
90 if (*argv[2] == 0 || *endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -040091 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020092 }
93 if (argc >= 4) {
TsiChung Liewef4a5622009-06-30 14:30:19 +000094 mode = simple_strtoul(argv[3], &endp, 16);
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020095 if (*argv[3] == 0 || *endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -040096 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +020097 }
98
99 new = spi_flash_probe(bus, cs, speed, mode);
100 if (!new) {
101 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
102 return 1;
103 }
104
105 if (flash)
106 spi_flash_free(flash);
107 flash = new;
108
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200109 return 0;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200110}
111
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200112static int do_spi_flash_read_write(int argc, char * const argv[])
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200113{
114 unsigned long addr;
115 unsigned long offset;
116 unsigned long len;
117 void *buf;
118 char *endp;
119 int ret;
120
121 if (argc < 4)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400122 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200123
124 addr = simple_strtoul(argv[1], &endp, 16);
125 if (*argv[1] == 0 || *endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400126 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200127 offset = simple_strtoul(argv[2], &endp, 16);
128 if (*argv[2] == 0 || *endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400129 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200130 len = simple_strtoul(argv[3], &endp, 16);
131 if (*argv[3] == 0 || *endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400132 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200133
134 buf = map_physmem(addr, len, MAP_WRBACK);
135 if (!buf) {
136 puts("Failed to map physical memory\n");
137 return 1;
138 }
139
140 if (strcmp(argv[0], "read") == 0)
141 ret = spi_flash_read(flash, offset, len, buf);
142 else
143 ret = spi_flash_write(flash, offset, len, buf);
144
145 unmap_physmem(buf, len);
146
147 if (ret) {
148 printf("SPI flash %s failed\n", argv[0]);
149 return 1;
150 }
151
152 return 0;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200153}
154
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200155static int do_spi_flash_erase(int argc, char * const argv[])
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200156{
157 unsigned long offset;
158 unsigned long len;
159 char *endp;
160 int ret;
161
162 if (argc < 3)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400163 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200164
165 offset = simple_strtoul(argv[1], &endp, 16);
166 if (*argv[1] == 0 || *endp != 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400167 return -1;
Richard Retanubun605dbd12011-02-16 16:39:44 -0500168
169 ret = sf_parse_len_arg(argv[2], &len);
170 if (ret != 1)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400171 return -1;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200172
173 ret = spi_flash_erase(flash, offset, len);
174 if (ret) {
175 printf("SPI flash %s failed\n", argv[0]);
176 return 1;
177 }
178
179 return 0;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200180}
181
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200182static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200183{
184 const char *cmd;
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400185 int ret;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200186
187 /* need at least two arguments */
188 if (argc < 2)
189 goto usage;
190
191 cmd = argv[1];
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400192 --argc;
193 ++argv;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200194
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400195 if (strcmp(cmd, "probe") == 0) {
196 ret = do_spi_flash_probe(argc, argv);
197 goto done;
198 }
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200199
200 /* The remaining commands require a selected device */
201 if (!flash) {
202 puts("No SPI flash selected. Please run `sf probe'\n");
203 return 1;
204 }
205
206 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
Mike Frysinger153c3cd2011-04-12 02:21:31 -0400207 ret = do_spi_flash_read_write(argc, argv);
208 else if (strcmp(cmd, "erase") == 0)
209 ret = do_spi_flash_erase(argc, argv);
210 else
211 ret = -1;
212
213done:
214 if (ret != -1)
215 return ret;
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200216
217usage:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200218 return cmd_usage(cmdtp);
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200219}
220
221U_BOOT_CMD(
222 sf, 5, 1, do_spi_flash,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600223 "SPI flash sub-system",
Haavard Skinnemoen21266a32008-05-16 11:10:34 +0200224 "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
225 " and chip select\n"
226 "sf read addr offset len - read `len' bytes starting at\n"
227 " `offset' to memory at `addr'\n"
228 "sf write addr offset len - write `len' bytes from memory\n"
229 " at `addr' to flash at `offset'\n"
Richard Retanubun605dbd12011-02-16 16:39:44 -0500230 "sf erase offset [+]len - erase `len' bytes from `offset'\n"
231 " `+len' round up `len' to block size"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200232);