2 * Command for accessing SPI flash.
4 * Copyright (C) 2008 Atmel Corporation
11 #ifndef CONFIG_SF_DEFAULT_SPEED
12 # define CONFIG_SF_DEFAULT_SPEED 1000000
14 #ifndef CONFIG_SF_DEFAULT_MODE
15 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
18 static struct spi_flash *flash;
20 static int do_spi_flash_probe(int argc, char *argv[])
24 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
25 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
27 struct spi_flash *new;
32 cs = simple_strtoul(argv[1], &endp, 0);
33 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
40 cs = simple_strtoul(endp + 1, &endp, 0);
46 speed = simple_strtoul(argv[2], &endp, 0);
47 if (*argv[2] == 0 || *endp != 0)
51 mode = simple_strtoul(argv[3], &endp, 0);
52 if (*argv[3] == 0 || *endp != 0)
56 new = spi_flash_probe(bus, cs, speed, mode);
58 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
63 spi_flash_free(flash);
66 printf("%u KiB %s at %u:%u is now current device\n",
67 flash->size >> 10, flash->name, bus, cs);
72 puts("Usage: sf probe [bus:]cs [hz] [mode]\n");
76 static int do_spi_flash_read_write(int argc, char *argv[])
88 addr = simple_strtoul(argv[1], &endp, 16);
89 if (*argv[1] == 0 || *endp != 0)
91 offset = simple_strtoul(argv[2], &endp, 16);
92 if (*argv[2] == 0 || *endp != 0)
94 len = simple_strtoul(argv[3], &endp, 16);
95 if (*argv[3] == 0 || *endp != 0)
98 buf = map_physmem(addr, len, MAP_WRBACK);
100 puts("Failed to map physical memory\n");
104 if (strcmp(argv[0], "read") == 0)
105 ret = spi_flash_read(flash, offset, len, buf);
107 ret = spi_flash_write(flash, offset, len, buf);
109 unmap_physmem(buf, len);
112 printf("SPI flash %s failed\n", argv[0]);
119 printf("Usage: sf %s addr offset len\n", argv[0]);
123 static int do_spi_flash_erase(int argc, char *argv[])
125 unsigned long offset;
133 offset = simple_strtoul(argv[1], &endp, 16);
134 if (*argv[1] == 0 || *endp != 0)
136 len = simple_strtoul(argv[2], &endp, 16);
137 if (*argv[2] == 0 || *endp != 0)
140 ret = spi_flash_erase(flash, offset, len);
142 printf("SPI flash %s failed\n", argv[0]);
149 puts("Usage: sf erase offset len\n");
153 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
157 /* need at least two arguments */
163 if (strcmp(cmd, "probe") == 0)
164 return do_spi_flash_probe(argc - 1, argv + 1);
166 /* The remaining commands require a selected device */
168 puts("No SPI flash selected. Please run `sf probe'\n");
172 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
173 return do_spi_flash_read_write(argc - 1, argv + 1);
174 if (strcmp(cmd, "erase") == 0)
175 return do_spi_flash_erase(argc - 1, argv + 1);
178 printf("Usage:\n%s\n", cmdtp->usage);
183 sf, 5, 1, do_spi_flash,
184 "sf - SPI flash sub-system\n",
185 "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
187 "sf read addr offset len - read `len' bytes starting at\n"
188 " `offset' to memory at `addr'\n"
189 "sf write addr offset len - write `len' bytes from memory\n"
190 " at `addr' to flash at `offset'\n"
191 "sf erase offset len - erase `len' bytes from `offset'\n");