2 * atmel_df_pow2.c - convert Atmel Dataflashes to Power of 2 mode
4 * Copyright 2009 Analog Devices Inc.
6 * Licensed under the 2-clause BSD.
12 #include <linux/delay.h>
18 static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len)
21 return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
24 static int flash_status(struct spi_slave *slave)
27 if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf)))
32 static int flash_set_pow2(struct spi_slave *slave)
41 ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf));
45 /* wait Tp, or 6 msec */
48 ret = flash_status(slave);
52 return ret & 0x1 ? 0 : 1;
55 static int flash_check(struct spi_slave *slave)
60 ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf));
65 printf("atmel flash not found (id[0] = %#x)\n", buf[1]);
69 if ((buf[2] >> 5) != 0x1) {
70 printf("AT45 flash not found (id[0] = %#x)\n", buf[2]);
77 static char *getline(void)
79 static char buffer[100];
90 case '\r': /* Enter/Return key */
95 case 0x03: /* ^C - break */
99 case 0x08: /* ^H - backspace */
100 case 0x7F: /* DEL - backspace */
108 /* Ignore control characters */
111 /* Queue up all other characters */
119 int atmel_df_pow2(int argc, char *const argv[])
121 /* Print the ABI version */
123 if (XF_VERSION != get_version()) {
124 printf("Expects ABI version %d\n", XF_VERSION);
125 printf("Actual U-Boot ABI version %lu\n", get_version());
126 printf("Can't run\n\n");
131 struct spi_slave *slave;
135 puts("\nenter the [BUS:]CS of the SPI flash: ");
144 bus = cs = simple_strtoul(line, &p, 10);
148 cs = simple_strtoul(p, &p, 10);
151 puts("invalid format, please try again\n");
157 printf("\ngoing to work with dataflash at %i:%i\n", bus, cs);
159 /* use a low speed -- it'll work with all devices, and
160 * speed here doesn't really matter.
162 slave = spi_setup_slave(bus, cs, 1000, SPI_MODE_3);
164 puts("unable to setup slave\n");
168 if (spi_claim_bus(slave)) {
169 spi_free_slave(slave);
173 if (flash_check(slave)) {
174 puts("no flash found\n");
178 status = flash_status(slave);
180 puts("unable to read status register\n");
184 puts("flash is already in power-of-2 mode!\n");
188 puts("are you sure you wish to set power-of-2 mode?\n");
189 puts("this operation is permanent and irreversible\n");
190 printf("enter YES to continue: ");
192 if (!line || strcmp(line, "YES"))
195 if (flash_set_pow2(slave)) {
196 puts("setting pow2 mode failed\n");
201 "Configuration should be updated now. You will have to\n"
202 "power cycle the part in order to finish the conversion.\n"
206 spi_release_bus(slave);
207 spi_free_slave(slave);