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