Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * Author: Mingkai Hu (Mingkai.hu@freescale.com) |
| 5 | * Based on stmicro.c by Wolfgang Denk (wd@denx.de), |
| 6 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com), |
| 7 | * and Jason McMullan (mcmullan@netapp.com) |
| 8 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Paul Menzel | a8ae1c6 | 2013-02-20 13:21:20 +0100 | [diff] [blame] | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 17 | * GNU General Public License for more details. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <spi_flash.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 22 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 23 | #include "spi_flash_internal.h" |
| 24 | |
| 25 | /* S25FLxx-specific commands */ |
| 26 | #define CMD_S25FLXX_READ 0x03 /* Read Data Bytes */ |
| 27 | #define CMD_S25FLXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 28 | #define CMD_S25FLXX_READID 0x90 /* Read Manufacture ID and Device ID */ |
| 29 | #define CMD_S25FLXX_WREN 0x06 /* Write Enable */ |
| 30 | #define CMD_S25FLXX_WRDI 0x04 /* Write Disable */ |
| 31 | #define CMD_S25FLXX_RDSR 0x05 /* Read Status Register */ |
| 32 | #define CMD_S25FLXX_WRSR 0x01 /* Write Status Register */ |
| 33 | #define CMD_S25FLXX_PP 0x02 /* Page Program */ |
| 34 | #define CMD_S25FLXX_SE 0xd8 /* Sector Erase */ |
| 35 | #define CMD_S25FLXX_BE 0xc7 /* Bulk Erase */ |
| 36 | #define CMD_S25FLXX_DP 0xb9 /* Deep Power-down */ |
| 37 | #define CMD_S25FLXX_RES 0xab /* Release from DP, and Read Signature */ |
| 38 | |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 39 | #define SPSN_MANUFACTURER_ID_S25FL116K 0x01 |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 40 | #define SPSN_ID_S25FL008A 0x0213 |
| 41 | #define SPSN_ID_S25FL016A 0x0214 |
| 42 | #define SPSN_ID_S25FL032A 0x0215 |
| 43 | #define SPSN_ID_S25FL064A 0x0216 |
Vadim Bendebury | b152883 | 2014-05-01 15:50:43 -0700 | [diff] [blame] | 44 | #define SPSN_ID_S25FL128S 0x0219 |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 45 | #define SPSN_ID_S25FL128P 0x2018 |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 46 | #define SPSN_ID_S25FL116K 0x4015 |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 47 | #define SPSN_EXT_ID_S25FL128P_256KB 0x0300 |
| 48 | #define SPSN_EXT_ID_S25FL128P_64KB 0x0301 |
| 49 | #define SPSN_EXT_ID_S25FL032P 0x4d00 |
Vadim Bendebury | b152883 | 2014-05-01 15:50:43 -0700 | [diff] [blame] | 50 | #define SPSN_EXT_ID_S25FLXXS_64KB 0x4d01 |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 51 | |
| 52 | struct spansion_spi_flash_params { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 53 | u8 idcode0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 54 | u16 idcode1; |
| 55 | u16 idcode2; |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 56 | int (*identify) (const struct spansion_spi_flash_params *params, |
| 57 | u8 *idcode); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 58 | u16 page_size; |
| 59 | u16 pages_per_sector; |
| 60 | u16 nr_sectors; |
| 61 | const char *name; |
| 62 | }; |
| 63 | |
| 64 | struct spansion_spi_flash { |
| 65 | struct spi_flash flash; |
| 66 | const struct spansion_spi_flash_params *params; |
| 67 | }; |
| 68 | |
| 69 | static inline struct spansion_spi_flash *to_spansion_spi_flash(struct spi_flash |
| 70 | *flash) |
| 71 | { |
| 72 | return container_of(flash, struct spansion_spi_flash, flash); |
| 73 | } |
| 74 | |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 75 | /* |
| 76 | * returns non-zero if the given idcode matches the ID of the chip. this is for |
| 77 | * chips which use 2nd, 3rd, 4th, and 5th byte. |
| 78 | */ |
| 79 | static int identify_2345(const struct spansion_spi_flash_params *params, |
| 80 | u8 *idcode) |
| 81 | { |
| 82 | u16 jedec = idcode[1] << 8 | idcode[2]; |
| 83 | u16 ext_jedec = idcode[3] << 8 | idcode[4]; |
| 84 | return (params->idcode1 == jedec) && (params->idcode2 == ext_jedec); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * returns non-zero if the given idcode matches the ID of the chip. this is for |
| 89 | * chips which use 1st, 2nd, and 3rd byte. |
| 90 | */ |
| 91 | static int identify_123(const struct spansion_spi_flash_params *params, |
| 92 | u8 *idcode) |
| 93 | { |
| 94 | u16 jedec = idcode[1] << 8 | idcode[2]; |
| 95 | return (params->idcode0 == idcode[0]) && (params->idcode1 == jedec); |
| 96 | } |
| 97 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 98 | static const struct spansion_spi_flash_params spansion_spi_flash_table[] = { |
| 99 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 100 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 101 | .idcode1 = SPSN_ID_S25FL008A, |
| 102 | .idcode2 = 0, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 103 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 104 | .page_size = 256, |
| 105 | .pages_per_sector = 256, |
| 106 | .nr_sectors = 16, |
| 107 | .name = "S25FL008A", |
| 108 | }, |
| 109 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 110 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 111 | .idcode1 = SPSN_ID_S25FL016A, |
| 112 | .idcode2 = 0, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 113 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 114 | .page_size = 256, |
| 115 | .pages_per_sector = 256, |
| 116 | .nr_sectors = 32, |
| 117 | .name = "S25FL016A", |
| 118 | }, |
| 119 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 120 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 121 | .idcode1 = SPSN_ID_S25FL032A, |
| 122 | .idcode2 = 0, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 123 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 124 | .page_size = 256, |
| 125 | .pages_per_sector = 256, |
| 126 | .nr_sectors = 64, |
| 127 | .name = "S25FL032A", |
| 128 | }, |
| 129 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 130 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 131 | .idcode1 = SPSN_ID_S25FL064A, |
| 132 | .idcode2 = 0, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 133 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 134 | .page_size = 256, |
| 135 | .pages_per_sector = 256, |
| 136 | .nr_sectors = 128, |
| 137 | .name = "S25FL064A", |
| 138 | }, |
| 139 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 140 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 141 | .idcode1 = SPSN_ID_S25FL128P, |
| 142 | .idcode2 = SPSN_EXT_ID_S25FL128P_64KB, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 143 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 144 | .page_size = 256, |
| 145 | .pages_per_sector = 256, |
| 146 | .nr_sectors = 256, |
| 147 | .name = "S25FL128P_64K", |
| 148 | }, |
| 149 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 150 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 151 | .idcode1 = SPSN_ID_S25FL128P, |
| 152 | .idcode2 = SPSN_EXT_ID_S25FL128P_256KB, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 153 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 154 | .page_size = 256, |
| 155 | .pages_per_sector = 1024, |
| 156 | .nr_sectors = 64, |
| 157 | .name = "S25FL128P_256K", |
| 158 | }, |
| 159 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 160 | .idcode0 = 0, |
Vadim Bendebury | b152883 | 2014-05-01 15:50:43 -0700 | [diff] [blame] | 161 | .idcode1 = SPSN_ID_S25FL128S, |
| 162 | .idcode2 = SPSN_EXT_ID_S25FLXXS_64KB, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 163 | .identify = identify_2345, |
Vadim Bendebury | b152883 | 2014-05-01 15:50:43 -0700 | [diff] [blame] | 164 | .page_size = 256, |
| 165 | .pages_per_sector = 256, |
| 166 | .nr_sectors = 512, |
| 167 | .name = "S25FL128S_256K", |
| 168 | }, |
| 169 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 170 | .idcode0 = 0, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 171 | .idcode1 = SPSN_ID_S25FL032A, |
| 172 | .idcode2 = SPSN_EXT_ID_S25FL032P, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 173 | .identify = identify_2345, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 174 | .page_size = 256, |
| 175 | .pages_per_sector = 256, |
| 176 | .nr_sectors = 64, |
| 177 | .name = "S25FL032P", |
| 178 | }, |
Vadim Bendebury | 22fa0e0 | 2014-06-23 09:16:18 -0700 | [diff] [blame] | 179 | { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 180 | .idcode0 = 0, |
Vadim Bendebury | 22fa0e0 | 2014-06-23 09:16:18 -0700 | [diff] [blame] | 181 | .idcode1 = SPSN_ID_S25FL128P, |
| 182 | .idcode2 = SPSN_EXT_ID_S25FLXXS_64KB, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 183 | .identify = identify_2345, |
Vadim Bendebury | 22fa0e0 | 2014-06-23 09:16:18 -0700 | [diff] [blame] | 184 | .page_size = 256, |
| 185 | .pages_per_sector = 256, |
| 186 | .nr_sectors = 256, |
| 187 | .name = "25FS128S", |
| 188 | }, |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 189 | { |
| 190 | .idcode0 = SPSN_MANUFACTURER_ID_S25FL116K, |
| 191 | .idcode1 = SPSN_ID_S25FL116K, |
| 192 | .idcode2 = 0, |
| 193 | .identify = identify_123, |
| 194 | .page_size = 256, |
| 195 | .pages_per_sector = 256, |
| 196 | .nr_sectors = 32, |
| 197 | .name = "S25FL116K_16M", |
| 198 | }, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | static int spansion_write(struct spi_flash *flash, |
| 202 | u32 offset, size_t len, const void *buf) |
| 203 | { |
| 204 | struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash); |
| 205 | unsigned long page_addr; |
| 206 | unsigned long byte_addr; |
| 207 | unsigned long page_size; |
| 208 | size_t chunk_len; |
| 209 | size_t actual; |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 210 | int ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 211 | u8 cmd[4]; |
| 212 | |
| 213 | page_size = spsn->params->page_size; |
| 214 | page_addr = offset / page_size; |
| 215 | byte_addr = offset % page_size; |
| 216 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 217 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 218 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 219 | for (actual = 0; actual < len; actual += chunk_len) { |
| 220 | chunk_len = min(len - actual, page_size - byte_addr); |
| 221 | |
| 222 | cmd[0] = CMD_S25FLXX_PP; |
| 223 | cmd[1] = page_addr >> 8; |
| 224 | cmd[2] = page_addr; |
| 225 | cmd[3] = byte_addr; |
| 226 | |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 227 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 8ea5a34 | 2012-05-14 13:52:32 -0700 | [diff] [blame] | 228 | printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
| 229 | " chunk_len = %zu\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 230 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 231 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 232 | |
| 233 | ret = spi_flash_cmd(flash->spi, CMD_S25FLXX_WREN, NULL, 0); |
| 234 | if (ret < 0) { |
| 235 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, |
| 240 | buf + actual, chunk_len); |
| 241 | if (ret < 0) { |
| 242 | printk(BIOS_WARNING, "SF: SPANSION Page Program failed\n"); |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 247 | if (ret) |
| 248 | break; |
| 249 | |
| 250 | page_addr++; |
| 251 | byte_addr = 0; |
| 252 | } |
| 253 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 254 | #if CONFIG_DEBUG_SPI_FLASH |
| 255 | printk(BIOS_SPEW, "SF: SPANSION: Successfully programmed %zu bytes @ 0x%x\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 256 | len, offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 257 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 258 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 259 | return ret; |
| 260 | } |
| 261 | |
Vadim Bendebury | 8e64388 | 2014-05-01 19:30:46 -0700 | [diff] [blame] | 262 | static struct spansion_spi_flash spsn_flash; |
| 263 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 264 | struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode) |
| 265 | { |
| 266 | const struct spansion_spi_flash_params *params; |
| 267 | struct spansion_spi_flash *spsn; |
| 268 | unsigned int i; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 269 | |
| 270 | for (i = 0; i < ARRAY_SIZE(spansion_spi_flash_table); i++) { |
| 271 | params = &spansion_spi_flash_table[i]; |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 272 | if (params->identify(params, idcode)) |
| 273 | break; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (i == ARRAY_SIZE(spansion_spi_flash_table)) { |
Daisuke Nojiri | 93b9cb7 | 2014-12-10 11:37:05 -0800 | [diff] [blame] | 277 | printk(BIOS_WARNING, |
| 278 | "SF: Unsupported SPANSION ID %02x %02x %02x %02x %02x\n", |
| 279 | idcode[0], idcode[1], idcode[2], idcode[3], idcode[4]); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 280 | return NULL; |
| 281 | } |
| 282 | |
Vadim Bendebury | 8e64388 | 2014-05-01 19:30:46 -0700 | [diff] [blame] | 283 | spsn = &spsn_flash; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 284 | |
| 285 | spsn->params = params; |
| 286 | spsn->flash.spi = spi; |
| 287 | spsn->flash.name = params->name; |
| 288 | |
| 289 | spsn->flash.write = spansion_write; |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 290 | spsn->flash.erase = spi_flash_cmd_erase; |
Vadim Bendebury | 8e64388 | 2014-05-01 19:30:46 -0700 | [diff] [blame] | 291 | spsn->flash.read = spi_flash_cmd_read_slow; |
Duncan Laurie | fb03239 | 2015-01-15 15:28:46 -0800 | [diff] [blame] | 292 | spsn->flash.status = spi_flash_cmd_status; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 293 | spsn->flash.sector_size = params->page_size * params->pages_per_sector; |
| 294 | spsn->flash.size = spsn->flash.sector_size * params->nr_sectors; |
Dan Ehrenberg | a5aac76 | 2015-01-08 10:29:19 -0800 | [diff] [blame] | 295 | spsn->flash.erase_cmd = CMD_S25FLXX_SE; |
Duncan Laurie | fb03239 | 2015-01-15 15:28:46 -0800 | [diff] [blame] | 296 | spsn->flash.status_cmd = CMD_S25FLXX_RDSR; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 297 | |
| 298 | return &spsn->flash; |
| 299 | } |