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 | |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 49 | /* |
| 50 | * Device ID = (memory_type << 8) + memory_capacity |
| 51 | */ |
| 52 | #define STM_ID_M25P10 0x2011 |
| 53 | #define STM_ID_M25P16 0x2015 |
| 54 | #define STM_ID_M25P20 0x2012 |
| 55 | #define STM_ID_M25P32 0x2016 |
| 56 | #define STM_ID_M25P40 0x2013 |
| 57 | #define STM_ID_M25P64 0x2017 |
| 58 | #define STM_ID_M25P80 0x2014 |
| 59 | #define STM_ID_M25P128 0x2018 |
David Imhoff | fd1f514 | 2015-05-03 16:57:38 +0200 | [diff] [blame^] | 60 | #define STM_ID_N25Q256 0xba19 |
David Imhoff | 61295b5 | 2015-05-03 16:02:56 +0200 | [diff] [blame] | 61 | #define STM_ID_N25Q064 0xbb17 |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 62 | #define STM_ID_N25Q128 0xbb18 |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 63 | |
| 64 | struct stmicro_spi_flash_params { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 65 | u16 device_id; |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 66 | u8 op_erase; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 67 | u16 page_size; |
| 68 | u16 pages_per_sector; |
| 69 | u16 nr_sectors; |
| 70 | const char *name; |
| 71 | }; |
| 72 | |
| 73 | /* spi_flash needs to be first so upper layers can free() it */ |
| 74 | struct stmicro_spi_flash { |
| 75 | struct spi_flash flash; |
| 76 | const struct stmicro_spi_flash_params *params; |
| 77 | }; |
| 78 | |
| 79 | static inline struct stmicro_spi_flash *to_stmicro_spi_flash(struct spi_flash |
| 80 | *flash) |
| 81 | { |
| 82 | return container_of(flash, struct stmicro_spi_flash, flash); |
| 83 | } |
| 84 | |
| 85 | static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = { |
| 86 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 87 | .device_id = STM_ID_M25P10, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 88 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 89 | .page_size = 256, |
| 90 | .pages_per_sector = 128, |
| 91 | .nr_sectors = 4, |
| 92 | .name = "M25P10", |
| 93 | }, |
| 94 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 95 | .device_id = STM_ID_M25P16, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 96 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 97 | .page_size = 256, |
| 98 | .pages_per_sector = 256, |
| 99 | .nr_sectors = 32, |
| 100 | .name = "M25P16", |
| 101 | }, |
| 102 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 103 | .device_id = STM_ID_M25P20, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 104 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 105 | .page_size = 256, |
| 106 | .pages_per_sector = 256, |
| 107 | .nr_sectors = 4, |
| 108 | .name = "M25P20", |
| 109 | }, |
| 110 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 111 | .device_id = STM_ID_M25P32, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 112 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 113 | .page_size = 256, |
| 114 | .pages_per_sector = 256, |
| 115 | .nr_sectors = 64, |
| 116 | .name = "M25P32", |
| 117 | }, |
| 118 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 119 | .device_id = STM_ID_M25P40, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 120 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 121 | .page_size = 256, |
| 122 | .pages_per_sector = 256, |
| 123 | .nr_sectors = 8, |
| 124 | .name = "M25P40", |
| 125 | }, |
| 126 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 127 | .device_id = STM_ID_M25P64, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 128 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 129 | .page_size = 256, |
| 130 | .pages_per_sector = 256, |
| 131 | .nr_sectors = 128, |
| 132 | .name = "M25P64", |
| 133 | }, |
| 134 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 135 | .device_id = STM_ID_M25P80, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 136 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 137 | .page_size = 256, |
| 138 | .pages_per_sector = 256, |
| 139 | .nr_sectors = 16, |
| 140 | .name = "M25P80", |
| 141 | }, |
| 142 | { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 143 | .device_id = STM_ID_M25P128, |
Scott Radcliffe | f5fcedf | 2014-10-14 15:40:59 -0400 | [diff] [blame] | 144 | .op_erase = CMD_M25PXX_SE, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 145 | .page_size = 256, |
| 146 | .pages_per_sector = 1024, |
| 147 | .nr_sectors = 64, |
| 148 | .name = "M25P128", |
| 149 | }, |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 150 | { |
David Imhoff | 61295b5 | 2015-05-03 16:02:56 +0200 | [diff] [blame] | 151 | .device_id = STM_ID_N25Q064, |
| 152 | .op_erase = CMD_M25PXX_SSE, |
| 153 | .page_size = 256, |
| 154 | .pages_per_sector = 16, |
| 155 | .nr_sectors = 2048, |
| 156 | .name = "N25Q064", |
| 157 | }, |
| 158 | { |
David Imhoff | 7249572 | 2015-05-03 14:06:21 +0200 | [diff] [blame] | 159 | .device_id = STM_ID_N25Q128, |
| 160 | .op_erase = CMD_M25PXX_SSE, |
| 161 | .page_size = 256, |
| 162 | .pages_per_sector = 16, |
| 163 | .nr_sectors = 4096, |
| 164 | .name = "N25Q128", |
| 165 | }, |
| 166 | { |
David Imhoff | fd1f514 | 2015-05-03 16:57:38 +0200 | [diff] [blame^] | 167 | .device_id = STM_ID_N25Q256, |
David Imhoff | a42edc30 | 2015-05-03 16:05:41 +0200 | [diff] [blame] | 168 | .op_erase = CMD_M25PXX_SSE, |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 169 | .page_size = 256, |
David Imhoff | a42edc30 | 2015-05-03 16:05:41 +0200 | [diff] [blame] | 170 | .pages_per_sector = 16, |
| 171 | .nr_sectors = 8192, |
David Imhoff | fd1f514 | 2015-05-03 16:57:38 +0200 | [diff] [blame^] | 172 | .name = "N25Q256", |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 173 | }, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static int stmicro_write(struct spi_flash *flash, |
| 177 | u32 offset, size_t len, const void *buf) |
| 178 | { |
| 179 | struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 180 | unsigned long byte_addr; |
| 181 | unsigned long page_size; |
| 182 | size_t chunk_len; |
| 183 | size_t actual; |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 184 | int ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 185 | u8 cmd[4]; |
| 186 | |
Kyösti Mälkki | 77d1280 | 2014-06-29 16:15:39 +0300 | [diff] [blame] | 187 | page_size = stm->params->page_size; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 188 | byte_addr = offset % page_size; |
| 189 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 190 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 191 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 192 | for (actual = 0; actual < len; actual += chunk_len) { |
| 193 | chunk_len = min(len - actual, page_size - byte_addr); |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 194 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 195 | |
| 196 | cmd[0] = CMD_M25PXX_PP; |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 197 | cmd[1] = (offset >> 16) & 0xff; |
| 198 | cmd[2] = (offset >> 8) & 0xff; |
| 199 | cmd[3] = offset & 0xff; |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 200 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 8ea5a34 | 2012-05-14 13:52:32 -0700 | [diff] [blame] | 201 | printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" |
| 202 | " chunk_len = %zu\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 203 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 204 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 205 | |
| 206 | ret = spi_flash_cmd(flash->spi, CMD_M25PXX_WREN, NULL, 0); |
| 207 | if (ret < 0) { |
| 208 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 209 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 212 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 213 | buf + actual, chunk_len); |
| 214 | if (ret < 0) { |
| 215 | printk(BIOS_WARNING, "SF: STMicro Page Program failed\n"); |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 216 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
| 220 | if (ret) |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 221 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 222 | |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 223 | offset += chunk_len; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 224 | byte_addr = 0; |
| 225 | } |
| 226 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 227 | #if CONFIG_DEBUG_SPI_FLASH |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 228 | printk(BIOS_SPEW, "SF: STMicro: Successfully programmed %zu bytes @" |
| 229 | " 0x%lx\n", len, (unsigned long)(offset - len)); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 230 | #endif |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 231 | ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 232 | |
Kyösti Mälkki | 1cf8577 | 2013-08-18 12:44:24 +0300 | [diff] [blame] | 233 | out: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 234 | spi_release_bus(flash->spi); |
| 235 | return ret; |
| 236 | } |
| 237 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame] | 238 | static struct stmicro_spi_flash stm; |
| 239 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 240 | struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode) |
| 241 | { |
| 242 | const struct stmicro_spi_flash_params *params; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 243 | unsigned int i; |
| 244 | |
| 245 | if (idcode[0] == 0xff) { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 246 | i = spi_flash_cmd(spi, CMD_M25PXX_RES, idcode, 4); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 247 | if (i) |
| 248 | return NULL; |
| 249 | if ((idcode[3] & 0xf0) == 0x10) { |
| 250 | idcode[0] = 0x20; |
| 251 | idcode[1] = 0x20; |
| 252 | idcode[2] = idcode[3] + 1; |
| 253 | } else |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) { |
| 258 | params = &stmicro_spi_flash_table[i]; |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 259 | if (params->device_id == (idcode[1] << 8 | idcode[2])) { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (i == ARRAY_SIZE(stmicro_spi_flash_table)) { |
Daisuke Nojiri | f0d038f | 2015-02-17 13:35:20 -0800 | [diff] [blame] | 265 | printk(BIOS_WARNING, "SF: Unsupported STMicro ID %02x%02x\n", |
| 266 | idcode[1], idcode[2]); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
| 269 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame] | 270 | stm.params = params; |
| 271 | stm.flash.spi = spi; |
| 272 | stm.flash.name = params->name; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 273 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame] | 274 | stm.flash.write = stmicro_write; |
| 275 | stm.flash.erase = spi_flash_cmd_erase; |
| 276 | stm.flash.read = spi_flash_cmd_read_fast; |
| 277 | stm.flash.sector_size = params->page_size * params->pages_per_sector; |
| 278 | stm.flash.size = stm.flash.sector_size * params->nr_sectors; |
| 279 | stm.flash.erase_cmd = params->op_erase; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 280 | |
Vadim Bendebury | e5fd1c9 | 2015-02-11 19:13:22 -0800 | [diff] [blame] | 281 | return &stm.flash; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 282 | } |