165f6f07bSMike Frysinger /* 265f6f07bSMike Frysinger * atmel_df_pow2.c - convert Atmel Dataflashes to Power of 2 mode 365f6f07bSMike Frysinger * 465f6f07bSMike Frysinger * Copyright 2009 Analog Devices Inc. 565f6f07bSMike Frysinger * 665f6f07bSMike Frysinger * Licensed under the 2-clause BSD. 765f6f07bSMike Frysinger */ 865f6f07bSMike Frysinger 965f6f07bSMike Frysinger #include <common.h> 1065f6f07bSMike Frysinger #include <exports.h> 1165f6f07bSMike Frysinger 1265f6f07bSMike Frysinger #define CMD_ID 0x9f 1365f6f07bSMike Frysinger #define CMD_STAT 0xd7 1465f6f07bSMike Frysinger #define CMD_CFG 0x3d 1565f6f07bSMike Frysinger 1665f6f07bSMike Frysinger static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len) 1765f6f07bSMike Frysinger { 1865f6f07bSMike Frysinger buf[0] = cmd; 1965f6f07bSMike Frysinger return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END); 2065f6f07bSMike Frysinger } 2165f6f07bSMike Frysinger 2265f6f07bSMike Frysinger static int flash_status(struct spi_slave *slave) 2365f6f07bSMike Frysinger { 2465f6f07bSMike Frysinger uchar buf[2]; 2565f6f07bSMike Frysinger if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf))) 2665f6f07bSMike Frysinger return -1; 2765f6f07bSMike Frysinger return buf[1]; 2865f6f07bSMike Frysinger } 2965f6f07bSMike Frysinger 3065f6f07bSMike Frysinger static int flash_set_pow2(struct spi_slave *slave) 3165f6f07bSMike Frysinger { 3265f6f07bSMike Frysinger int ret; 3365f6f07bSMike Frysinger uchar buf[4]; 3465f6f07bSMike Frysinger 3565f6f07bSMike Frysinger buf[1] = 0x2a; 3665f6f07bSMike Frysinger buf[2] = 0x80; 3765f6f07bSMike Frysinger buf[3] = 0xa6; 3865f6f07bSMike Frysinger 3965f6f07bSMike Frysinger ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf)); 4065f6f07bSMike Frysinger if (ret) 4165f6f07bSMike Frysinger return ret; 4265f6f07bSMike Frysinger 4365f6f07bSMike Frysinger /* wait Tp, or 6 msec */ 4465f6f07bSMike Frysinger udelay(6000); 4565f6f07bSMike Frysinger 4665f6f07bSMike Frysinger ret = flash_status(slave); 4765f6f07bSMike Frysinger if (ret == -1) 4865f6f07bSMike Frysinger return 1; 4965f6f07bSMike Frysinger 5065f6f07bSMike Frysinger return ret & 0x1 ? 0 : 1; 5165f6f07bSMike Frysinger } 5265f6f07bSMike Frysinger 5365f6f07bSMike Frysinger static int flash_check(struct spi_slave *slave) 5465f6f07bSMike Frysinger { 5565f6f07bSMike Frysinger int ret; 5665f6f07bSMike Frysinger uchar buf[4]; 5765f6f07bSMike Frysinger 5865f6f07bSMike Frysinger ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf)); 5965f6f07bSMike Frysinger if (ret) 6065f6f07bSMike Frysinger return ret; 6165f6f07bSMike Frysinger 6265f6f07bSMike Frysinger if (buf[1] != 0x1F) { 6365f6f07bSMike Frysinger printf("atmel flash not found (id[0] = %#x)\n", buf[1]); 6465f6f07bSMike Frysinger return 1; 6565f6f07bSMike Frysinger } 6665f6f07bSMike Frysinger 6765f6f07bSMike Frysinger if ((buf[2] >> 5) != 0x1) { 6865f6f07bSMike Frysinger printf("AT45 flash not found (id[0] = %#x)\n", buf[2]); 6965f6f07bSMike Frysinger return 2; 7065f6f07bSMike Frysinger } 7165f6f07bSMike Frysinger 7265f6f07bSMike Frysinger return 0; 7365f6f07bSMike Frysinger } 7465f6f07bSMike Frysinger 7565f6f07bSMike Frysinger static char *getline(void) 7665f6f07bSMike Frysinger { 7765f6f07bSMike Frysinger static char buffer[100]; 7865f6f07bSMike Frysinger char c; 7965f6f07bSMike Frysinger size_t i; 8065f6f07bSMike Frysinger 8165f6f07bSMike Frysinger i = 0; 8265f6f07bSMike Frysinger while (1) { 8365f6f07bSMike Frysinger buffer[i] = '\0'; 8465f6f07bSMike Frysinger 8565f6f07bSMike Frysinger c = getc(); 8665f6f07bSMike Frysinger 8765f6f07bSMike Frysinger switch (c) { 8865f6f07bSMike Frysinger case '\r': /* Enter/Return key */ 8965f6f07bSMike Frysinger case '\n': 9065f6f07bSMike Frysinger puts("\n"); 9165f6f07bSMike Frysinger return buffer; 9265f6f07bSMike Frysinger 9365f6f07bSMike Frysinger case 0x03: /* ^C - break */ 9465f6f07bSMike Frysinger return NULL; 9565f6f07bSMike Frysinger 9665f6f07bSMike Frysinger case 0x5F: 9765f6f07bSMike Frysinger case 0x08: /* ^H - backspace */ 9865f6f07bSMike Frysinger case 0x7F: /* DEL - backspace */ 9965f6f07bSMike Frysinger if (i) { 10065f6f07bSMike Frysinger puts("\b \b"); 10165f6f07bSMike Frysinger i--; 10265f6f07bSMike Frysinger } 10365f6f07bSMike Frysinger break; 10465f6f07bSMike Frysinger 10565f6f07bSMike Frysinger default: 10665f6f07bSMike Frysinger /* Ignore control characters */ 10765f6f07bSMike Frysinger if (c < 0x20) 10865f6f07bSMike Frysinger break; 10965f6f07bSMike Frysinger /* Queue up all other characters */ 11065f6f07bSMike Frysinger buffer[i++] = c; 11165f6f07bSMike Frysinger printf("%c", c); 11265f6f07bSMike Frysinger break; 11365f6f07bSMike Frysinger } 11465f6f07bSMike Frysinger } 11565f6f07bSMike Frysinger } 11665f6f07bSMike Frysinger 117*54841ab5SWolfgang Denk int atmel_df_pow2(int argc, char * const argv[]) 11865f6f07bSMike Frysinger { 11965f6f07bSMike Frysinger /* Print the ABI version */ 12065f6f07bSMike Frysinger app_startup(argv); 12165f6f07bSMike Frysinger if (XF_VERSION != get_version()) { 12265f6f07bSMike Frysinger printf("Expects ABI version %d\n", XF_VERSION); 12365f6f07bSMike Frysinger printf("Actual U-Boot ABI version %lu\n", get_version()); 12465f6f07bSMike Frysinger printf("Can't run\n\n"); 12565f6f07bSMike Frysinger return 1; 12665f6f07bSMike Frysinger } 12765f6f07bSMike Frysinger 12865f6f07bSMike Frysinger spi_init(); 12965f6f07bSMike Frysinger 13065f6f07bSMike Frysinger while (1) { 13165f6f07bSMike Frysinger struct spi_slave *slave; 13265f6f07bSMike Frysinger char *line, *p; 13365f6f07bSMike Frysinger int bus, cs, status; 13465f6f07bSMike Frysinger 13565f6f07bSMike Frysinger puts("\nenter the [BUS:]CS of the SPI flash: "); 13665f6f07bSMike Frysinger line = getline(); 13765f6f07bSMike Frysinger 13865f6f07bSMike Frysinger /* CTRL+C */ 13965f6f07bSMike Frysinger if (!line) 14065f6f07bSMike Frysinger return 0; 14165f6f07bSMike Frysinger if (line[0] == '\0') 14265f6f07bSMike Frysinger continue; 14365f6f07bSMike Frysinger 14465f6f07bSMike Frysinger bus = cs = simple_strtoul(line, &p, 10); 14565f6f07bSMike Frysinger if (*p) { 14665f6f07bSMike Frysinger if (*p == ':') { 14765f6f07bSMike Frysinger ++p; 14865f6f07bSMike Frysinger cs = simple_strtoul(p, &p, 10); 14965f6f07bSMike Frysinger } 15065f6f07bSMike Frysinger if (*p) { 15165f6f07bSMike Frysinger puts("invalid format, please try again\n"); 15265f6f07bSMike Frysinger continue; 15365f6f07bSMike Frysinger } 15465f6f07bSMike Frysinger } else 15565f6f07bSMike Frysinger bus = 0; 15665f6f07bSMike Frysinger 15765f6f07bSMike Frysinger printf("\ngoing to work with dataflash at %i:%i\n", bus, cs); 15865f6f07bSMike Frysinger 15965f6f07bSMike Frysinger /* use a low speed -- it'll work with all devices, and 16065f6f07bSMike Frysinger * speed here doesn't really matter. 16165f6f07bSMike Frysinger */ 16265f6f07bSMike Frysinger slave = spi_setup_slave(bus, cs, 1000, SPI_MODE_3); 16365f6f07bSMike Frysinger if (!slave) { 16465f6f07bSMike Frysinger puts("unable to setup slave\n"); 16565f6f07bSMike Frysinger continue; 16665f6f07bSMike Frysinger } 16765f6f07bSMike Frysinger 16865f6f07bSMike Frysinger if (spi_claim_bus(slave)) { 16965f6f07bSMike Frysinger spi_free_slave(slave); 17065f6f07bSMike Frysinger continue; 17165f6f07bSMike Frysinger } 17265f6f07bSMike Frysinger 17365f6f07bSMike Frysinger if (flash_check(slave)) { 17465f6f07bSMike Frysinger puts("no flash found\n"); 17565f6f07bSMike Frysinger goto done; 17665f6f07bSMike Frysinger } 17765f6f07bSMike Frysinger 17865f6f07bSMike Frysinger status = flash_status(slave); 17965f6f07bSMike Frysinger if (status == -1) { 18065f6f07bSMike Frysinger puts("unable to read status register\n"); 18165f6f07bSMike Frysinger goto done; 18265f6f07bSMike Frysinger } 18365f6f07bSMike Frysinger if (status & 0x1) { 18465f6f07bSMike Frysinger puts("flash is already in power-of-2 mode!\n"); 18565f6f07bSMike Frysinger goto done; 18665f6f07bSMike Frysinger } 18765f6f07bSMike Frysinger 18865f6f07bSMike Frysinger puts("are you sure you wish to set power-of-2 mode?\n"); 18965f6f07bSMike Frysinger puts("this operation is permanent and irreversible\n"); 19065f6f07bSMike Frysinger printf("enter YES to continue: "); 19165f6f07bSMike Frysinger line = getline(); 19265f6f07bSMike Frysinger if (!line || strcmp(line, "YES")) 19365f6f07bSMike Frysinger goto done; 19465f6f07bSMike Frysinger 19565f6f07bSMike Frysinger if (flash_set_pow2(slave)) { 19665f6f07bSMike Frysinger puts("setting pow2 mode failed\n"); 19765f6f07bSMike Frysinger goto done; 19865f6f07bSMike Frysinger } 19965f6f07bSMike Frysinger 20065f6f07bSMike Frysinger puts( 20165f6f07bSMike Frysinger "Configuration should be updated now. You will have to\n" 20265f6f07bSMike Frysinger "power cycle the part in order to finish the conversion.\n" 20365f6f07bSMike Frysinger ); 20465f6f07bSMike Frysinger 20565f6f07bSMike Frysinger done: 20665f6f07bSMike Frysinger spi_release_bus(slave); 20765f6f07bSMike Frysinger spi_free_slave(slave); 20865f6f07bSMike Frysinger } 20965f6f07bSMike Frysinger } 210