Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * atmel_df_pow2.c - convert Atmel Dataflashes to Power of 2 mode |
| 3 | * |
| 4 | * Copyright 2009 Analog Devices Inc. |
| 5 | * |
| 6 | * Licensed under the 2-clause BSD. |
| 7 | */ |
| 8 | |
Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 9 | #include <exports.h> |
Mike Partington | 46f387d | 2010-10-27 10:31:09 +0000 | [diff] [blame] | 10 | #include <spi.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 11 | #include <linux/delay.h> |
Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 12 | |
| 13 | #define CMD_ID 0x9f |
| 14 | #define CMD_STAT 0xd7 |
| 15 | #define CMD_CFG 0x3d |
| 16 | |
| 17 | static int flash_cmd(struct spi_slave *slave, uchar cmd, uchar *buf, int len) |
| 18 | { |
| 19 | buf[0] = cmd; |
| 20 | return spi_xfer(slave, 8 * len, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END); |
| 21 | } |
| 22 | |
| 23 | static int flash_status(struct spi_slave *slave) |
| 24 | { |
| 25 | uchar buf[2]; |
| 26 | if (flash_cmd(slave, CMD_STAT, buf, sizeof(buf))) |
| 27 | return -1; |
| 28 | return buf[1]; |
| 29 | } |
| 30 | |
| 31 | static int flash_set_pow2(struct spi_slave *slave) |
| 32 | { |
| 33 | int ret; |
| 34 | uchar buf[4]; |
| 35 | |
| 36 | buf[1] = 0x2a; |
| 37 | buf[2] = 0x80; |
| 38 | buf[3] = 0xa6; |
| 39 | |
| 40 | ret = flash_cmd(slave, CMD_CFG, buf, sizeof(buf)); |
| 41 | if (ret) |
| 42 | return ret; |
| 43 | |
| 44 | /* wait Tp, or 6 msec */ |
| 45 | udelay(6000); |
| 46 | |
| 47 | ret = flash_status(slave); |
| 48 | if (ret == -1) |
| 49 | return 1; |
| 50 | |
| 51 | return ret & 0x1 ? 0 : 1; |
| 52 | } |
| 53 | |
| 54 | static int flash_check(struct spi_slave *slave) |
| 55 | { |
| 56 | int ret; |
| 57 | uchar buf[4]; |
| 58 | |
| 59 | ret = flash_cmd(slave, CMD_ID, buf, sizeof(buf)); |
| 60 | if (ret) |
| 61 | return ret; |
| 62 | |
| 63 | if (buf[1] != 0x1F) { |
| 64 | printf("atmel flash not found (id[0] = %#x)\n", buf[1]); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | if ((buf[2] >> 5) != 0x1) { |
| 69 | printf("AT45 flash not found (id[0] = %#x)\n", buf[2]); |
| 70 | return 2; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static char *getline(void) |
| 77 | { |
| 78 | static char buffer[100]; |
| 79 | char c; |
| 80 | size_t i; |
| 81 | |
| 82 | i = 0; |
| 83 | while (1) { |
| 84 | buffer[i] = '\0'; |
| 85 | |
| 86 | c = getc(); |
| 87 | |
| 88 | switch (c) { |
| 89 | case '\r': /* Enter/Return key */ |
| 90 | case '\n': |
| 91 | puts("\n"); |
| 92 | return buffer; |
| 93 | |
| 94 | case 0x03: /* ^C - break */ |
| 95 | return NULL; |
| 96 | |
| 97 | case 0x5F: |
| 98 | case 0x08: /* ^H - backspace */ |
| 99 | case 0x7F: /* DEL - backspace */ |
| 100 | if (i) { |
| 101 | puts("\b \b"); |
| 102 | i--; |
| 103 | } |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | /* Ignore control characters */ |
| 108 | if (c < 0x20) |
| 109 | break; |
| 110 | /* Queue up all other characters */ |
| 111 | buffer[i++] = c; |
| 112 | printf("%c", c); |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 118 | int atmel_df_pow2(int argc, char *const argv[]) |
Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 119 | { |
| 120 | /* Print the ABI version */ |
| 121 | app_startup(argv); |
| 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"); |
| 126 | return 1; |
| 127 | } |
| 128 | |
Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 129 | while (1) { |
| 130 | struct spi_slave *slave; |
| 131 | char *line, *p; |
| 132 | int bus, cs, status; |
| 133 | |
| 134 | puts("\nenter the [BUS:]CS of the SPI flash: "); |
| 135 | line = getline(); |
| 136 | |
| 137 | /* CTRL+C */ |
| 138 | if (!line) |
| 139 | return 0; |
| 140 | if (line[0] == '\0') |
| 141 | continue; |
| 142 | |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 143 | bus = dectoul(line, &p); |
| 144 | cs = bus; |
Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 145 | if (*p) { |
| 146 | if (*p == ':') { |
| 147 | ++p; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 148 | cs = dectoul(p, &p); |
Mike Frysinger | 0f4964a | 2009-07-23 16:37:03 -0400 | [diff] [blame] | 149 | } |
| 150 | if (*p) { |
| 151 | puts("invalid format, please try again\n"); |
| 152 | continue; |
| 153 | } |
| 154 | } else |
| 155 | bus = 0; |
| 156 | |
| 157 | printf("\ngoing to work with dataflash at %i:%i\n", bus, cs); |
| 158 | |
| 159 | /* use a low speed -- it'll work with all devices, and |
| 160 | * speed here doesn't really matter. |
| 161 | */ |
| 162 | slave = spi_setup_slave(bus, cs, 1000, SPI_MODE_3); |
| 163 | if (!slave) { |
| 164 | puts("unable to setup slave\n"); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | if (spi_claim_bus(slave)) { |
| 169 | spi_free_slave(slave); |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | if (flash_check(slave)) { |
| 174 | puts("no flash found\n"); |
| 175 | goto done; |
| 176 | } |
| 177 | |
| 178 | status = flash_status(slave); |
| 179 | if (status == -1) { |
| 180 | puts("unable to read status register\n"); |
| 181 | goto done; |
| 182 | } |
| 183 | if (status & 0x1) { |
| 184 | puts("flash is already in power-of-2 mode!\n"); |
| 185 | goto done; |
| 186 | } |
| 187 | |
| 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: "); |
| 191 | line = getline(); |
| 192 | if (!line || strcmp(line, "YES")) |
| 193 | goto done; |
| 194 | |
| 195 | if (flash_set_pow2(slave)) { |
| 196 | puts("setting pow2 mode failed\n"); |
| 197 | goto done; |
| 198 | } |
| 199 | |
| 200 | puts( |
| 201 | "Configuration should be updated now. You will have to\n" |
| 202 | "power cycle the part in order to finish the conversion.\n" |
| 203 | ); |
| 204 | |
| 205 | done: |
| 206 | spi_release_bus(slave); |
| 207 | spi_free_slave(slave); |
| 208 | } |
| 209 | } |