Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010, 2011 Carl-Daniel Hailfinger |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
Carl-Daniel Hailfinger | 7053926 | 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 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * Contains the generic SPI framework |
| 23 | */ |
| 24 | |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 25 | #include <strings.h> |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 26 | #include <string.h> |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 27 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 28 | #include "flashchips.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 29 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 30 | #include "programmer.h" |
Carl-Daniel Hailfinger | d6cbf76 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 31 | #include "spi.h" |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 32 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 33 | int spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 34 | unsigned int readcnt, const unsigned char *writearr, |
| 35 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 36 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 37 | return flash->pgm->spi.command(flash, writecnt, readcnt, writearr, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 38 | readarr); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 41 | int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds) |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 42 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 43 | return flash->pgm->spi.multicommand(flash, cmds); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 46 | int default_spi_send_command(struct flashctx *flash, unsigned int writecnt, |
| 47 | unsigned int readcnt, |
| 48 | const unsigned char *writearr, |
| 49 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 50 | { |
| 51 | struct spi_command cmd[] = { |
| 52 | { |
| 53 | .writecnt = writecnt, |
| 54 | .readcnt = readcnt, |
| 55 | .writearr = writearr, |
| 56 | .readarr = readarr, |
| 57 | }, { |
| 58 | .writecnt = 0, |
| 59 | .writearr = NULL, |
| 60 | .readcnt = 0, |
| 61 | .readarr = NULL, |
| 62 | }}; |
| 63 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 64 | return spi_send_multicommand(flash, cmd); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 67 | int default_spi_send_multicommand(struct flashctx *flash, |
| 68 | struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 69 | { |
| 70 | int result = 0; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 71 | for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 72 | result = spi_send_command(flash, cmds->writecnt, cmds->readcnt, |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 73 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 74 | } |
| 75 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 78 | int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 79 | unsigned int len) |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 80 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 81 | unsigned int max_data = flash->pgm->spi.max_data_read; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 82 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 83 | msg_perr("%s called, but SPI read chunk size not defined " |
| 84 | "on this hardware. Please report a bug at " |
| 85 | "flashrom@flashrom.org\n", __func__); |
| 86 | return 1; |
| 87 | } |
| 88 | return spi_read_chunked(flash, buf, start, len, max_data); |
| 89 | } |
| 90 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 91 | int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 92 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 93 | unsigned int max_data = flash->pgm->spi.max_data_write; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 94 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 95 | msg_perr("%s called, but SPI write chunk size not defined " |
| 96 | "on this hardware. Please report a bug at " |
| 97 | "flashrom@flashrom.org\n", __func__); |
| 98 | return 1; |
| 99 | } |
| 100 | return spi_write_chunked(flash, buf, start, len, max_data); |
| 101 | } |
| 102 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 103 | int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 104 | unsigned int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 105 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 106 | unsigned int addrbase = 0; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 107 | |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 108 | /* Check if the chip fits between lowest valid and highest possible |
| 109 | * address. Highest possible address with the current SPI implementation |
| 110 | * means 0xffffff, the highest unsigned 24bit number. |
| 111 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 112 | addrbase = spi_get_valid_read_addr(flash); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 113 | if (addrbase + flash->chip->total_size * 1024 > (1 << 24)) { |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 114 | msg_perr("Flash chip size exceeds the allowed access window. "); |
| 115 | msg_perr("Read will probably fail.\n"); |
| 116 | /* Try to get the best alignment subject to constraints. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 117 | addrbase = (1 << 24) - flash->chip->total_size * 1024; |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 118 | } |
| 119 | /* Check if alignment is native (at least the largest power of two which |
| 120 | * is a factor of the mapped size of the chip). |
| 121 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 122 | if (ffs(flash->chip->total_size * 1024) > (ffs(addrbase) ? : 33)) { |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 123 | msg_perr("Flash chip is not aligned natively in the allowed " |
| 124 | "access window.\n"); |
| 125 | msg_perr("Read will probably return garbage.\n"); |
| 126 | } |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 127 | return flash->pgm->spi.read(flash, buf, addrbase + start, len); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 130 | /* |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 131 | * Program chip using page (256 bytes) programming. |
| 132 | * Some SPI masters can't do this, they use single byte programming instead. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 133 | * The redirect to single byte programming is achieved by setting |
| 134 | * .write_256 = spi_chip_write_1 |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 135 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 136 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 137 | int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 138 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 139 | return flash->pgm->spi.write_256(flash, buf, start, len); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 142 | /* |
| 143 | * Get the lowest allowed address for read accesses. This often happens to |
| 144 | * be the lowest allowed address for all commands which take an address. |
| 145 | * This is a programmer limitation. |
| 146 | */ |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 147 | uint32_t spi_get_valid_read_addr(struct flashctx *flash) |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 148 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 149 | switch (flash->pgm->spi.type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 150 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 151 | #if defined(__i386__) || defined(__x86_64__) |
| 152 | case SPI_CONTROLLER_ICH7: |
Stefan Tauner | eb58257 | 2012-09-21 12:52:50 +0000 | [diff] [blame] | 153 | case SPI_CONTROLLER_ICH9: |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 154 | /* Return BBAR for ICH chipsets. */ |
| 155 | return ichspi_bbar; |
| 156 | #endif |
| 157 | #endif |
| 158 | default: |
| 159 | return 0; |
| 160 | } |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 161 | } |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 162 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 163 | int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 164 | { |
| 165 | return flash->pgm->spi.write_aai(flash, buf, start, len); |
| 166 | } |
| 167 | |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 168 | int register_spi_programmer(const struct spi_programmer *pgm) |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 169 | { |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 170 | struct registered_programmer rpgm; |
| 171 | |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 172 | if (!pgm->write_aai || !pgm->write_256 || !pgm->read || !pgm->command || |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 173 | !pgm->multicommand || |
| 174 | ((pgm->command == default_spi_send_command) && |
| 175 | (pgm->multicommand == default_spi_send_multicommand))) { |
| 176 | msg_perr("%s called with incomplete programmer definition. " |
| 177 | "Please report a bug at flashrom@flashrom.org\n", |
| 178 | __func__); |
| 179 | return ERROR_FLASHROM_BUG; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | rpgm.buses_supported = BUS_SPI; |
| 184 | rpgm.spi = *pgm; |
| 185 | return register_programmer(&rpgm); |
Stefan Tauner | 93f7023 | 2011-07-26 14:33:46 +0000 | [diff] [blame] | 186 | } |