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