Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Martin Roth | effaf8f | 2019-10-20 20:29:22 -0600 | [diff] [blame] | 2 | |
| 3 | /* |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 4 | * Driver for SST serial flashes |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 7 | #include <console/console.h> |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 8 | #include <commonlib/helpers.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 9 | #include <spi_flash.h> |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 10 | #include <spi-generic.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 11 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 12 | #include "spi_flash_internal.h" |
| 13 | |
| 14 | #define CMD_SST_WREN 0x06 /* Write Enable */ |
| 15 | #define CMD_SST_WRDI 0x04 /* Write Disable */ |
| 16 | #define CMD_SST_RDSR 0x05 /* Read Status Register */ |
| 17 | #define CMD_SST_WRSR 0x01 /* Write Status Register */ |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 18 | #define CMD_SST_EWSR 0x50 /* Enable Write Status Register */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 19 | #define CMD_SST_READ 0x03 /* Read Data Bytes */ |
| 20 | #define CMD_SST_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 21 | #define CMD_SST_BP 0x02 /* Byte Program */ |
Aaron Durbin | 8b1cdd5 | 2019-12-27 13:48:14 -0700 | [diff] [blame] | 22 | #define CMD_SST_PP 0x02 /* Page Program */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 23 | #define CMD_SST_AAI_WP 0xAD /* Auto Address Increment Word Program */ |
| 24 | #define CMD_SST_SE 0x20 /* Sector Erase */ |
| 25 | |
| 26 | #define SST_SR_WIP (1 << 0) /* Write-in-Progress */ |
| 27 | #define SST_SR_WEL (1 << 1) /* Write enable */ |
| 28 | #define SST_SR_BP0 (1 << 2) /* Block Protection 0 */ |
| 29 | #define SST_SR_BP1 (1 << 3) /* Block Protection 1 */ |
| 30 | #define SST_SR_BP2 (1 << 4) /* Block Protection 2 */ |
| 31 | #define SST_SR_AAI (1 << 6) /* Addressing mode */ |
| 32 | #define SST_SR_BPL (1 << 7) /* BP bits lock */ |
| 33 | |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 34 | static const struct spi_flash_part_id flash_table_ai[] = { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 35 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 36 | /* SST25VF040B */ |
| 37 | .id[0] = 0x8d, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 38 | .nr_sectors_shift = 7, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 39 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 40 | /* SST25VF080B */ |
| 41 | .id[0] = 0x8e, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 42 | .nr_sectors_shift = 8, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 43 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 44 | /* SST25VF080 */ |
| 45 | .id[0] = 0x80, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 46 | .nr_sectors_shift = 8, |
Mike Banon | 90af720 | 2019-01-15 03:03:16 +0300 | [diff] [blame] | 47 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 48 | /* SST25VF016B */ |
| 49 | .id[0] = 0x41, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 50 | .nr_sectors_shift = 9, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 51 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 52 | /* SST25VF032B */ |
| 53 | .id[0] = 0x4a, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 54 | .nr_sectors_shift = 10, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 55 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 56 | /* SST25WF512 */ |
| 57 | .id[0] = 0x01, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 58 | .nr_sectors_shift = 4, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 59 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 60 | /* SST25WF010 */ |
| 61 | .id[0] = 0x02, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 62 | .nr_sectors_shift = 5, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 63 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 64 | /* SST25WF020 */ |
| 65 | .id[0] = 0x03, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 66 | .nr_sectors_shift = 6, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 67 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 68 | /* SST25WF040 */ |
| 69 | .id[0] = 0x04, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 70 | .nr_sectors_shift = 7, |
Mike Banon | 90af720 | 2019-01-15 03:03:16 +0300 | [diff] [blame] | 71 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 72 | /* SST25WF080 */ |
| 73 | .id[0] = 0x05, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 74 | .nr_sectors_shift = 8, |
Mike Banon | 90af720 | 2019-01-15 03:03:16 +0300 | [diff] [blame] | 75 | },{ |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 76 | /* SST25WF080B */ |
| 77 | .id[0] = 0x14, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 78 | .nr_sectors_shift = 8, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 79 | }, |
| 80 | }; |
| 81 | |
| 82 | static const struct spi_flash_part_id flash_table_pp256[] = { |
| 83 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 84 | /* SST25VF064C */ |
| 85 | .id[0] = 0x4b, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 86 | .nr_sectors_shift = 11, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 87 | }, |
| 88 | }; |
| 89 | |
| 90 | static int |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 91 | sst_enable_writing(const struct spi_flash *flash) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 92 | { |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 93 | int ret = spi_flash_cmd(&flash->spi, CMD_SST_WREN, NULL, 0); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 94 | if (ret) |
| 95 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static int |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 100 | sst_enable_writing_status(const struct spi_flash *flash) |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 101 | { |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 102 | int ret = spi_flash_cmd(&flash->spi, CMD_SST_EWSR, NULL, 0); |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 103 | if (ret) |
| 104 | printk(BIOS_WARNING, "SF: Enabling Write Status failed\n"); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static int |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 109 | sst_disable_writing(const struct spi_flash *flash) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 110 | { |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 111 | int ret = spi_flash_cmd(&flash->spi, CMD_SST_WRDI, NULL, 0); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 112 | if (ret) |
| 113 | printk(BIOS_WARNING, "SF: Disabling Write failed\n"); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static int |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 118 | sst_byte_write(const struct spi_flash *flash, u32 offset, const void *buf) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 119 | { |
| 120 | int ret; |
| 121 | u8 cmd[4] = { |
| 122 | CMD_SST_BP, |
| 123 | offset >> 16, |
| 124 | offset >> 8, |
| 125 | offset, |
| 126 | }; |
| 127 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 128 | #if CONFIG(DEBUG_SPI_FLASH) |
Julius Werner | 540a980 | 2019-12-09 13:03:29 -0800 | [diff] [blame] | 129 | printk(BIOS_SPEW, "BP[%02x]: %p => cmd = { 0x%02x 0x%06x }\n", |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 130 | spi_w8r8(&flash->spi, CMD_SST_RDSR), buf, cmd[0], offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 131 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 132 | |
| 133 | ret = sst_enable_writing(flash); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 137 | ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd), buf, 1); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 138 | if (ret) |
| 139 | return ret; |
| 140 | |
Uwe Poeche | 17362be | 2019-07-17 14:27:13 +0200 | [diff] [blame] | 141 | return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT_MS); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 144 | static int sst_write_ai(const struct spi_flash *flash, u32 offset, size_t len, |
| 145 | const void *buf) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 146 | { |
| 147 | size_t actual, cmd_len; |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 148 | int ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 149 | u8 cmd[4]; |
| 150 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 151 | /* If the data is not word aligned, write out leading single byte */ |
| 152 | actual = offset % 2; |
| 153 | if (actual) { |
| 154 | ret = sst_byte_write(flash, offset, buf); |
| 155 | if (ret) |
| 156 | goto done; |
| 157 | } |
| 158 | offset += actual; |
| 159 | |
| 160 | ret = sst_enable_writing(flash); |
| 161 | if (ret) |
| 162 | goto done; |
| 163 | |
| 164 | cmd_len = 4; |
| 165 | cmd[0] = CMD_SST_AAI_WP; |
| 166 | cmd[1] = offset >> 16; |
| 167 | cmd[2] = offset >> 8; |
| 168 | cmd[3] = offset; |
| 169 | |
| 170 | for (; actual < len - 1; actual += 2) { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 171 | #if CONFIG(DEBUG_SPI_FLASH) |
Julius Werner | 540a980 | 2019-12-09 13:03:29 -0800 | [diff] [blame] | 172 | printk(BIOS_SPEW, "WP[%02x]: %p => cmd = { 0x%02x 0x%06x }\n", |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 173 | spi_w8r8(&flash->spi, CMD_SST_RDSR), buf + actual, cmd[0], |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 174 | offset); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 175 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 176 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 177 | ret = spi_flash_cmd_write(&flash->spi, cmd, cmd_len, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 178 | buf + actual, 2); |
| 179 | if (ret) { |
| 180 | printk(BIOS_WARNING, "SF: SST word program failed\n"); |
| 181 | break; |
| 182 | } |
| 183 | |
Uwe Poeche | 17362be | 2019-07-17 14:27:13 +0200 | [diff] [blame] | 184 | ret = spi_flash_cmd_wait_ready(flash, |
| 185 | SPI_FLASH_PROG_TIMEOUT_MS); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 186 | if (ret) |
| 187 | break; |
| 188 | |
| 189 | cmd_len = 1; |
| 190 | offset += 2; |
| 191 | } |
| 192 | |
| 193 | if (!ret) |
| 194 | ret = sst_disable_writing(flash); |
| 195 | |
| 196 | /* If there is a single trailing byte, write it out */ |
| 197 | if (!ret && actual != len) |
| 198 | ret = sst_byte_write(flash, offset, buf + actual); |
| 199 | |
Patrick Georgi | 20959ba | 2012-05-12 23:30:36 +0200 | [diff] [blame] | 200 | done: |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 201 | #if CONFIG(DEBUG_SPI_FLASH) |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 202 | printk(BIOS_SPEW, "SF: SST: program %s %zu bytes @ 0x%lx\n", |
Stefan Reinauer | 8ea5a34 | 2012-05-14 13:52:32 -0700 | [diff] [blame] | 203 | ret ? "failure" : "success", len, (unsigned long)offset - actual); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 204 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 208 | /* Flash powers up read-only, so clear BP# bits */ |
| 209 | static int sst_unlock(const struct spi_flash *flash) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 210 | { |
| 211 | int ret; |
| 212 | u8 cmd, status; |
| 213 | |
Zheng Bao | b8117b0 | 2012-11-27 18:08:53 +0800 | [diff] [blame] | 214 | ret = sst_enable_writing_status(flash); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 215 | if (ret) |
| 216 | return ret; |
| 217 | |
| 218 | cmd = CMD_SST_WRSR; |
| 219 | status = 0; |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 220 | ret = spi_flash_cmd_write(&flash->spi, &cmd, 1, &status, 1); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 221 | if (ret) |
| 222 | printk(BIOS_WARNING, "SF: Unable to set status byte\n"); |
| 223 | |
Furquan Shaikh | 810e2cd | 2016-12-05 20:32:24 -0800 | [diff] [blame] | 224 | printk(BIOS_INFO, "SF: SST: status = %x\n", spi_w8r8(&flash->spi, CMD_SST_RDSR)); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 229 | static const struct spi_flash_ops_descriptor descai = { |
| 230 | .erase_cmd = CMD_SST_SE, |
| 231 | .status_cmd = CMD_SST_RDSR, |
| 232 | .wren_cmd = CMD_SST_WREN, |
| 233 | .ops = { |
| 234 | .read = spi_flash_cmd_read, |
| 235 | .write = sst_write_ai, |
| 236 | .erase = spi_flash_cmd_erase, |
| 237 | .status = spi_flash_cmd_status, |
| 238 | }, |
| 239 | }; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 240 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 241 | const struct spi_flash_vendor_info spi_flash_sst_ai_vi = { |
| 242 | .id = VENDOR_ID_SST, |
| 243 | .sector_size_kib_shift = 2, |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 244 | .match_id_mask[0] = 0xff, |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 245 | .ids = flash_table_ai, |
| 246 | .nr_part_ids = ARRAY_SIZE(flash_table_ai), |
| 247 | .desc = &descai, |
| 248 | .after_probe = sst_unlock, |
| 249 | }; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 250 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 251 | const struct spi_flash_vendor_info spi_flash_sst_vi = { |
| 252 | .id = VENDOR_ID_SST, |
| 253 | .page_size_shift = 8, |
| 254 | .sector_size_kib_shift = 2, |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 255 | .match_id_mask[0] = 0xff, |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 256 | .ids = flash_table_pp256, |
| 257 | .nr_part_ids = ARRAY_SIZE(flash_table_pp256), |
| 258 | .desc = &spi_flash_pp_0x20_sector_desc, |
| 259 | .after_probe = sst_unlock, |
| 260 | }; |