Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 6 | * |
Mike Frysinger | 4166ee5 | 2009-10-09 17:12:44 -0400 | [diff] [blame] | 7 | * Licensed under the GPL-2 or later. |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 8 | */ |
Mike Frysinger | f773a1b | 2009-03-23 23:03:58 -0400 | [diff] [blame] | 9 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 11 | #include <fdtdec.h> |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <spi.h> |
| 14 | #include <spi_flash.h> |
Patrick Sestier | bd0d19c | 2011-04-15 14:25:25 +0000 | [diff] [blame] | 15 | #include <watchdog.h> |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 16 | |
| 17 | #include "spi_flash_internal.h" |
| 18 | |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Mike Frysinger | e7b44ed | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 21 | static void spi_flash_addr(u32 addr, u8 *cmd) |
| 22 | { |
| 23 | /* cmd[0] is actual command */ |
| 24 | cmd[1] = addr >> 16; |
| 25 | cmd[2] = addr >> 8; |
| 26 | cmd[3] = addr >> 0; |
| 27 | } |
| 28 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 29 | static int spi_flash_read_write(struct spi_slave *spi, |
| 30 | const u8 *cmd, size_t cmd_len, |
| 31 | const u8 *data_out, u8 *data_in, |
| 32 | size_t data_len) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 33 | { |
| 34 | unsigned long flags = SPI_XFER_BEGIN; |
| 35 | int ret; |
| 36 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 37 | if (data_len == 0) |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 38 | flags |= SPI_XFER_END; |
| 39 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 40 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 41 | if (ret) { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 42 | debug("SF: Failed to send command (%zu bytes): %d\n", |
| 43 | cmd_len, ret); |
| 44 | } else if (data_len != 0) { |
| 45 | ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 46 | if (ret) |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 47 | debug("SF: Failed to transfer %zu bytes of data: %d\n", |
| 48 | data_len, ret); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | return ret; |
| 52 | } |
| 53 | |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 54 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 55 | { |
| 56 | return spi_flash_cmd_read(spi, &cmd, 1, response, len); |
| 57 | } |
| 58 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 59 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 60 | size_t cmd_len, void *data, size_t data_len) |
| 61 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 62 | return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 66 | const void *data, size_t data_len) |
| 67 | { |
Mike Frysinger | 000044d | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 68 | return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Jagannadha Sutradharudu Teki | 95e779e | 2013-06-21 19:19:01 +0530 | [diff] [blame^] | 71 | int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 72 | { |
| 73 | struct spi_slave *spi = flash->spi; |
| 74 | unsigned long timebase; |
| 75 | int ret; |
| 76 | u8 status; |
| 77 | u8 check_status = 0x0; |
| 78 | u8 poll_bit = STATUS_WIP; |
| 79 | u8 cmd = flash->poll_cmd; |
| 80 | |
| 81 | if (cmd == CMD_FLAG_STATUS) { |
| 82 | poll_bit = STATUS_PEC; |
| 83 | check_status = poll_bit; |
| 84 | } |
| 85 | |
| 86 | ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); |
| 87 | if (ret) { |
| 88 | debug("SF: fail to read %s status register\n", |
| 89 | cmd == CMD_READ_STATUS ? "read" : "flag"); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | timebase = get_timer(0); |
| 94 | do { |
| 95 | WATCHDOG_RESET(); |
| 96 | |
| 97 | ret = spi_xfer(spi, 8, NULL, &status, 0); |
| 98 | if (ret) |
| 99 | return -1; |
| 100 | |
| 101 | if ((status & poll_bit) == check_status) |
| 102 | break; |
| 103 | |
| 104 | } while (get_timer(timebase) < timeout); |
| 105 | |
| 106 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); |
| 107 | |
| 108 | if ((status & poll_bit) == check_status) |
| 109 | return 0; |
| 110 | |
| 111 | /* Timed out */ |
| 112 | debug("SF: time out!\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 116 | int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd, |
| 117 | size_t cmd_len, const void *buf, size_t buf_len) |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 118 | { |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 119 | struct spi_slave *spi = flash->spi; |
| 120 | unsigned long timeout = SPI_FLASH_PROG_TIMEOUT; |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 121 | int ret; |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 122 | |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 123 | if (buf == NULL) |
| 124 | timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT; |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 125 | |
| 126 | ret = spi_claim_bus(flash->spi); |
| 127 | if (ret) { |
| 128 | debug("SF: unable to claim SPI bus\n"); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 132 | ret = spi_flash_cmd_write_enable(flash); |
| 133 | if (ret < 0) { |
| 134 | debug("SF: enabling write failed\n"); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len); |
| 139 | if (ret < 0) { |
| 140 | debug("SF: write cmd failed\n"); |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | ret = spi_flash_cmd_wait_ready(flash, timeout); |
| 145 | if (ret < 0) { |
| 146 | debug("SF: write %s timed out\n", |
| 147 | timeout == SPI_FLASH_PROG_TIMEOUT ? |
| 148 | "program" : "page erase"); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | spi_release_bus(spi); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
Jagannadha Sutradharudu Teki | 95e779e | 2013-06-21 19:19:01 +0530 | [diff] [blame^] | 157 | int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 158 | { |
| 159 | u32 erase_size; |
| 160 | u8 cmd[4]; |
| 161 | int ret = -1; |
| 162 | |
| 163 | erase_size = flash->sector_size; |
| 164 | if (offset % erase_size || len % erase_size) { |
| 165 | debug("SF: Erase offset/length not multiple of erase size\n"); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | if (erase_size == 4096) |
| 170 | cmd[0] = CMD_ERASE_4K; |
| 171 | else |
| 172 | cmd[0] = CMD_ERASE_64K; |
| 173 | |
| 174 | while (len) { |
| 175 | #ifdef CONFIG_SPI_FLASH_BAR |
| 176 | u8 bank_sel; |
| 177 | |
| 178 | bank_sel = offset / SPI_FLASH_16MB_BOUN; |
| 179 | |
| 180 | ret = spi_flash_cmd_bankaddr_write(flash, bank_sel); |
| 181 | if (ret) { |
| 182 | debug("SF: fail to set bank%d\n", bank_sel); |
| 183 | return ret; |
| 184 | } |
| 185 | #endif |
| 186 | spi_flash_addr(offset, cmd); |
| 187 | |
| 188 | debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
| 189 | cmd[2], cmd[3], offset); |
| 190 | |
| 191 | ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0); |
| 192 | if (ret < 0) { |
| 193 | debug("SF: erase failed\n"); |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | offset += erase_size; |
| 198 | len -= erase_size; |
| 199 | } |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 204 | int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset, |
| 205 | size_t len, const void *buf) |
| 206 | { |
| 207 | unsigned long byte_addr, page_size; |
| 208 | size_t chunk_len, actual; |
| 209 | u8 cmd[4]; |
| 210 | int ret = -1; |
| 211 | |
| 212 | page_size = flash->page_size; |
| 213 | |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 214 | cmd[0] = CMD_PAGE_PROGRAM; |
| 215 | for (actual = 0; actual < len; actual += chunk_len) { |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 216 | #ifdef CONFIG_SPI_FLASH_BAR |
| 217 | u8 bank_sel; |
| 218 | |
Jagannadha Sutradharudu Teki | e3ff9d5 | 2013-05-30 20:24:14 +0530 | [diff] [blame] | 219 | bank_sel = offset / SPI_FLASH_16MB_BOUN; |
| 220 | |
| 221 | ret = spi_flash_cmd_bankaddr_write(flash, bank_sel); |
| 222 | if (ret) { |
| 223 | debug("SF: fail to set bank%d\n", bank_sel); |
| 224 | return ret; |
| 225 | } |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 226 | #endif |
Jagannadha Sutradharudu Teki | e3ff9d5 | 2013-05-30 20:24:14 +0530 | [diff] [blame] | 227 | byte_addr = offset % page_size; |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 228 | chunk_len = min(len - actual, page_size - byte_addr); |
| 229 | |
Simon Glass | 1e566bc | 2013-03-11 06:08:06 +0000 | [diff] [blame] | 230 | if (flash->spi->max_write_size) |
| 231 | chunk_len = min(chunk_len, flash->spi->max_write_size); |
| 232 | |
Jagannadha Sutradharudu Teki | f76b1bd | 2013-06-11 21:36:20 +0530 | [diff] [blame] | 233 | spi_flash_addr(offset, cmd); |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 234 | |
| 235 | debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", |
| 236 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 237 | |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 238 | ret = spi_flash_write_common(flash, cmd, sizeof(cmd), |
| 239 | buf + actual, chunk_len); |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 240 | if (ret < 0) { |
| 241 | debug("SF: write failed\n"); |
| 242 | break; |
| 243 | } |
| 244 | |
Jagannadha Sutradharudu Teki | e3ff9d5 | 2013-05-30 20:24:14 +0530 | [diff] [blame] | 245 | offset += chunk_len; |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mike Frysinger | d4aa500 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 251 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 252 | size_t cmd_len, void *data, size_t data_len) |
| 253 | { |
| 254 | struct spi_slave *spi = flash->spi; |
| 255 | int ret; |
| 256 | |
| 257 | spi_claim_bus(spi); |
| 258 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 259 | spi_release_bus(spi); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
Mike Frysinger | a4c3b40 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 264 | int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, |
| 265 | size_t len, void *data) |
| 266 | { |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 267 | u8 cmd[5], bank_sel = 0; |
Jagannadha Sutradharudu Teki | fc207ee | 2013-05-31 16:00:36 +0530 | [diff] [blame] | 268 | u32 remain_len, read_len; |
| 269 | int ret = -1; |
Mike Frysinger | a4c3b40 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 270 | |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 271 | /* Handle memory-mapped SPI */ |
Jagannadha Sutradharudu Teki | 0d3b596 | 2013-05-27 10:14:14 +0000 | [diff] [blame] | 272 | if (flash->memory_map) { |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 273 | memcpy(data, flash->memory_map + offset, len); |
Jagannadha Sutradharudu Teki | 0d3b596 | 2013-05-27 10:14:14 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | } |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 276 | |
Mike Frysinger | a4c3b40 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 277 | cmd[0] = CMD_READ_ARRAY_FAST; |
Mike Frysinger | a4c3b40 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 278 | cmd[4] = 0x00; |
| 279 | |
Jagannadha Sutradharudu Teki | fc207ee | 2013-05-31 16:00:36 +0530 | [diff] [blame] | 280 | while (len) { |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 281 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | fc207ee | 2013-05-31 16:00:36 +0530 | [diff] [blame] | 282 | bank_sel = offset / SPI_FLASH_16MB_BOUN; |
| 283 | |
| 284 | ret = spi_flash_cmd_bankaddr_write(flash, bank_sel); |
| 285 | if (ret) { |
| 286 | debug("SF: fail to set bank%d\n", bank_sel); |
| 287 | return ret; |
| 288 | } |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 289 | #endif |
Jagannadha Sutradharudu Teki | fc207ee | 2013-05-31 16:00:36 +0530 | [diff] [blame] | 290 | remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1) - offset); |
| 291 | if (len < remain_len) |
| 292 | read_len = len; |
| 293 | else |
| 294 | read_len = remain_len; |
| 295 | |
| 296 | spi_flash_addr(offset, cmd); |
| 297 | |
| 298 | ret = spi_flash_read_common(flash, cmd, sizeof(cmd), |
| 299 | data, read_len); |
| 300 | if (ret < 0) { |
| 301 | debug("SF: read failed\n"); |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | offset += read_len; |
| 306 | len -= read_len; |
| 307 | data += read_len; |
| 308 | } |
| 309 | |
| 310 | return ret; |
Mike Frysinger | a4c3b40 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Mike Frysinger | 41e1713 | 2012-03-04 23:18:17 -0500 | [diff] [blame] | 313 | int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr) |
| 314 | { |
| 315 | u8 cmd; |
| 316 | int ret; |
| 317 | |
Mike Frysinger | 41e1713 | 2012-03-04 23:18:17 -0500 | [diff] [blame] | 318 | cmd = CMD_WRITE_STATUS; |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 319 | ret = spi_flash_write_common(flash, &cmd, 1, &sr, 1); |
Mike Frysinger | 41e1713 | 2012-03-04 23:18:17 -0500 | [diff] [blame] | 320 | if (ret < 0) { |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 321 | debug("SF: fail to write status register\n"); |
Mike Frysinger | 41e1713 | 2012-03-04 23:18:17 -0500 | [diff] [blame] | 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 328 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | c9fcb59 | 2013-06-13 20:37:19 +0530 | [diff] [blame] | 329 | int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel) |
| 330 | { |
| 331 | u8 cmd; |
| 332 | int ret; |
| 333 | |
Jagannadha Sutradharudu Teki | e612ddf | 2013-06-19 15:37:09 +0530 | [diff] [blame] | 334 | if (flash->bank_curr == bank_sel) { |
| 335 | debug("SF: not require to enable bank%d\n", bank_sel); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | cmd = flash->bank_write_cmd; |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 340 | ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1); |
Jagannadha Sutradharudu Teki | c9fcb59 | 2013-06-13 20:37:19 +0530 | [diff] [blame] | 341 | if (ret < 0) { |
Jagannadha Sutradharudu Teki | acc2375 | 2013-06-21 19:19:00 +0530 | [diff] [blame] | 342 | debug("SF: fail to write bank register\n"); |
Jagannadha Sutradharudu Teki | c9fcb59 | 2013-06-13 20:37:19 +0530 | [diff] [blame] | 343 | return ret; |
| 344 | } |
Jagannadha Sutradharudu Teki | e612ddf | 2013-06-19 15:37:09 +0530 | [diff] [blame] | 345 | flash->bank_curr = bank_sel; |
Jagannadha Sutradharudu Teki | c9fcb59 | 2013-06-13 20:37:19 +0530 | [diff] [blame] | 346 | |
Jagannadha Sutradharudu Teki | c9fcb59 | 2013-06-13 20:37:19 +0530 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Jagannadha Sutradharudu Teki | cf6b11d | 2013-06-19 15:31:23 +0530 | [diff] [blame] | 350 | int spi_flash_bank_config(struct spi_flash *flash, u8 idcode0) |
| 351 | { |
Jagannadha Sutradharudu Teki | e612ddf | 2013-06-19 15:37:09 +0530 | [diff] [blame] | 352 | u8 cmd; |
| 353 | u8 curr_bank = 0; |
| 354 | |
Jagannadha Sutradharudu Teki | cf6b11d | 2013-06-19 15:31:23 +0530 | [diff] [blame] | 355 | /* discover bank cmds */ |
| 356 | switch (idcode0) { |
| 357 | case SPI_FLASH_SPANSION_IDCODE0: |
| 358 | flash->bank_read_cmd = CMD_BANKADDR_BRRD; |
| 359 | flash->bank_write_cmd = CMD_BANKADDR_BRWR; |
| 360 | break; |
| 361 | case SPI_FLASH_STMICRO_IDCODE0: |
| 362 | case SPI_FLASH_WINBOND_IDCODE0: |
| 363 | flash->bank_read_cmd = CMD_EXTNADDR_RDEAR; |
| 364 | flash->bank_write_cmd = CMD_EXTNADDR_WREAR; |
| 365 | break; |
| 366 | default: |
| 367 | printf("SF: Unsupported bank commands %02x\n", idcode0); |
| 368 | return -1; |
| 369 | } |
| 370 | |
Jagannadha Sutradharudu Teki | e612ddf | 2013-06-19 15:37:09 +0530 | [diff] [blame] | 371 | /* read the bank reg - on which bank the flash is in currently */ |
| 372 | cmd = flash->bank_read_cmd; |
| 373 | if (flash->size > SPI_FLASH_16MB_BOUN) { |
| 374 | if (spi_flash_read_common(flash, &cmd, 1, &curr_bank, 1)) { |
| 375 | debug("SF: fail to read bank addr register\n"); |
| 376 | return -1; |
| 377 | } |
| 378 | flash->bank_curr = curr_bank; |
| 379 | } else { |
| 380 | flash->bank_curr = curr_bank; |
| 381 | } |
| 382 | |
Jagannadha Sutradharudu Teki | cf6b11d | 2013-06-19 15:31:23 +0530 | [diff] [blame] | 383 | return 0; |
| 384 | } |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 385 | #endif |
Jagannadha Sutradharudu Teki | cf6b11d | 2013-06-19 15:31:23 +0530 | [diff] [blame] | 386 | |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 387 | #ifdef CONFIG_OF_CONTROL |
| 388 | int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash) |
| 389 | { |
| 390 | fdt_addr_t addr; |
| 391 | fdt_size_t size; |
| 392 | int node; |
| 393 | |
| 394 | /* If there is no node, do nothing */ |
| 395 | node = fdtdec_next_compatible(blob, 0, COMPAT_GENERIC_SPI_FLASH); |
| 396 | if (node < 0) |
| 397 | return 0; |
| 398 | |
| 399 | addr = fdtdec_get_addr_size(blob, node, "memory-map", &size); |
| 400 | if (addr == FDT_ADDR_T_NONE) { |
| 401 | debug("%s: Cannot decode address\n", __func__); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | if (flash->size != size) { |
| 406 | debug("%s: Memory map must cover entire device\n", __func__); |
| 407 | return -1; |
| 408 | } |
| 409 | flash->memory_map = (void *)addr; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | #endif /* CONFIG_OF_CONTROL */ |
| 414 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 415 | /* |
| 416 | * The following table holds all device probe functions |
| 417 | * |
| 418 | * shift: number of continuation bytes before the ID |
| 419 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 420 | * probe: the function to call |
| 421 | * |
| 422 | * Non JEDEC devices should be ordered in the table such that |
| 423 | * the probe functions with best detection algorithms come first. |
| 424 | * |
| 425 | * Several matching entries are permitted, they will be tried |
| 426 | * in sequence until a probe function returns non NULL. |
| 427 | * |
| 428 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 429 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 430 | * changed. This is the max number of bytes probe functions may |
| 431 | * examine when looking up part-specific identification info. |
| 432 | * |
| 433 | * Probe functions will be given the idcode buffer starting at their |
| 434 | * manu id byte (the "idcode" in the table below). In other words, |
| 435 | * all of the continuation bytes will be skipped (the "shift" below). |
| 436 | */ |
| 437 | #define IDCODE_CONT_LEN 0 |
| 438 | #define IDCODE_PART_LEN 5 |
| 439 | static const struct { |
| 440 | const u8 shift; |
| 441 | const u8 idcode; |
| 442 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 443 | } flashes[] = { |
| 444 | /* Keep it sorted by define name */ |
| 445 | #ifdef CONFIG_SPI_FLASH_ATMEL |
| 446 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 447 | #endif |
Chong Huang | d1d90656 | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 448 | #ifdef CONFIG_SPI_FLASH_EON |
| 449 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 450 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 451 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 452 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 453 | #endif |
| 454 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 455 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 456 | #endif |
| 457 | #ifdef CONFIG_SPI_FLASH_SST |
| 458 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 459 | #endif |
| 460 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 461 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 462 | #endif |
| 463 | #ifdef CONFIG_SPI_FLASH_WINBOND |
| 464 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 465 | #endif |
Reinhard Meyer | e0987e2 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 466 | #ifdef CONFIG_SPI_FRAM_RAMTRON |
| 467 | { 6, 0xc2, spi_fram_probe_ramtron, }, |
| 468 | # undef IDCODE_CONT_LEN |
| 469 | # define IDCODE_CONT_LEN 6 |
| 470 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 471 | /* Keep it sorted by best detection */ |
| 472 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 473 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 474 | #endif |
Reinhard Meyer | e0987e2 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 475 | #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC |
| 476 | { 0, 0xff, spi_fram_probe_ramtron, }, |
| 477 | #endif |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 478 | }; |
| 479 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 480 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 481 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 482 | unsigned int max_hz, unsigned int spi_mode) |
| 483 | { |
| 484 | struct spi_slave *spi; |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 485 | struct spi_flash *flash = NULL; |
| 486 | int ret, i, shift; |
| 487 | u8 idcode[IDCODE_LEN], *idp; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 488 | |
| 489 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 490 | if (!spi) { |
Mike Frysinger | b376bbb | 2010-04-29 00:35:12 -0400 | [diff] [blame] | 491 | printf("SF: Failed to set up slave\n"); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | ret = spi_claim_bus(spi); |
| 496 | if (ret) { |
| 497 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 498 | goto err_claim_bus; |
| 499 | } |
| 500 | |
| 501 | /* Read the ID codes */ |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 502 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 503 | if (ret) |
| 504 | goto err_read_id; |
| 505 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 506 | #ifdef DEBUG |
| 507 | printf("SF: Got idcodes\n"); |
| 508 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
| 509 | #endif |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 510 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 511 | /* count the number of continuation bytes */ |
| 512 | for (shift = 0, idp = idcode; |
| 513 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 514 | ++shift, ++idp) |
| 515 | continue; |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 516 | |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 517 | /* search the table for matches in shift and id */ |
| 518 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 519 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
| 520 | /* we have a match, call probe */ |
| 521 | flash = flashes[i].probe(spi, idp); |
| 522 | if (flash) |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | if (!flash) { |
| 527 | printf("SF: Unsupported manufacturer %02x\n", *idp); |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 528 | goto err_manufacturer_probe; |
Reinhard Meyer | 0d3fe2b | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 529 | } |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 530 | |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 531 | #ifdef CONFIG_SPI_FLASH_BAR |
Jagannadha Sutradharudu Teki | e612ddf | 2013-06-19 15:37:09 +0530 | [diff] [blame] | 532 | /* Configure the BAR - disover bank cmds and read current bank */ |
| 533 | ret = spi_flash_bank_config(flash, *idp); |
| 534 | if (ret < 0) |
| 535 | goto err_manufacturer_probe; |
Jagannadha Sutradharudu Teki | 1dcd6d0 | 2013-06-19 15:33:58 +0530 | [diff] [blame] | 536 | #endif |
Jagannadha Sutradharudu Teki | e612ddf | 2013-06-19 15:37:09 +0530 | [diff] [blame] | 537 | |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 538 | #ifdef CONFIG_OF_CONTROL |
| 539 | if (spi_flash_decode_fdt(gd->fdt_blob, flash)) { |
| 540 | debug("SF: FDT decode error\n"); |
| 541 | goto err_manufacturer_probe; |
| 542 | } |
| 543 | #endif |
Mike Frysinger | 493c360 | 2011-04-12 02:09:28 -0400 | [diff] [blame] | 544 | printf("SF: Detected %s with page size ", flash->name); |
| 545 | print_size(flash->sector_size, ", total "); |
Simon Glass | bb8215f | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 546 | print_size(flash->size, ""); |
| 547 | if (flash->memory_map) |
| 548 | printf(", mapped at %p", flash->memory_map); |
| 549 | puts("\n"); |
Richard Retanubun | 4e6a515 | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 550 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 551 | spi_release_bus(spi); |
| 552 | |
| 553 | return flash; |
| 554 | |
| 555 | err_manufacturer_probe: |
| 556 | err_read_id: |
| 557 | spi_release_bus(spi); |
| 558 | err_claim_bus: |
| 559 | spi_free_slave(spi); |
| 560 | return NULL; |
| 561 | } |
| 562 | |
Simon Glass | b5aec14 | 2013-03-11 06:08:02 +0000 | [diff] [blame] | 563 | void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi, |
| 564 | const char *name) |
| 565 | { |
| 566 | struct spi_flash *flash; |
| 567 | void *ptr; |
| 568 | |
| 569 | ptr = malloc(size); |
| 570 | if (!ptr) { |
| 571 | debug("SF: Failed to allocate memory\n"); |
| 572 | return NULL; |
| 573 | } |
| 574 | memset(ptr, '\0', size); |
| 575 | flash = (struct spi_flash *)(ptr + offset); |
| 576 | |
| 577 | /* Set up some basic fields - caller will sort out sizes */ |
| 578 | flash->spi = spi; |
| 579 | flash->name = name; |
Jagannadha Sutradharudu Teki | 615a156 | 2013-06-21 15:56:30 +0530 | [diff] [blame] | 580 | flash->poll_cmd = CMD_READ_STATUS; |
Simon Glass | b5aec14 | 2013-03-11 06:08:02 +0000 | [diff] [blame] | 581 | |
| 582 | flash->read = spi_flash_cmd_read_fast; |
| 583 | flash->write = spi_flash_cmd_write_multi; |
| 584 | flash->erase = spi_flash_cmd_erase; |
| 585 | |
| 586 | return flash; |
| 587 | } |
| 588 | |
Haavard Skinnemoen | d25ce7d | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 589 | void spi_flash_free(struct spi_flash *flash) |
| 590 | { |
| 591 | spi_free_slave(flash->spi); |
| 592 | free(flash); |
| 593 | } |