Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SPI flash interface |
| 3 | * |
| 4 | * Copyright (C) 2008 Atmel Corporation |
Mike Frysinger | 22f8b68 | 2009-10-09 17:12:44 -0400 | [diff] [blame] | 5 | * Licensed under the GPL-2 or later. |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 6 | */ |
Mike Frysinger | c5431ac | 2009-03-23 23:03:58 -0400 | [diff] [blame] | 7 | |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <spi.h> |
| 11 | #include <spi_flash.h> |
| 12 | |
| 13 | #include "spi_flash_internal.h" |
| 14 | |
| 15 | int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len) |
| 16 | { |
| 17 | unsigned long flags = SPI_XFER_BEGIN; |
| 18 | int ret; |
| 19 | |
| 20 | if (len == 0) |
| 21 | flags |= SPI_XFER_END; |
| 22 | |
| 23 | ret = spi_xfer(spi, 8, &cmd, NULL, flags); |
| 24 | if (ret) { |
| 25 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); |
| 26 | return ret; |
| 27 | } |
| 28 | |
| 29 | if (len) { |
| 30 | ret = spi_xfer(spi, len * 8, NULL, response, SPI_XFER_END); |
| 31 | if (ret) |
| 32 | debug("SF: Failed to read response (%zu bytes): %d\n", |
| 33 | len, ret); |
| 34 | } |
| 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | int spi_flash_cmd_read(struct spi_slave *spi, const u8 *cmd, |
| 40 | size_t cmd_len, void *data, size_t data_len) |
| 41 | { |
| 42 | unsigned long flags = SPI_XFER_BEGIN; |
| 43 | int ret; |
| 44 | |
| 45 | if (data_len == 0) |
| 46 | flags |= SPI_XFER_END; |
| 47 | |
| 48 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
| 49 | if (ret) { |
| 50 | debug("SF: Failed to send read command (%zu bytes): %d\n", |
| 51 | cmd_len, ret); |
| 52 | } else if (data_len != 0) { |
| 53 | ret = spi_xfer(spi, data_len * 8, NULL, data, SPI_XFER_END); |
| 54 | if (ret) |
| 55 | debug("SF: Failed to read %zu bytes of data: %d\n", |
| 56 | data_len, ret); |
| 57 | } |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 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 | unsigned long flags = SPI_XFER_BEGIN; |
| 66 | int ret; |
| 67 | |
| 68 | if (data_len == 0) |
| 69 | flags |= SPI_XFER_END; |
| 70 | |
| 71 | ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags); |
| 72 | if (ret) { |
| 73 | debug("SF: Failed to send read command (%zu bytes): %d\n", |
| 74 | cmd_len, ret); |
| 75 | } else if (data_len != 0) { |
| 76 | ret = spi_xfer(spi, data_len * 8, data, NULL, SPI_XFER_END); |
| 77 | if (ret) |
| 78 | debug("SF: Failed to read %zu bytes of data: %d\n", |
| 79 | data_len, ret); |
| 80 | } |
| 81 | |
| 82 | return ret; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, |
| 87 | size_t cmd_len, void *data, size_t data_len) |
| 88 | { |
| 89 | struct spi_slave *spi = flash->spi; |
| 90 | int ret; |
| 91 | |
| 92 | spi_claim_bus(spi); |
| 93 | ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len); |
| 94 | spi_release_bus(spi); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, |
| 100 | unsigned int max_hz, unsigned int spi_mode) |
| 101 | { |
| 102 | struct spi_slave *spi; |
| 103 | struct spi_flash *flash; |
| 104 | int ret; |
Mike Frysinger | f145ce5 | 2009-03-28 06:41:09 -0400 | [diff] [blame] | 105 | u8 idcode[5]; |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 106 | |
| 107 | spi = spi_setup_slave(bus, cs, max_hz, spi_mode); |
| 108 | if (!spi) { |
Mike Frysinger | 02110b5 | 2010-04-29 00:35:12 -0400 | [diff] [blame] | 109 | printf("SF: Failed to set up slave\n"); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | ret = spi_claim_bus(spi); |
| 114 | if (ret) { |
| 115 | debug("SF: Failed to claim SPI bus: %d\n", ret); |
| 116 | goto err_claim_bus; |
| 117 | } |
| 118 | |
| 119 | /* Read the ID codes */ |
| 120 | ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode)); |
| 121 | if (ret) |
| 122 | goto err_read_id; |
| 123 | |
Mike Frysinger | f145ce5 | 2009-03-28 06:41:09 -0400 | [diff] [blame] | 124 | debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0], |
| 125 | idcode[1], idcode[2], idcode[3], idcode[4]); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 126 | |
| 127 | switch (idcode[0]) { |
| 128 | #ifdef CONFIG_SPI_FLASH_SPANSION |
| 129 | case 0x01: |
| 130 | flash = spi_flash_probe_spansion(spi, idcode); |
| 131 | break; |
| 132 | #endif |
| 133 | #ifdef CONFIG_SPI_FLASH_ATMEL |
| 134 | case 0x1F: |
| 135 | flash = spi_flash_probe_atmel(spi, idcode); |
| 136 | break; |
| 137 | #endif |
Prafulla Wadaskar | 0956907 | 2009-04-06 21:24:43 +0530 | [diff] [blame] | 138 | #ifdef CONFIG_SPI_FLASH_MACRONIX |
| 139 | case 0xc2: |
| 140 | flash = spi_flash_probe_macronix(spi, idcode); |
| 141 | break; |
| 142 | #endif |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 143 | #ifdef CONFIG_SPI_FLASH_WINBOND |
| 144 | case 0xef: |
| 145 | flash = spi_flash_probe_winbond(spi, idcode); |
| 146 | break; |
| 147 | #endif |
TsiChung Liew | 8dd593a | 2008-08-06 16:08:41 -0500 | [diff] [blame] | 148 | #ifdef CONFIG_SPI_FLASH_STMICRO |
| 149 | case 0x20: |
Thomas Chou | 51bc6bb | 2010-04-30 21:11:20 +0800 | [diff] [blame] | 150 | case 0xff: /* Let the stmicro func handle non-JEDEC ids */ |
TsiChung Liew | 8dd593a | 2008-08-06 16:08:41 -0500 | [diff] [blame] | 151 | flash = spi_flash_probe_stmicro(spi, idcode); |
| 152 | break; |
| 153 | #endif |
Mike Frysinger | f1cb7c8 | 2009-03-27 19:27:58 -0400 | [diff] [blame] | 154 | #ifdef CONFIG_SPI_FLASH_SST |
| 155 | case 0xBF: |
| 156 | flash = spi_flash_probe_sst(spi, idcode); |
| 157 | break; |
| 158 | #endif |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 159 | default: |
Mike Frysinger | 02110b5 | 2010-04-29 00:35:12 -0400 | [diff] [blame] | 160 | printf("SF: Unsupported manufacturer %02X\n", idcode[0]); |
Haavard Skinnemoen | 2f5bfb7 | 2008-05-16 11:10:33 +0200 | [diff] [blame] | 161 | flash = NULL; |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | if (!flash) |
| 166 | goto err_manufacturer_probe; |
| 167 | |
| 168 | spi_release_bus(spi); |
| 169 | |
| 170 | return flash; |
| 171 | |
| 172 | err_manufacturer_probe: |
| 173 | err_read_id: |
| 174 | spi_release_bus(spi); |
| 175 | err_claim_bus: |
| 176 | spi_free_slave(spi); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | void spi_flash_free(struct spi_flash *flash) |
| 181 | { |
| 182 | spi_free_slave(flash->spi); |
| 183 | free(flash); |
| 184 | } |