stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
hailfinger | a128904 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * Contains the generic SPI framework |
| 20 | */ |
| 21 | |
oxygene | 70aa650 | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 22 | #include <strings.h> |
hailfinger | 132df7b | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 23 | #include <string.h> |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 24 | #include "flash.h" |
hailfinger | 66966da | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 25 | #include "flashchips.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 26 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 27 | #include "programmer.h" |
hailfinger | 7803156 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 28 | #include "spi.h" |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 29 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 30 | const struct spi_master spi_master_none = { |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 31 | .type = SPI_CONTROLLER_NONE, |
| 32 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 33 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 34 | .command = NULL, |
| 35 | .multicommand = NULL, |
| 36 | .read = NULL, |
| 37 | .write_256 = NULL, |
| 38 | }; |
| 39 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 40 | const struct spi_master *spi_master = &spi_master_none; |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 41 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 42 | int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 43 | const unsigned char *writearr, unsigned char *readarr) |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 44 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 45 | if (!spi_master->command) { |
Duncan Laurie | 870d8af | 2019-01-09 18:05:23 -0800 | [diff] [blame] | 46 | msg_pdbg("%s called, but SPI is unsupported on this " |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 47 | "hardware. Please report a bug at " |
| 48 | "flashrom@flashrom.org\n", __func__); |
| 49 | return 1; |
| 50 | } |
| 51 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 52 | return spi_master->command(flash, writecnt, readcnt, |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 53 | writearr, readarr); |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 56 | int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds) |
hailfinger | 68002c2 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 57 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 58 | if (!spi_master->multicommand) { |
Duncan Laurie | 870d8af | 2019-01-09 18:05:23 -0800 | [diff] [blame] | 59 | msg_pdbg("%s called, but SPI is unsupported on this " |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 60 | "hardware. Please report a bug at " |
| 61 | "flashrom@flashrom.org\n", __func__); |
| 62 | return 1; |
| 63 | } |
| 64 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 65 | return spi_master->multicommand(flash, cmds); |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 68 | int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 69 | const unsigned char *writearr, unsigned char *readarr) |
| 70 | { |
| 71 | struct spi_command cmd[] = { |
| 72 | { |
| 73 | .writecnt = writecnt, |
| 74 | .readcnt = readcnt, |
| 75 | .writearr = writearr, |
| 76 | .readarr = readarr, |
| 77 | }, { |
| 78 | .writecnt = 0, |
| 79 | .writearr = NULL, |
| 80 | .readcnt = 0, |
| 81 | .readarr = NULL, |
| 82 | }}; |
| 83 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 84 | return spi_send_multicommand(flash, cmd); |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 87 | int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds) |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 88 | { |
| 89 | int result = 0; |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 90 | for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 91 | result = spi_send_command(flash, cmds->writecnt, cmds->readcnt, |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 92 | cmds->writearr, cmds->readarr); |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 93 | } |
| 94 | return result; |
hailfinger | 68002c2 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 97 | int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 98 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 99 | unsigned int max_data = spi_master->max_data_read; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 100 | int rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 101 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 102 | msg_perr("%s called, but SPI read chunk size not defined " |
| 103 | "on this hardware. Please report a bug at " |
| 104 | "flashrom@flashrom.org\n", __func__); |
| 105 | return 1; |
| 106 | } |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 107 | if (flash->chip->feature_bits & FEATURE_UNBOUND_READ) |
Duncan Laurie | 06ffd52 | 2015-10-26 12:56:08 -0700 | [diff] [blame] | 108 | rc = spi_read_unbound(flash, buf, start, len, max_data); |
| 109 | else |
| 110 | rc = spi_read_chunked(flash, buf, start, len, max_data); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 111 | /* translate SPI-specific access denied error to generic error */ |
| 112 | if (rc == SPI_ACCESS_DENIED) |
| 113 | rc = ACCESS_DENIED; |
| 114 | return rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 117 | int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 118 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 119 | unsigned int max_data = spi_master->max_data_write; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 120 | int rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 121 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 122 | msg_perr("%s called, but SPI write chunk size not defined " |
| 123 | "on this hardware. Please report a bug at " |
| 124 | "flashrom@flashrom.org\n", __func__); |
| 125 | return 1; |
| 126 | } |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 127 | rc = spi_write_chunked(flash, buf, start, len, max_data); |
| 128 | /* translate SPI-specific access denied error to generic error */ |
| 129 | if (rc == SPI_ACCESS_DENIED) |
| 130 | rc = ACCESS_DENIED; |
| 131 | return rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 134 | int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 135 | { |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 136 | unsigned int addrbase = 0; |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 137 | if (!spi_master->read) { |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 138 | msg_perr("%s called, but SPI read is unsupported on this " |
| 139 | "hardware. Please report a bug at " |
| 140 | "flashrom@flashrom.org\n", __func__); |
| 141 | return 1; |
| 142 | } |
| 143 | |
hailfinger | 132df7b | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 144 | /* Check if the chip fits between lowest valid and highest possible |
| 145 | * address. Highest possible address with the current SPI implementation |
| 146 | * means 0xffffff, the highest unsigned 24bit number. |
| 147 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 148 | addrbase = spi_get_valid_read_addr(flash); |
Boris Baykov | 1a2f532 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 149 | /* Show flash chip size warning if flash chip doesn't support |
| 150 | 4-Bytes Addressing mode and last address excedes 24 bits */ |
Edward O'Callaghan | 9713aa6 | 2019-07-18 18:28:57 +1000 | [diff] [blame^] | 151 | if (!(flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN) && |
Boris Baykov | 1a2f532 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 152 | addrbase + flash->chip->total_size * 1024 > (1 << 24)) { |
hailfinger | 132df7b | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 153 | msg_perr("Flash chip size exceeds the allowed access window. "); |
| 154 | msg_perr("Read will probably fail.\n"); |
| 155 | /* Try to get the best alignment subject to constraints. */ |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 156 | addrbase = (1 << 24) - flash->chip->total_size * 1024; |
hailfinger | 132df7b | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 157 | } |
| 158 | /* Check if alignment is native (at least the largest power of two which |
| 159 | * is a factor of the mapped size of the chip). |
| 160 | */ |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 161 | if (ffs(flash->chip->total_size * 1024) > (ffs(addrbase) ? : 33)) { |
hailfinger | 132df7b | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 162 | msg_perr("Flash chip is not aligned natively in the allowed " |
| 163 | "access window.\n"); |
| 164 | msg_perr("Read will probably return garbage.\n"); |
| 165 | } |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 166 | return spi_master->read(flash, buf, addrbase + start, len); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 167 | } |
| 168 | |
hailfinger | ed063f5 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 169 | /* |
hailfinger | ed063f5 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 170 | * Program chip using page (256 bytes) programming. |
| 171 | * Some SPI masters can't do this, they use single byte programming instead. |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 172 | * The redirect to single byte programming is achieved by setting |
| 173 | * .write_256 = spi_chip_write_1 |
hailfinger | ed063f5 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 174 | */ |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 175 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 176 | int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 177 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 178 | if (!spi_master->write_256) { |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 179 | msg_perr("%s called, but SPI page write is unsupported on this " |
| 180 | "hardware. Please report a bug at " |
| 181 | "flashrom@flashrom.org\n", __func__); |
| 182 | return 1; |
| 183 | } |
| 184 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 185 | return spi_master->write_256(flash, buf, start, len); |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 186 | } |
| 187 | |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 188 | /* |
| 189 | * Get the lowest allowed address for read accesses. This often happens to |
| 190 | * be the lowest allowed address for all commands which take an address. |
| 191 | * This is a programmer limitation. |
| 192 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 193 | uint32_t spi_get_valid_read_addr(struct flashctx *flash) |
hailfinger | 54c1466 | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 194 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 195 | switch (spi_master->type) { |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 196 | #if CONFIG_INTERNAL == 1 |
hailfinger | b767c12 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 197 | #if defined(__i386__) || defined(__x86_64__) |
| 198 | case SPI_CONTROLLER_ICH7: |
| 199 | /* Return BBAR for ICH chipsets. */ |
| 200 | return ichspi_bbar; |
| 201 | #endif |
| 202 | #endif |
| 203 | default: |
| 204 | return 0; |
| 205 | } |
hailfinger | 54c1466 | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 206 | } |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 207 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 208 | void register_spi_master(const struct spi_master *pgm) |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 209 | { |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 210 | spi_master = pgm; |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 211 | buses_supported |= BUS_SPI; |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 212 | } |