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.
17 static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len)
20 return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
23 static int flash_status(struct spi_slave *slave)
26 if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf)))
31 static int flash_set_pow2(struct spi_slave *slave)
40 ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf));
44 /* wait Tp, or 6 msec */
47 ret = flash_status(slave);
51 return ret & 0x1 ? 0 : 1;
54 static int flash_check(struct spi_slave *slave)
59 ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf));
64 printf("atmel flash not found (id[0] = %#x)\n", buf[1]);
68 if ((buf[2] >> 5) != 0x1) {
69 printf("AT45 flash not found (id[0] = %#x)\n", buf[2]);
76 static char *getline(void)
78 static char buffer[100];
89 case '\r': /* Enter/Return key */
94 case 0x03: /* ^C - break */
98 case 0x08: /* ^H - backspace */
99 case 0x7F: /* DEL - backspace */
107 /* Ignore control characters */
110 /* Queue up all other characters */
118 int atmel_df_pow2(int argc, char * const argv[])
120 /* Print the ABI version */
122 if (XF_VERSION != get_version()) {
123 printf("Expects ABI version %d\n", XF_VERSION);
124 printf("Actual U-Boot ABI version %lu\n", get_version());
125 printf("Can't run\n\n");
132 struct spi_slave *slave;
136 puts("\nenter the [BUS:]CS of the SPI flash: ");
145 bus = cs = simple_strtoul(line, &p, 10);
149 cs = simple_strtoul(p, &p, 10);
152 puts("invalid format, please try again\n");
158 printf("\ngoing to work with dataflash at %i:%i\n", bus, cs);
160 /* use a low speed -- it'll work with all devices, and
161 * speed here doesn't really matter.
163 slave = spi_setup_slave(bus, cs, 1000, SPI_MODE_3);
165 puts("unable to setup slave\n");
169 if (spi_claim_bus(slave)) {
170 spi_free_slave(slave);
174 if (flash_check(slave)) {
175 puts("no flash found\n");
179 status = flash_status(slave);
181 puts("unable to read status register\n");
185 puts("flash is already in power-of-2 mode!\n");
189 puts("are you sure you wish to set power-of-2 mode?\n");
190 puts("this operation is permanent and irreversible\n");
191 printf("enter YES to continue: ");
193 if (!line || strcmp(line, "YES"))
196 if (flash_set_pow2(slave)) {
197 puts("setting pow2 mode failed\n");
202 "Configuration should be updated now. You will have to\n"
203 "power cycle the part in order to finish the conversion.\n"
207 spi_release_bus(slave);
208 spi_free_slave(slave);