2 * Command for accessing SPI flash.
4 * Copyright (C) 2008 Atmel Corporation
5 * Licensed under the GPL-2 or later.
13 #ifndef CONFIG_SF_DEFAULT_SPEED
14 # define CONFIG_SF_DEFAULT_SPEED 1000000
16 #ifndef CONFIG_SF_DEFAULT_MODE
17 # define CONFIG_SF_DEFAULT_MODE SPI_MODE_3
20 static struct spi_flash *flash;
22 static int do_spi_flash_probe(int argc, char * const argv[])
26 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
27 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
29 struct spi_flash *new;
34 cs = simple_strtoul(argv[1], &endp, 0);
35 if (*argv[1] == 0 || (*endp != 0 && *endp != ':'))
42 cs = simple_strtoul(endp + 1, &endp, 0);
48 speed = simple_strtoul(argv[2], &endp, 0);
49 if (*argv[2] == 0 || *endp != 0)
53 mode = simple_strtoul(argv[3], &endp, 16);
54 if (*argv[3] == 0 || *endp != 0)
58 new = spi_flash_probe(bus, cs, speed, mode);
60 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
65 spi_flash_free(flash);
68 printf("%u KiB %s at %u:%u is now current device\n",
69 flash->size >> 10, flash->name, bus, cs);
74 puts("Usage: sf probe [bus:]cs [hz] [mode]\n");
78 static int do_spi_flash_read_write(int argc, char * const argv[])
90 addr = simple_strtoul(argv[1], &endp, 16);
91 if (*argv[1] == 0 || *endp != 0)
93 offset = simple_strtoul(argv[2], &endp, 16);
94 if (*argv[2] == 0 || *endp != 0)
96 len = simple_strtoul(argv[3], &endp, 16);
97 if (*argv[3] == 0 || *endp != 0)
100 buf = map_physmem(addr, len, MAP_WRBACK);
102 puts("Failed to map physical memory\n");
106 if (strcmp(argv[0], "read") == 0)
107 ret = spi_flash_read(flash, offset, len, buf);
109 ret = spi_flash_write(flash, offset, len, buf);
111 unmap_physmem(buf, len);
114 printf("SPI flash %s failed\n", argv[0]);
121 printf("Usage: sf %s addr offset len\n", argv[0]);
125 static int do_spi_flash_erase(int argc, char * const argv[])
127 unsigned long offset;
135 offset = simple_strtoul(argv[1], &endp, 16);
136 if (*argv[1] == 0 || *endp != 0)
138 len = simple_strtoul(argv[2], &endp, 16);
139 if (*argv[2] == 0 || *endp != 0)
142 ret = spi_flash_erase(flash, offset, len);
144 printf("SPI flash %s failed\n", argv[0]);
151 puts("Usage: sf erase offset len\n");
155 static int do_spi_flash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
159 /* need at least two arguments */
165 if (strcmp(cmd, "probe") == 0)
166 return do_spi_flash_probe(argc - 1, argv + 1);
168 /* The remaining commands require a selected device */
170 puts("No SPI flash selected. Please run `sf probe'\n");
174 if (strcmp(cmd, "read") == 0 || strcmp(cmd, "write") == 0)
175 return do_spi_flash_read_write(argc - 1, argv + 1);
176 if (strcmp(cmd, "erase") == 0)
177 return do_spi_flash_erase(argc - 1, argv + 1);
180 return cmd_usage(cmdtp);
184 sf, 5, 1, do_spi_flash,
185 "SPI flash sub-system",
186 "probe [bus:]cs [hz] [mode] - init flash device on given SPI bus\n"
188 "sf read addr offset len - read `len' bytes starting at\n"
189 " `offset' to memory at `addr'\n"
190 "sf write addr offset len - write `len' bytes from memory\n"
191 " at `addr' to flash at `offset'\n"
192 "sf erase offset len - erase `len' bytes from `offset'"