Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * Copyright 2008, Network Appliance Inc. |
| 6 | * Jason McMullan <mcmullan@netapp.com> |
| 7 | * |
| 8 | * Copyright (C) 2004-2007 Freescale Semiconductor, Inc. |
| 9 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Paul Menzel | a8ae1c6 | 2013-02-20 13:21:20 +0100 | [diff] [blame] | 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <spi_flash.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 32 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 33 | #include "spi_flash_internal.h" |
| 34 | |
| 35 | /* M25Pxx-specific commands */ |
| 36 | #define CMD_M25PXX_WREN 0x06 /* Write Enable */ |
| 37 | #define CMD_M25PXX_WRDI 0x04 /* Write Disable */ |
| 38 | #define CMD_M25PXX_RDSR 0x05 /* Read Status Register */ |
| 39 | #define CMD_M25PXX_WRSR 0x01 /* Write Status Register */ |
| 40 | #define CMD_M25PXX_READ 0x03 /* Read Data Bytes */ |
| 41 | #define CMD_M25PXX_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 42 | #define CMD_M25PXX_PP 0x02 /* Page Program */ |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 43 | #define CMD_M25PXX_SSE 0x20 /* Subsector Erase */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 44 | #define CMD_M25PXX_SE 0xd8 /* Sector Erase */ |
| 45 | #define CMD_M25PXX_BE 0xc7 /* Bulk Erase */ |
| 46 | #define CMD_M25PXX_DP 0xb9 /* Deep Power-down */ |
| 47 | #define CMD_M25PXX_RES 0xab /* Release from DP, and Read Signature */ |
| 48 | |
| 49 | #define STM_ID_M25P10 0x11 |
| 50 | #define STM_ID_M25P16 0x15 |
| 51 | #define STM_ID_M25P20 0x12 |
| 52 | #define STM_ID_M25P32 0x16 |
| 53 | #define STM_ID_M25P40 0x13 |
| 54 | #define STM_ID_M25P64 0x17 |
| 55 | #define STM_ID_M25P80 0x14 |
| 56 | #define STM_ID_M25P128 0x18 |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 57 | #define STM_ID_USE_ALT_ID 0xFF |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 58 | |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 59 | /* Some SPI flash share the same .idcode1 (idcode[2]). To handle this without |
| 60 | * (possibly) breaking existing implementations, add the new device at the top |
| 61 | * of the flash table array and set its .idcode1 = STM_ID_USE_ALT_ID. The .id |
| 62 | * is then (idcode[1] << 8 | idcode[2]). |
| 63 | */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 64 | struct stmicro_spi_flash_params { |
| 65 | u8 idcode1; |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 66 | u8 op_erase; |
| 67 | u16 id; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 68 | u16 page_size; |
| 69 | u16 pages_per_sector; |
| 70 | u16 nr_sectors; |
| 71 | const char *name; |
| 72 | }; |
| 73 | |
| 74 | /* spi_flash needs to be first so upper layers can free() it */ |
| 75 | struct stmicro_spi_flash { |
| 76 | struct spi_flash flash; |
| 77 | const struct stmicro_spi_flash_params *params; |
| 78 | }; |
| 79 | |
| 80 | static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash |
| 81 | *flash) |
| 82 | { |
| 83 | return container_of(flash, struct stmicro_spi_flash, flash); |
| 84 | } |
| 85 | |
| 86 | static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = { |
| 87 | { |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 88 | .idcode1 = STM_ID_USE_ALT_ID, |
| 89 | .id = 0xbb18, |
| 90 | .op_erase = CMD_M25PXX_SSE, |
| 91 | .page_size = 256, |
| 92 | .pages_per_sector = 16, |
| 93 | .nr_sectors = 4096, |
| 94 | .name = "N25Q128", |
| 95 | }, |
| 96 | { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 97 | .idcode1 = STM_ID_M25P10, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 98 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 99 | .page_size = 256, |
| 100 | .pages_per_sector = 128, |
| 101 | .nr_sectors = 4, |
| 102 | .name = "M25P10", |
| 103 | }, |
| 104 | { |
| 105 | .idcode1 = STM_ID_M25P16, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 106 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 107 | .page_size = 256, |
| 108 | .pages_per_sector = 256, |
| 109 | .nr_sectors = 32, |
| 110 | .name = "M25P16", |
| 111 | }, |
| 112 | { |
| 113 | .idcode1 = STM_ID_M25P20, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 114 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 115 | .page_size = 256, |
| 116 | .pages_per_sector = 256, |
| 117 | .nr_sectors = 4, |
| 118 | .name = "M25P20", |
| 119 | }, |
| 120 | { |
| 121 | .idcode1 = STM_ID_M25P32, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 122 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 123 | .page_size = 256, |
| 124 | .pages_per_sector = 256, |
| 125 | .nr_sectors = 64, |
| 126 | .name = "M25P32", |
| 127 | }, |
| 128 | { |
| 129 | .idcode1 = STM_ID_M25P40, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 130 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 131 | .page_size = 256, |
| 132 | .pages_per_sector = 256, |
| 133 | .nr_sectors = 8, |
| 134 | .name = "M25P40", |
| 135 | }, |
| 136 | { |
| 137 | .idcode1 = STM_ID_M25P64, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 138 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 139 | .page_size = 256, |
| 140 | .pages_per_sector = 256, |
| 141 | .nr_sectors = 128, |
| 142 | .name = "M25P64", |
| 143 | }, |
| 144 | { |
| 145 | .idcode1 = STM_ID_M25P80, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 146 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 147 | .page_size = 256, |
| 148 | .pages_per_sector = 256, |
| 149 | .nr_sectors = 16, |
| 150 | .name = "M25P80", |
| 151 | }, |
| 152 | { |
| 153 | .idcode1 = STM_ID_M25P128, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 154 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 155 | .page_size = 256, |
| 156 | .pages_per_sector = 1024, |
| 157 | .nr_sectors = 64, |
| 158 | .name = "M25P128", |
| 159 | }, |
| 160 | }; |
| 161 | |
| 162 | static int stmicro_write(struct spi_flash *flash, |
| 163 | u32 offset, size_t len, const void *buf) |
| 164 | { |
| 165 | struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 166 | unsigned long byte_addr; |
| 167 | unsigned long page_size; |
| 168 | size_t chunk_len; |
| 169 | size_t actual; |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 170 | int ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 171 | u8 cmd[4]; |
| 172 | |
Kyösti Mälkki | 77d1280 | 2014-06-29 16:15:39 +0300 | [diff] [blame] | 173 | page_size = stm->params->page_size; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 174 | byte_addr = offset % page_size; |
| 175 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 176 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 177 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 178 | for (actual = 0; actual < len; actual += chunk_len) { |
| 179 | chunk_len = min(len - actual, page_size - byte_addr); |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 180 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 181 | |
| 182 | cmd[0] = CMD_M25PXX_PP; |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 183 | cmd[1] = (offset >> 16) & 0xff; |
| 184 | cmd[2] = (offset >> 8) & 0xff; |
| 185 | cmd[3] = offset & 0xff; |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 186 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 8ea5a34 | 2012-05-14 13:52:32 -0700 | [diff] [blame] | 187 | printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
| 188 | " chunk_len = %zu\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 189 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 190 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 191 | |
| 192 | ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0); |
| 193 | if (ret < 0) { |
| 194 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 195 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 198 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 199 | buf + actual, chunk_len); |
| 200 | if (ret < 0) { |
| 201 | printk(BIOS_WARNING, "SF: STMicro Page Program failed\n"); |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 202 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 206 | if (ret) |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 207 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 208 | |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 209 | offset += chunk_len; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 210 | byte_addr = 0; |
| 211 | } |
| 212 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 213 | #if CONFIG_DEBUG_SPI_FLASH |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 214 | printk(BIOS_SPEW, "SF: STMicro: Successfully programmed %zu bytes @" |
| 215 | " 0x%lx\n", len, (unsigned long)(offset - len)); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 216 | #endif |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 217 | ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 218 | |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 219 | out: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 220 | spi_release_bus(flash->spi); |
| 221 | return ret; |
| 222 | } |
| 223 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame^] | 224 | static struct stmicro_spi_flash stm; |
| 225 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 226 | struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode) |
| 227 | { |
| 228 | const struct stmicro_spi_flash_params *params; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 229 | unsigned int i; |
| 230 | |
| 231 | if (idcode[0] == 0xff) { |
| 232 | i = spi_flash_cmd(spi, CMD_M25PXX_RES, |
| 233 | idcode, 4); |
| 234 | if (i) |
| 235 | return NULL; |
| 236 | if ((idcode[3] & 0xf0) == 0x10) { |
| 237 | idcode[0] = 0x20; |
| 238 | idcode[1] = 0x20; |
| 239 | idcode[2] = idcode[3] + 1; |
| 240 | } else |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) { |
| 245 | params = &stmicro_spi_flash_table[i]; |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 246 | if (params->idcode1 == STM_ID_USE_ALT_ID) { |
| 247 | if (params->id == ((idcode[1] << 8) | idcode[2])) { |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | else if (params->idcode1 == idcode[2]) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (i == ARRAY_SIZE(stmicro_spi_flash_table)) { |
| 257 | printk(BIOS_WARNING, "SF: Unsupported STMicro ID %02x\n", idcode[1]); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame^] | 261 | stm.params = params; |
| 262 | stm.flash.spi = spi; |
| 263 | stm.flash.name = params->name; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 264 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame^] | 265 | stm.flash.write = stmicro_write; |
| 266 | stm.flash.erase = spi_flash_cmd_erase; |
| 267 | stm.flash.read = spi_flash_cmd_read_fast; |
| 268 | stm.flash.sector_size = params->page_size * params->pages_per_sector; |
| 269 | stm.flash.size = stm.flash.sector_size * params->nr_sectors; |
| 270 | stm.flash.erase_cmd = params->op_erase; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 271 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame^] | 272 | return &stm.flash; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 273 | } |