Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik |
| 6 | * |
Mike Frysinger | 22f8b68 | 2009-10-09 17:12:44 -0400 | [diff] [blame] | 7 | * Licensed under the GPL-2 or later. |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 8 | */ |
Mike Frysinger | c5431ac | 2009-03-23 23:03:58 -0400 | [diff] [blame] | 9 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <malloc.h> |
| 12 | #include <spi.h> |
| 13 | #include <spi_flash.h> |
Patrick Sestier | adae983 | 2011-04-15 14:25:25 +0000 | [diff] [blame] | 14 | #include <watchdog.h> |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 15 | |
| 16 | #include "spi_flash_internal.h" |
| 17 | |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 18 | static void spi_flash_addr(u32 addr, u8 *cmd) |
| 19 | { |
| 20 | /* cmd[0] is actual command */ |
| 21 | cmd[1] = addr >> 16; |
| 22 | cmd[2] = addr >> 8; |
| 23 | cmd[3] = addr >> 0; |
| 24 | } |
| 25 | |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 26 | static int spi_flash_read_write(struct spi_slave *spi, |
| 27 | const u8 *cmd, size_t cmd_len, |
| 28 | const u8 *data_out, u8 *data_in, |
| 29 | size_t data_len) |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 30 | { |
| 31 | unsigned long flags = SPI_XFER_BEGIN; |
| 32 | int ret; |
| 33 | |
| 34 | if (data_len == 0) |
| 35 | flags |= SPI_XFER_END; |
| 36 | |
| 37 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
| 38 | if (ret) { |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 39 | debug("SF: Failed to send command (%zu bytes): %d\n", |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 40 | cmd_len, ret); |
| 41 | } else if (data_len != 0) { |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 42 | ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 43 | if (ret) |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 44 | debug("SF: Failed to transfer %zu bytes of data: %d\n", |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 45 | data_len, ret); |
| 46 | } |
| 47 | |
| 48 | return ret; |
| 49 | } |
| 50 | |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 51 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 52 | { |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 53 | return spi_flash_cmd_read(spi, &cmd, 1, response, len); |
| 54 | } |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 55 | |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 56 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 57 | size_t cmd_len, void *data, size_t data_len) |
| 58 | { |
| 59 | return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Mike Frysinger | 5efdb04 | 2011-01-10 02:20:11 -0500 | [diff] [blame] | 62 | int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, |
| 63 | const void *data, size_t data_len) |
| 64 | { |
| 65 | return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); |
| 66 | } |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 67 | |
Mike Frysinger | 301e9b4 | 2011-04-25 06:58:29 +0000 | [diff] [blame] | 68 | int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset, |
| 69 | size_t len, const void *buf) |
| 70 | { |
| 71 | unsigned long page_addr, byte_addr, page_size; |
| 72 | size_t chunk_len, actual; |
| 73 | int ret; |
| 74 | u8 cmd[4]; |
| 75 | |
| 76 | page_size = flash->page_size; |
| 77 | page_addr = offset / page_size; |
| 78 | byte_addr = offset % page_size; |
| 79 | |
| 80 | ret = spi_claim_bus(flash->spi); |
| 81 | if (ret) { |
| 82 | debug("SF: unable to claim SPI bus\n"); |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | cmd[0] = CMD_PAGE_PROGRAM; |
| 87 | for (actual = 0; actual < len; actual += chunk_len) { |
| 88 | chunk_len = min(len - actual, page_size - byte_addr); |
| 89 | |
| 90 | cmd[1] = page_addr >> 8; |
| 91 | cmd[2] = page_addr; |
| 92 | cmd[3] = byte_addr; |
| 93 | |
| 94 | debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", |
| 95 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
| 96 | |
| 97 | ret = spi_flash_cmd_write_enable(flash); |
| 98 | if (ret < 0) { |
| 99 | debug("SF: enabling write failed\n"); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, |
| 104 | buf + actual, chunk_len); |
| 105 | if (ret < 0) { |
| 106 | debug("SF: write failed\n"); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 111 | if (ret) |
| 112 | break; |
| 113 | |
| 114 | page_addr++; |
| 115 | byte_addr = 0; |
| 116 | } |
| 117 | |
| 118 | debug("SF: program %s %zu bytes @ %#x\n", |
| 119 | ret ? "failure" : "success", len, offset); |
| 120 | |
| 121 | spi_release_bus(flash->spi); |
| 122 | return ret; |
| 123 | } |
| 124 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 125 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 126 | size_t cmd_len, void *data, size_t data_len) |
| 127 | { |
| 128 | struct spi_slave *spi = flash->spi; |
| 129 | int ret; |
| 130 | |
| 131 | spi_claim_bus(spi); |
| 132 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 133 | spi_release_bus(spi); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Mike Frysinger | 373e7d6 | 2011-01-10 02:20:14 -0500 | [diff] [blame] | 138 | int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset, |
| 139 | size_t len, void *data) |
| 140 | { |
| 141 | u8 cmd[5]; |
| 142 | |
| 143 | cmd[0] = CMD_READ_ARRAY_FAST; |
| 144 | spi_flash_addr(offset, cmd); |
| 145 | cmd[4] = 0x00; |
| 146 | |
| 147 | return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len); |
| 148 | } |
| 149 | |
Mike Frysinger | 37e13bc | 2011-01-10 02:20:12 -0500 | [diff] [blame] | 150 | int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout, |
| 151 | u8 cmd, u8 poll_bit) |
| 152 | { |
| 153 | struct spi_slave *spi = flash->spi; |
| 154 | unsigned long timebase; |
| 155 | int ret; |
| 156 | u8 status; |
| 157 | |
| 158 | ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN); |
| 159 | if (ret) { |
| 160 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | timebase = get_timer(0); |
| 165 | do { |
Patrick Sestier | adae983 | 2011-04-15 14:25:25 +0000 | [diff] [blame] | 166 | WATCHDOG_RESET(); |
| 167 | |
Mike Frysinger | 37e13bc | 2011-01-10 02:20:12 -0500 | [diff] [blame] | 168 | ret = spi_xfer(spi, 8, NULL, &status, 0); |
| 169 | if (ret) |
| 170 | return -1; |
| 171 | |
| 172 | if ((status & poll_bit) == 0) |
| 173 | break; |
| 174 | |
| 175 | } while (get_timer(timebase) < timeout); |
| 176 | |
| 177 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); |
| 178 | |
| 179 | if ((status & poll_bit) == 0) |
| 180 | return 0; |
| 181 | |
| 182 | /* Timed out */ |
| 183 | debug("SF: time out!\n"); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout) |
| 188 | { |
| 189 | return spi_flash_cmd_poll_bit(flash, timeout, |
| 190 | CMD_READ_STATUS, STATUS_WIP); |
| 191 | } |
| 192 | |
Mike Frysinger | 4147798 | 2012-03-04 22:35:50 -0500 | [diff] [blame] | 193 | int spi_flash_cmd_erase(struct spi_flash *flash, u32 offset, size_t len) |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 194 | { |
Richard Retanubun | b0148dc | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 195 | u32 start, end, erase_size; |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 196 | int ret; |
| 197 | u8 cmd[4]; |
| 198 | |
Richard Retanubun | b0148dc | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 199 | erase_size = flash->sector_size; |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 200 | if (offset % erase_size || len % erase_size) { |
| 201 | debug("SF: Erase offset/length not multiple of erase size\n"); |
| 202 | return -1; |
| 203 | } |
| 204 | |
| 205 | ret = spi_claim_bus(flash->spi); |
| 206 | if (ret) { |
| 207 | debug("SF: Unable to claim SPI bus\n"); |
| 208 | return ret; |
| 209 | } |
| 210 | |
Mike Frysinger | 4147798 | 2012-03-04 22:35:50 -0500 | [diff] [blame] | 211 | if (erase_size == 4096) |
| 212 | cmd[0] = CMD_ERASE_4K; |
| 213 | else |
| 214 | cmd[0] = CMD_ERASE_64K; |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 215 | start = offset; |
| 216 | end = start + len; |
| 217 | |
| 218 | while (offset < end) { |
| 219 | spi_flash_addr(offset, cmd); |
| 220 | offset += erase_size; |
| 221 | |
| 222 | debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1], |
| 223 | cmd[2], cmd[3], offset); |
| 224 | |
Mike Frysinger | 8ec7f4c | 2011-04-23 23:05:55 +0000 | [diff] [blame] | 225 | ret = spi_flash_cmd_write_enable(flash); |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 226 | if (ret) |
| 227 | goto out; |
| 228 | |
| 229 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0); |
| 230 | if (ret) |
| 231 | goto out; |
| 232 | |
| 233 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); |
| 234 | if (ret) |
| 235 | goto out; |
| 236 | } |
| 237 | |
Vadim Bendebury | 8708e83 | 2011-10-12 11:28:38 +0000 | [diff] [blame] | 238 | debug("SF: Successfully erased %zu bytes @ %#x\n", len, start); |
Mike Frysinger | 53421bb | 2011-01-10 02:20:13 -0500 | [diff] [blame] | 239 | |
| 240 | out: |
| 241 | spi_release_bus(flash->spi); |
| 242 | return ret; |
| 243 | } |
| 244 | |
Mike Frysinger | 6fe6d0d | 2012-03-04 23:18:17 -0500 | [diff] [blame] | 245 | int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr) |
| 246 | { |
| 247 | u8 cmd; |
| 248 | int ret; |
| 249 | |
| 250 | ret = spi_flash_cmd_write_enable(flash); |
| 251 | if (ret < 0) { |
| 252 | debug("SF: enabling write failed\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | cmd = CMD_WRITE_STATUS; |
| 257 | ret = spi_flash_cmd_write(flash->spi, &cmd, 1, &sr, 1); |
| 258 | if (ret) { |
| 259 | debug("SF: fail to write status register\n"); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 264 | if (ret < 0) { |
| 265 | debug("SF: write status register timed out\n"); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 272 | /* |
| 273 | * The following table holds all device probe functions |
| 274 | * |
| 275 | * shift: number of continuation bytes before the ID |
| 276 | * idcode: the expected IDCODE or 0xff for non JEDEC devices |
| 277 | * probe: the function to call |
| 278 | * |
| 279 | * Non JEDEC devices should be ordered in the table such that |
| 280 | * the probe functions with best detection algorithms come first. |
| 281 | * |
| 282 | * Several matching entries are permitted, they will be tried |
| 283 | * in sequence until a probe function returns non NULL. |
| 284 | * |
| 285 | * IDCODE_CONT_LEN may be redefined if a device needs to declare a |
| 286 | * larger "shift" value. IDCODE_PART_LEN generally shouldn't be |
| 287 | * changed. This is the max number of bytes probe functions may |
| 288 | * examine when looking up part-specific identification info. |
| 289 | * |
| 290 | * Probe functions will be given the idcode buffer starting at their |
| 291 | * manu id byte (the "idcode" in the table below). In other words, |
| 292 | * all of the continuation bytes will be skipped (the "shift" below). |
| 293 | */ |
| 294 | #define IDCODE_CONT_LEN 0 |
| 295 | #define IDCODE_PART_LEN 5 |
| 296 | static const struct { |
| 297 | const u8 shift; |
| 298 | const u8 idcode; |
| 299 | struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); |
| 300 | } flashes[] = { |
| 301 | /* Keep it sorted by define name */ |
| 302 | #ifdef CONFIG_SPI_FLASH_ATMEL |
| 303 | { 0, 0x1f, spi_flash_probe_atmel, }, |
| 304 | #endif |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 305 | #ifdef CONFIG_SPI_FLASH_EON |
| 306 | { 0, 0x1c, spi_flash_probe_eon, }, |
| 307 | #endif |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 308 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 309 | { 0, 0xc2, spi_flash_probe_macronix, }, |
| 310 | #endif |
| 311 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 312 | { 0, 0x01, spi_flash_probe_spansion, }, |
| 313 | #endif |
| 314 | #ifdef CONFIG_SPI_FLASH_SST |
| 315 | { 0, 0xbf, spi_flash_probe_sst, }, |
| 316 | #endif |
| 317 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 318 | { 0, 0x20, spi_flash_probe_stmicro, }, |
| 319 | #endif |
| 320 | #ifdef CONFIG_SPI_FLASH_WINBOND |
| 321 | { 0, 0xef, spi_flash_probe_winbond, }, |
| 322 | #endif |
Reinhard Meyer | 52cb0a7 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 323 | #ifdef CONFIG_SPI_FRAM_RAMTRON |
| 324 | { 6, 0xc2, spi_fram_probe_ramtron, }, |
| 325 | # undef IDCODE_CONT_LEN |
| 326 | # define IDCODE_CONT_LEN 6 |
| 327 | #endif |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 328 | /* Keep it sorted by best detection */ |
| 329 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 330 | { 0, 0xff, spi_flash_probe_stmicro, }, |
| 331 | #endif |
Reinhard Meyer | 52cb0a7 | 2010-10-05 16:56:40 +0200 | [diff] [blame] | 332 | #ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC |
| 333 | { 0, 0xff, spi_fram_probe_ramtron, }, |
| 334 | #endif |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 335 | }; |
| 336 | #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) |
| 337 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 338 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 339 | unsigned int max_hz, unsigned int spi_mode) |
| 340 | { |
| 341 | struct spi_slave *spi; |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 342 | struct spi_flash *flash = NULL; |
| 343 | int ret, i, shift; |
| 344 | u8 idcode[IDCODE_LEN], *idp; |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 345 | |
| 346 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 347 | if (!spi) { |
Mike Frysinger | 02110b5 | 2010-04-29 00:35:12 -0400 | [diff] [blame] | 348 | printf("SF: Failed to set up slave\n"); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | ret = spi_claim_bus(spi); |
| 353 | if (ret) { |
| 354 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 355 | goto err_claim_bus; |
| 356 | } |
| 357 | |
| 358 | /* Read the ID codes */ |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 359 | ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 360 | if (ret) |
| 361 | goto err_read_id; |
| 362 | |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 363 | #ifdef DEBUG |
| 364 | printf("SF: Got idcodes\n"); |
| 365 | print_buffer(0, idcode, 1, sizeof(idcode), 0); |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 366 | #endif |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 367 | |
| 368 | /* count the number of continuation bytes */ |
| 369 | for (shift = 0, idp = idcode; |
| 370 | shift < IDCODE_CONT_LEN && *idp == 0x7f; |
| 371 | ++shift, ++idp) |
| 372 | continue; |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 373 | |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 374 | /* search the table for matches in shift and id */ |
| 375 | for (i = 0; i < ARRAY_SIZE(flashes); ++i) |
| 376 | if (flashes[i].shift == shift && flashes[i].idcode == *idp) { |
| 377 | /* we have a match, call probe */ |
| 378 | flash = flashes[i].probe(spi, idp); |
| 379 | if (flash) |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | if (!flash) { |
| 384 | printf("SF: Unsupported manufacturer %02x\n", *idp); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 385 | goto err_manufacturer_probe; |
Reinhard Meyer | cfe1c67 | 2010-10-05 16:56:39 +0200 | [diff] [blame] | 386 | } |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 387 | |
Mike Frysinger | fbb4248 | 2011-04-12 02:09:28 -0400 | [diff] [blame] | 388 | printf("SF: Detected %s with page size ", flash->name); |
| 389 | print_size(flash->sector_size, ", total "); |
Richard Retanubun | b0148dc | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 390 | print_size(flash->size, "\n"); |
| 391 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 392 | spi_release_bus(spi); |
| 393 | |
| 394 | return flash; |
| 395 | |
| 396 | err_manufacturer_probe: |
| 397 | err_read_id: |
| 398 | spi_release_bus(spi); |
| 399 | err_claim_bus: |
| 400 | spi_free_slave(spi); |
| 401 | return NULL; |
| 402 | } |
| 403 | |
Simon Glass | b162a7c | 2013-03-11 06:08:02 +0000 | [diff] [blame^] | 404 | void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi, |
| 405 | const char *name) |
| 406 | { |
| 407 | struct spi_flash *flash; |
| 408 | void *ptr; |
| 409 | |
| 410 | ptr = malloc(size); |
| 411 | if (!ptr) { |
| 412 | debug("SF: Failed to allocate memory\n"); |
| 413 | return NULL; |
| 414 | } |
| 415 | memset(ptr, '\0', size); |
| 416 | flash = (struct spi_flash *)(ptr + offset); |
| 417 | |
| 418 | /* Set up some basic fields - caller will sort out sizes */ |
| 419 | flash->spi = spi; |
| 420 | flash->name = name; |
| 421 | |
| 422 | flash->read = spi_flash_cmd_read_fast; |
| 423 | flash->write = spi_flash_cmd_write_multi; |
| 424 | flash->erase = spi_flash_cmd_erase; |
| 425 | |
| 426 | return flash; |
| 427 | } |
| 428 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 429 | void spi_flash_free(struct spi_flash *flash) |
| 430 | { |
| 431 | spi_free_slave(flash->spi); |
| 432 | free(flash); |
| 433 | } |