Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 3 | #include <console/console.h> |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 5 | #include <spi_flash.h> |
Furquan Shaikh | c28984d | 2016-11-20 21:04:00 -0800 | [diff] [blame] | 6 | #include <spi-generic.h> |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 7 | #include <delay.h> |
| 8 | #include <lib.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 9 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 10 | #include "spi_flash_internal.h" |
Frans Hendriks | 31eac4d | 2019-03-04 15:16:43 +0100 | [diff] [blame] | 11 | #include "spi_winbond.h" |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 12 | |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 13 | union status_reg1 { |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 14 | uint8_t u; |
| 15 | struct { |
| 16 | uint8_t busy : 1; |
| 17 | uint8_t wel : 1; |
| 18 | uint8_t bp : 3; |
| 19 | uint8_t tb : 1; |
| 20 | uint8_t sec : 1; |
| 21 | uint8_t srp0 : 1; |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 22 | } bp3; |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 23 | struct { |
| 24 | uint8_t busy : 1; |
| 25 | uint8_t wel : 1; |
| 26 | uint8_t bp : 4; |
| 27 | uint8_t tb : 1; |
| 28 | uint8_t srp0 : 1; |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 29 | } bp4; |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | union status_reg2 { |
| 33 | uint8_t u; |
| 34 | struct { |
| 35 | uint8_t srp1 : 1; |
| 36 | uint8_t qe : 1; |
| 37 | uint8_t res : 1; |
| 38 | uint8_t lb : 3; |
| 39 | uint8_t cmp : 1; |
| 40 | uint8_t sus : 1; |
| 41 | }; |
| 42 | }; |
| 43 | |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 44 | struct status_regs { |
| 45 | union { |
| 46 | struct { |
| 47 | #if defined(__BIG_ENDIAN) |
| 48 | union status_reg2 reg2; |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 49 | union status_reg1 reg1; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 50 | #else |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 51 | union status_reg1 reg1; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 52 | union status_reg2 reg2; |
| 53 | #endif |
| 54 | }; |
| 55 | u16 u; |
| 56 | }; |
| 57 | }; |
| 58 | |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 59 | static const struct spi_flash_part_id flash_table[] = { |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 60 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 61 | /* W25P80 */ |
| 62 | .id[0] = 0x2014, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 63 | .nr_sectors_shift = 8, |
Mike Banon | 72812fa | 2019-01-08 19:11:34 +0300 | [diff] [blame] | 64 | }, |
| 65 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 66 | /* W25P16 */ |
| 67 | .id[0] = 0x2015, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 68 | .nr_sectors_shift = 9, |
Mike Banon | 72812fa | 2019-01-08 19:11:34 +0300 | [diff] [blame] | 69 | }, |
| 70 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 71 | /* W25P32 */ |
| 72 | .id[0] = 0x2016, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 73 | .nr_sectors_shift = 10, |
Mike Banon | 72812fa | 2019-01-08 19:11:34 +0300 | [diff] [blame] | 74 | }, |
| 75 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 76 | /* W25X80 */ |
| 77 | .id[0] = 0x3014, |
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 | .fast_read_dual_output_support = 1, |
Mike Banon | 72812fa | 2019-01-08 19:11:34 +0300 | [diff] [blame] | 80 | }, |
| 81 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 82 | /* W25X16 */ |
| 83 | .id[0] = 0x3015, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 84 | .nr_sectors_shift = 9, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 85 | .fast_read_dual_output_support = 1, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 86 | }, |
| 87 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 88 | /* W25X32 */ |
| 89 | .id[0] = 0x3016, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 90 | .nr_sectors_shift = 10, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 91 | .fast_read_dual_output_support = 1, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 92 | }, |
| 93 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 94 | /* W25X64 */ |
| 95 | .id[0] = 0x3017, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 96 | .nr_sectors_shift = 11, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 97 | .fast_read_dual_output_support = 1, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 98 | }, |
| 99 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 100 | /* W25Q80_V */ |
| 101 | .id[0] = 0x4014, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 102 | .nr_sectors_shift = 8, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 103 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 104 | .fast_read_dual_io_support = 1, |
Kyösti Mälkki | 76f7b79 | 2018-06-14 22:25:58 +0300 | [diff] [blame] | 105 | }, |
| 106 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 107 | /* W25Q16_V */ |
| 108 | .id[0] = 0x4015, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 109 | .nr_sectors_shift = 9, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 110 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 111 | .fast_read_dual_io_support = 1, |
Mike Banon | 72812fa | 2019-01-08 19:11:34 +0300 | [diff] [blame] | 112 | .protection_granularity_shift = 16, |
| 113 | .bp_bits = 3, |
| 114 | }, |
| 115 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 116 | /* W25Q16DW */ |
| 117 | .id[0] = 0x6015, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 118 | .nr_sectors_shift = 9, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 119 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 120 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 121 | .protection_granularity_shift = 16, |
| 122 | .bp_bits = 3, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 123 | }, |
| 124 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 125 | /* W25Q32_V */ |
| 126 | .id[0] = 0x4016, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 127 | .nr_sectors_shift = 10, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 128 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 129 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 130 | .protection_granularity_shift = 16, |
| 131 | .bp_bits = 3, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 132 | }, |
| 133 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 134 | /* W25Q32DW */ |
| 135 | .id[0] = 0x6016, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 136 | .nr_sectors_shift = 10, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 137 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 138 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 139 | .protection_granularity_shift = 16, |
| 140 | .bp_bits = 3, |
David Hendricks | 43e9252 | 2014-03-21 19:45:02 -0700 | [diff] [blame] | 141 | }, |
| 142 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 143 | /* W25Q64_V */ |
| 144 | .id[0] = 0x4017, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 145 | .nr_sectors_shift = 11, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 146 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 147 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 148 | .protection_granularity_shift = 17, |
| 149 | .bp_bits = 3, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 150 | }, |
| 151 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 152 | /* W25Q64DW */ |
| 153 | .id[0] = 0x6017, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 154 | .nr_sectors_shift = 11, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 155 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 156 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 157 | .protection_granularity_shift = 17, |
| 158 | .bp_bits = 3, |
Aaron Durbin | 16000c8 | 2013-09-19 12:19:51 -0500 | [diff] [blame] | 159 | }, |
| 160 | { |
Scott Chao | e6d1c7f | 2020-04-08 15:43:03 +0800 | [diff] [blame] | 161 | /* W25Q64JW */ |
| 162 | .id[0] = 0x8017, |
| 163 | .nr_sectors_shift = 11, |
| 164 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 165 | .fast_read_dual_io_support = 1, |
Scott Chao | e6d1c7f | 2020-04-08 15:43:03 +0800 | [diff] [blame] | 166 | .protection_granularity_shift = 17, |
| 167 | .bp_bits = 3, |
| 168 | }, |
| 169 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 170 | /* W25Q128_V */ |
| 171 | .id[0] = 0x4018, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 172 | .nr_sectors_shift = 12, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 173 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 174 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 175 | .protection_granularity_shift = 18, |
| 176 | .bp_bits = 3, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 177 | }, |
Mohan D'Costa | bdae9be | 2014-09-25 14:40:44 +0900 | [diff] [blame] | 178 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 179 | /* W25Q128FW */ |
| 180 | .id[0] = 0x6018, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 181 | .nr_sectors_shift = 12, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 182 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 183 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 184 | .protection_granularity_shift = 18, |
| 185 | .bp_bits = 3, |
Mohan D'Costa | bdae9be | 2014-09-25 14:40:44 +0900 | [diff] [blame] | 186 | }, |
Varadarajan Narayanan | 934c683 | 2016-03-21 12:56:43 +0530 | [diff] [blame] | 187 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 188 | /* W25Q128J */ |
| 189 | .id[0] = 0x7018, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 190 | .nr_sectors_shift = 12, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 191 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 192 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | fca506d | 2018-10-01 16:00:16 +0200 | [diff] [blame] | 193 | .protection_granularity_shift = 18, |
| 194 | .bp_bits = 3, |
| 195 | }, |
| 196 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 197 | /* W25Q128JW */ |
| 198 | .id[0] = 0x8018, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 199 | .nr_sectors_shift = 12, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 200 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 201 | .fast_read_dual_io_support = 1, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 202 | .protection_granularity_shift = 18, |
| 203 | .bp_bits = 3, |
Peichao Wang | 0d92271 | 2019-11-12 08:36:53 +0800 | [diff] [blame] | 204 | }, |
| 205 | { |
Shaik Sajida Bhanu | 5ef1a83 | 2021-05-30 23:04:47 +0530 | [diff] [blame] | 206 | /* W25Q512NW-IM */ |
| 207 | .id[0] = 0x8020, |
Shaik Sajida Bhanu | 7ce0236 | 2021-05-30 23:04:47 +0530 | [diff] [blame] | 208 | .nr_sectors_shift = 14, |
Shaik Sajida Bhanu | 5ef1a83 | 2021-05-30 23:04:47 +0530 | [diff] [blame] | 209 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 210 | .fast_read_dual_io_support = 1, |
Shaik Sajida Bhanu | 5ef1a83 | 2021-05-30 23:04:47 +0530 | [diff] [blame] | 211 | .protection_granularity_shift = 16, |
| 212 | .bp_bits = 4, |
| 213 | }, |
| 214 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 215 | /* W25Q256_V */ |
| 216 | .id[0] = 0x4019, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 217 | .nr_sectors_shift = 13, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 218 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 219 | .fast_read_dual_io_support = 1, |
Mike Banon | 72812fa | 2019-01-08 19:11:34 +0300 | [diff] [blame] | 220 | .protection_granularity_shift = 16, |
| 221 | .bp_bits = 4, |
| 222 | }, |
| 223 | { |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 224 | /* W25Q256J */ |
| 225 | .id[0] = 0x7019, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 226 | .nr_sectors_shift = 13, |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 227 | .fast_read_dual_output_support = 1, |
Julius Werner | df50622 | 2021-07-13 15:57:29 -0700 | [diff] [blame] | 228 | .fast_read_dual_io_support = 1, |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 229 | .protection_granularity_shift = 16, |
| 230 | .bp_bits = 4, |
Varadarajan Narayanan | 934c683 | 2016-03-21 12:56:43 +0530 | [diff] [blame] | 231 | }, |
Ritul Guru | 5fcef01 | 2022-05-24 18:46:16 +0530 | [diff] [blame] | 232 | { |
| 233 | /* W25Q256JW */ |
| 234 | .id[0] = 0x6019, |
| 235 | .nr_sectors_shift = 13, |
| 236 | .fast_read_dual_output_support = 1, |
| 237 | .fast_read_dual_io_support = 1, |
| 238 | .protection_granularity_shift = 16, |
| 239 | .bp_bits = 4, |
| 240 | }, |
Fred Reitberger | 6296fba | 2023-06-01 16:05:37 -0400 | [diff] [blame^] | 241 | { |
| 242 | /* W25Q256JW_DTR */ |
| 243 | .id[0] = 0x8019, |
| 244 | .nr_sectors_shift = 13, |
| 245 | .fast_read_dual_output_support = 1, |
| 246 | .fast_read_dual_io_support = 1, |
| 247 | .protection_granularity_shift = 16, |
| 248 | .bp_bits = 4, |
| 249 | }, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Convert BPx, TB and CMP to a region. |
| 254 | * SEC (if available) must be zero. |
| 255 | */ |
| 256 | static void winbond_bpbits_to_region(const size_t granularity, |
| 257 | const u8 bp, |
| 258 | bool tb, |
| 259 | const bool cmp, |
| 260 | const size_t flash_size, |
| 261 | struct region *out) |
| 262 | { |
| 263 | size_t protected_size = |
Elyes HAOUAS | 361a935 | 2019-12-18 21:26:33 +0100 | [diff] [blame] | 264 | MIN(bp ? granularity << (bp - 1) : 0, flash_size); |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 265 | |
| 266 | if (cmp) { |
| 267 | protected_size = flash_size - protected_size; |
| 268 | tb = !tb; |
| 269 | } |
| 270 | |
Patrick Rudolph | e736015 | 2018-12-03 09:41:06 +0100 | [diff] [blame] | 271 | out->offset = tb ? 0 : flash_size - protected_size; |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 272 | out->size = protected_size; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Available on all devices. |
| 277 | * Read block protect bits from Status/Status2 Reg. |
| 278 | * Converts block protection bits to a region. |
| 279 | * |
| 280 | * Returns: |
| 281 | * -1 on error |
| 282 | * 1 if region is covered by write protection |
| 283 | * 0 if a part of region isn't covered by write protection |
| 284 | */ |
| 285 | static int winbond_get_write_protection(const struct spi_flash *flash, |
| 286 | const struct region *region) |
| 287 | { |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 288 | const struct spi_flash_part_id *params; |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 289 | struct region wp_region; |
| 290 | union status_reg2 reg2; |
| 291 | u8 bp, tb; |
| 292 | int ret; |
| 293 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 294 | params = flash->part; |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 295 | |
| 296 | if (!params) |
| 297 | return -1; |
| 298 | |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 299 | const size_t granularity = (1 << params->protection_granularity_shift); |
| 300 | |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 301 | union status_reg1 reg1 = { .u = 0 }; |
| 302 | |
| 303 | ret = spi_flash_cmd(&flash->spi, flash->status_cmd, ®1.u, |
| 304 | sizeof(reg1.u)); |
| 305 | if (ret) |
| 306 | return ret; |
| 307 | |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 308 | if (params->bp_bits == 3) { |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 309 | if (reg1.bp3.sec) { |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 310 | // FIXME: not supported |
| 311 | return -1; |
| 312 | } |
| 313 | |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 314 | bp = reg1.bp3.bp; |
| 315 | tb = reg1.bp3.tb; |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 316 | } else if (params->bp_bits == 4) { |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 317 | bp = reg1.bp4.bp; |
| 318 | tb = reg1.bp4.tb; |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 319 | } else { |
| 320 | // FIXME: not supported |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR2, ®2.u, |
| 325 | sizeof(reg2.u)); |
| 326 | if (ret) |
| 327 | return ret; |
| 328 | |
| 329 | winbond_bpbits_to_region(granularity, bp, tb, reg2.cmp, flash->size, |
| 330 | &wp_region); |
| 331 | |
Patrick Rudolph | 8747136 | 2018-09-25 14:26:33 +0200 | [diff] [blame] | 332 | if (!region_sz(&wp_region)) { |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 333 | printk(BIOS_DEBUG, "WINBOND: flash isn't protected\n"); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | printk(BIOS_DEBUG, "WINBOND: flash protected range 0x%08zx-0x%08zx\n", |
Aaron Durbin | 56aeae0 | 2019-11-08 14:37:58 -0700 | [diff] [blame] | 339 | region_offset(&wp_region), region_end(&wp_region)); |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 340 | |
| 341 | return region_is_subregion(&wp_region, region); |
| 342 | } |
| 343 | |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 344 | /** |
| 345 | * Common method to write some bit of the status register 1 & 2 at the same |
| 346 | * time. Only change bits that are one in @mask. |
| 347 | * Compare the final result to make sure that the register isn't locked. |
| 348 | * |
| 349 | * @param mask: The bits that are affected by @val |
| 350 | * @param val: The bits to write |
| 351 | * @param non_volatile: Make setting permanent |
| 352 | * |
| 353 | * @return 0 on success |
| 354 | */ |
| 355 | static int winbond_flash_cmd_status(const struct spi_flash *flash, |
| 356 | const u16 mask, |
| 357 | const u16 val, |
| 358 | const bool non_volatile) |
| 359 | { |
| 360 | struct { |
| 361 | u8 cmd; |
| 362 | u16 sreg; |
| 363 | } __packed cmdbuf; |
| 364 | u8 reg8; |
| 365 | int ret; |
| 366 | |
| 367 | if (!flash) |
| 368 | return -1; |
| 369 | |
| 370 | ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR, ®8, sizeof(reg8)); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | |
| 374 | cmdbuf.sreg = reg8; |
| 375 | |
| 376 | ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR2, ®8, sizeof(reg8)); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | |
| 380 | cmdbuf.sreg |= reg8 << 8; |
| 381 | |
| 382 | if ((val & mask) == (cmdbuf.sreg & mask)) |
| 383 | return 0; |
| 384 | |
| 385 | if (non_volatile) { |
| 386 | ret = spi_flash_cmd(&flash->spi, CMD_W25_WREN, NULL, 0); |
| 387 | } else { |
| 388 | ret = spi_flash_cmd(&flash->spi, CMD_VOLATILE_SREG_WREN, NULL, |
| 389 | 0); |
| 390 | } |
| 391 | if (ret) |
| 392 | return ret; |
| 393 | |
| 394 | cmdbuf.sreg &= ~mask; |
| 395 | cmdbuf.sreg |= val & mask; |
| 396 | cmdbuf.cmd = CMD_W25_WRSR; |
| 397 | |
| 398 | /* Legacy method of writing status register 1 & 2 */ |
| 399 | ret = spi_flash_cmd_write(&flash->spi, (u8 *)&cmdbuf, sizeof(cmdbuf), |
| 400 | NULL, 0); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | |
| 404 | if (non_volatile) { |
| 405 | /* Wait tw */ |
| 406 | ret = spi_flash_cmd_wait_ready(flash, WINBOND_FLASH_TIMEOUT); |
| 407 | if (ret) |
| 408 | return ret; |
| 409 | } else { |
| 410 | /* Wait tSHSL */ |
| 411 | udelay(1); |
| 412 | } |
| 413 | |
| 414 | /* Now read the status register to make sure it's not locked */ |
| 415 | ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR, ®8, sizeof(reg8)); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | |
| 419 | cmdbuf.sreg = reg8; |
| 420 | |
| 421 | ret = spi_flash_cmd(&flash->spi, CMD_W25_RDSR2, ®8, sizeof(reg8)); |
| 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | cmdbuf.sreg |= reg8 << 8; |
| 426 | |
| 427 | printk(BIOS_DEBUG, "WINBOND: SREG=%02x SREG2=%02x\n", |
| 428 | cmdbuf.sreg & 0xff, |
| 429 | cmdbuf.sreg >> 8); |
| 430 | |
| 431 | /* Compare against expected result */ |
| 432 | if ((val & mask) != (cmdbuf.sreg & mask)) { |
| 433 | printk(BIOS_ERR, "WINBOND: SREG is locked!\n"); |
| 434 | ret = -1; |
| 435 | } |
| 436 | |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Available on all devices. |
| 442 | * Protect a region starting from start of flash or end of flash. |
| 443 | * The caller must provide a supported protected region size. |
| 444 | * SEC isn't supported and set to zero. |
| 445 | * Write block protect bits to Status/Status2 Reg. |
| 446 | * Optionally lock the status register if lock_sreg is set with the provided |
| 447 | * mode. |
| 448 | * |
| 449 | * @param flash: The flash to operate on |
| 450 | * @param region: The region to write protect |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 451 | * @param mode: Optional status register lock-down mode |
| 452 | * |
| 453 | * @return 0 on success |
| 454 | */ |
| 455 | static int |
| 456 | winbond_set_write_protection(const struct spi_flash *flash, |
| 457 | const struct region *region, |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 458 | const enum spi_flash_status_reg_lockdown mode) |
| 459 | { |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 460 | const struct spi_flash_part_id *params; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 461 | struct status_regs mask, val; |
| 462 | struct region wp_region; |
| 463 | u8 cmp, bp, tb; |
| 464 | int ret; |
| 465 | |
| 466 | /* Need to touch TOP or BOTTOM */ |
Aaron Durbin | 56aeae0 | 2019-11-08 14:37:58 -0700 | [diff] [blame] | 467 | if (region_offset(region) != 0 && region_end(region) != flash->size) |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 468 | return -1; |
| 469 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 470 | params = flash->part; |
Aaron Durbin | a6c73c8 | 2020-01-11 23:18:51 -0700 | [diff] [blame] | 471 | |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 472 | if (!params) |
| 473 | return -1; |
| 474 | |
| 475 | if (params->bp_bits != 3 && params->bp_bits != 4) { |
| 476 | /* FIXME: not implemented */ |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | wp_region = *region; |
| 481 | |
| 482 | if (region_offset(&wp_region) == 0) |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 483 | tb = 1; |
Patrick Rudolph | e736015 | 2018-12-03 09:41:06 +0100 | [diff] [blame] | 484 | else |
| 485 | tb = 0; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 486 | |
| 487 | if (region_sz(&wp_region) > flash->size / 2) { |
| 488 | cmp = 1; |
| 489 | wp_region.offset = tb ? 0 : region_sz(&wp_region); |
| 490 | wp_region.size = flash->size - region_sz(&wp_region); |
| 491 | tb = !tb; |
| 492 | } else { |
| 493 | cmp = 0; |
| 494 | } |
| 495 | |
| 496 | if (region_sz(&wp_region) == 0) { |
| 497 | bp = 0; |
| 498 | } else if (IS_POWER_OF_2(region_sz(&wp_region)) && |
| 499 | (region_sz(&wp_region) >= |
| 500 | (1 << params->protection_granularity_shift))) { |
| 501 | bp = log2(region_sz(&wp_region)) - |
| 502 | params->protection_granularity_shift + 1; |
| 503 | } else { |
| 504 | printk(BIOS_ERR, "WINBOND: ERROR: unsupported region size\n"); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | /* Write block protection bits */ |
| 509 | |
| 510 | if (params->bp_bits == 3) { |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 511 | val.reg1 = (union status_reg1) { |
| 512 | .bp3 = { .bp = bp, .tb = tb, .sec = 0 } |
| 513 | }; |
| 514 | mask.reg1 = (union status_reg1) { |
| 515 | .bp3 = { .bp = ~0, .tb = 1, .sec = 1 } |
| 516 | }; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 517 | } else { |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 518 | val.reg1 = (union status_reg1) { |
| 519 | .bp4 = { .bp = bp, .tb = tb } |
| 520 | }; |
| 521 | mask.reg1 = (union status_reg1) { |
| 522 | .bp4 = { .bp = ~0, .tb = 1 } |
| 523 | }; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | val.reg2 = (union status_reg2) { .cmp = cmp }; |
| 527 | mask.reg2 = (union status_reg2) { .cmp = 1 }; |
| 528 | |
| 529 | if (mode != SPI_WRITE_PROTECTION_PRESERVE) { |
| 530 | u8 srp; |
| 531 | switch (mode) { |
| 532 | case SPI_WRITE_PROTECTION_NONE: |
| 533 | srp = 0; |
| 534 | break; |
| 535 | case SPI_WRITE_PROTECTION_PIN: |
| 536 | srp = 1; |
| 537 | break; |
| 538 | case SPI_WRITE_PROTECTION_REBOOT: |
| 539 | srp = 2; |
| 540 | break; |
| 541 | case SPI_WRITE_PROTECTION_PERMANENT: |
| 542 | srp = 3; |
| 543 | break; |
| 544 | default: |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | if (params->bp_bits == 3) { |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 549 | val.reg1.bp3.srp0 = !!(srp & 1); |
| 550 | mask.reg1.bp3.srp0 = 1; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 551 | } else { |
Daniel Gröber | 5569ee9 | 2020-06-04 16:25:13 +0200 | [diff] [blame] | 552 | val.reg1.bp4.srp0 = !!(srp & 1); |
| 553 | mask.reg1.bp4.srp0 = 1; |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | val.reg2.srp1 = !!(srp & 2); |
| 557 | mask.reg2.srp1 = 1; |
| 558 | } |
| 559 | |
Daniel Gröber | 0d0b2f4 | 2020-05-26 22:12:14 +0200 | [diff] [blame] | 560 | ret = winbond_flash_cmd_status(flash, mask.u, val.u, true); |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 561 | if (ret) |
| 562 | return ret; |
| 563 | |
| 564 | printk(BIOS_DEBUG, "WINBOND: write-protection set to range " |
Aaron Durbin | 56aeae0 | 2019-11-08 14:37:58 -0700 | [diff] [blame] | 565 | "0x%08zx-0x%08zx\n", region_offset(region), region_end(region)); |
Patrick Rudolph | e63a5f1 | 2018-03-12 11:34:53 +0100 | [diff] [blame] | 566 | |
| 567 | return ret; |
| 568 | } |
Patrick Rudolph | 0f8bf02 | 2018-03-09 14:20:25 +0100 | [diff] [blame] | 569 | |
Aaron Durbin | f584f19 | 2020-01-11 14:03:27 -0700 | [diff] [blame] | 570 | static const struct spi_flash_protection_ops spi_flash_protection_ops = { |
| 571 | .get_write = winbond_get_write_protection, |
| 572 | .set_write = winbond_set_write_protection, |
Furquan Shaikh | e2fc5e2 | 2017-05-17 17:26:01 -0700 | [diff] [blame] | 573 | }; |
| 574 | |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 575 | const struct spi_flash_vendor_info spi_flash_winbond_vi = { |
| 576 | .id = VENDOR_ID_WINBOND, |
| 577 | .page_size_shift = 8, |
| 578 | .sector_size_kib_shift = 2, |
Aaron Durbin | fc7b953 | 2020-01-23 11:45:30 -0700 | [diff] [blame] | 579 | .match_id_mask[0] = 0xffff, |
Aaron Durbin | 5abeb06 | 2020-01-12 15:12:18 -0700 | [diff] [blame] | 580 | .ids = flash_table, |
| 581 | .nr_part_ids = ARRAY_SIZE(flash_table), |
| 582 | .desc = &spi_flash_pp_0x20_sector_desc, |
| 583 | .prot_ops = &spi_flash_protection_ops, |
| 584 | }; |