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 | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009 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 | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 33 | enum spi_controller spi_controller = SPI_CONTROLLER_NONE; |
Carl-Daniel Hailfinger | 1dfe0ff | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 34 | |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 35 | const struct spi_programmer spi_programmer[] = { |
| 36 | { /* SPI_CONTROLLER_NONE */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 37 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 38 | .max_data_write = MAX_DATA_UNSPECIFIED, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 39 | .command = NULL, |
| 40 | .multicommand = NULL, |
| 41 | .read = NULL, |
| 42 | .write_256 = NULL, |
| 43 | }, |
| 44 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 45 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 46 | #if defined(__i386__) || defined(__x86_64__) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 47 | { /* SPI_CONTROLLER_ICH7 */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 48 | .max_data_read = 64, |
| 49 | .max_data_write = 64, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 50 | .command = ich_spi_send_command, |
| 51 | .multicommand = ich_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 52 | .read = default_spi_read, |
| 53 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 54 | }, |
| 55 | |
| 56 | { /* SPI_CONTROLLER_ICH9 */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 57 | .max_data_read = 64, |
| 58 | .max_data_write = 64, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 59 | .command = ich_spi_send_command, |
| 60 | .multicommand = ich_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 61 | .read = default_spi_read, |
| 62 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 63 | }, |
| 64 | |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 65 | { /* SPI_CONTROLLER_IT85XX */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 66 | .max_data_read = 64, |
| 67 | .max_data_write = 64, |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 68 | .command = it85xx_spi_send_command, |
| 69 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 70 | .read = default_spi_read, |
| 71 | .write_256 = default_spi_write_256, |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 72 | }, |
| 73 | |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 74 | { /* SPI_CONTROLLER_IT87XX */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 75 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 76 | .max_data_write = MAX_DATA_UNSPECIFIED, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 77 | .command = it8716f_spi_send_command, |
| 78 | .multicommand = default_spi_send_multicommand, |
| 79 | .read = it8716f_spi_chip_read, |
| 80 | .write_256 = it8716f_spi_chip_write_256, |
| 81 | }, |
| 82 | |
| 83 | { /* SPI_CONTROLLER_SB600 */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 84 | .max_data_read = 8, |
| 85 | .max_data_write = 5, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 86 | .command = sb600_spi_send_command, |
| 87 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 88 | .read = default_spi_read, |
| 89 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 90 | }, |
| 91 | |
| 92 | { /* SPI_CONTROLLER_VIA */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 93 | .max_data_read = 16, |
| 94 | .max_data_write = 16, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 95 | .command = ich_spi_send_command, |
| 96 | .multicommand = ich_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 97 | .read = default_spi_read, |
| 98 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 99 | }, |
| 100 | |
| 101 | { /* SPI_CONTROLLER_WBSIO */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 102 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 103 | .max_data_write = MAX_DATA_UNSPECIFIED, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 104 | .command = wbsio_spi_send_command, |
| 105 | .multicommand = default_spi_send_multicommand, |
| 106 | .read = wbsio_spi_read, |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 107 | .write_256 = spi_chip_write_1, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 108 | }, |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 109 | |
| 110 | { /* SPI_CONTROLLER_MCP6X_BITBANG */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 111 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 112 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 113 | .command = bitbang_spi_send_command, |
| 114 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 115 | .read = default_spi_read, |
| 116 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 2f43616 | 2010-07-28 15:08:35 +0000 | [diff] [blame] | 117 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 118 | #endif |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 119 | #endif |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 120 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 121 | #if CONFIG_FT2232_SPI == 1 |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 122 | { /* SPI_CONTROLLER_FT2232 */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 123 | .max_data_read = 64 * 1024, |
| 124 | .max_data_write = 256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 125 | .command = ft2232_spi_send_command, |
| 126 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 127 | .read = default_spi_read, |
| 128 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 129 | }, |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 130 | #endif |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 131 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 132 | #if CONFIG_DUMMY == 1 |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 133 | { /* SPI_CONTROLLER_DUMMY */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 134 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 135 | .max_data_write = MAX_DATA_UNSPECIFIED, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 136 | .command = dummy_spi_send_command, |
| 137 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 138 | .read = default_spi_read, |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 139 | .write_256 = dummy_spi_write_256, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 140 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 141 | #endif |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 142 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 143 | #if CONFIG_BUSPIRATE_SPI == 1 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 144 | { /* SPI_CONTROLLER_BUSPIRATE */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 145 | .max_data_read = 12, |
| 146 | .max_data_write = 12, |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 147 | .command = buspirate_spi_send_command, |
| 148 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 149 | .read = default_spi_read, |
| 150 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 151 | }, |
| 152 | #endif |
| 153 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 154 | #if CONFIG_DEDIPROG == 1 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 155 | { /* SPI_CONTROLLER_DEDIPROG */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 156 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 157 | .max_data_write = MAX_DATA_UNSPECIFIED, |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 158 | .command = dediprog_spi_send_command, |
| 159 | .multicommand = default_spi_send_multicommand, |
| 160 | .read = dediprog_spi_read, |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 161 | .write_256 = dediprog_spi_write_256, |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 162 | }, |
| 163 | #endif |
| 164 | |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 165 | #if CONFIG_RAYER_SPI == 1 |
| 166 | { /* SPI_CONTROLLER_RAYER */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 167 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 168 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 169 | .command = bitbang_spi_send_command, |
| 170 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 171 | .read = default_spi_read, |
| 172 | .write_256 = default_spi_write_256, |
Carl-Daniel Hailfinger | e7fdd6e | 2010-07-21 10:26:01 +0000 | [diff] [blame] | 173 | }, |
| 174 | #endif |
| 175 | |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 176 | #if CONFIG_NICINTEL_SPI == 1 |
| 177 | { /* SPI_CONTROLLER_NICINTEL */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 178 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 179 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 180 | .command = bitbang_spi_send_command, |
| 181 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 182 | .read = default_spi_read, |
| 183 | .write_256 = default_spi_write_256, |
Idwer Vollering | 004f4b7 | 2010-09-03 18:21:21 +0000 | [diff] [blame] | 184 | }, |
| 185 | #endif |
| 186 | |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 187 | #if CONFIG_OGP_SPI == 1 |
| 188 | { /* SPI_CONTROLLER_OGP */ |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 189 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 190 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 191 | .command = bitbang_spi_send_command, |
| 192 | .multicommand = default_spi_send_multicommand, |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 193 | .read = default_spi_read, |
| 194 | .write_256 = default_spi_write_256, |
Mark Marshall | 90021f2 | 2010-12-03 14:48:11 +0000 | [diff] [blame] | 195 | }, |
| 196 | #endif |
| 197 | |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 198 | {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */ |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 201 | const int spi_programmer_count = ARRAY_SIZE(spi_programmer); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 202 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 203 | int spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 204 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 205 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 206 | if (!spi_programmer[spi_controller].command) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 207 | msg_perr("%s called, but SPI is unsupported on this " |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 208 | "hardware. Please report a bug at " |
| 209 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 210 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 211 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 212 | |
| 213 | return spi_programmer[spi_controller].command(writecnt, readcnt, |
| 214 | writearr, readarr); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 217 | int spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 218 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 219 | if (!spi_programmer[spi_controller].multicommand) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 220 | msg_perr("%s called, but SPI is unsupported on this " |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 221 | "hardware. Please report a bug at " |
| 222 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 223 | return 1; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 224 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 225 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 226 | return spi_programmer[spi_controller].multicommand(cmds); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | int default_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 230 | const unsigned char *writearr, unsigned char *readarr) |
| 231 | { |
| 232 | struct spi_command cmd[] = { |
| 233 | { |
| 234 | .writecnt = writecnt, |
| 235 | .readcnt = readcnt, |
| 236 | .writearr = writearr, |
| 237 | .readarr = readarr, |
| 238 | }, { |
| 239 | .writecnt = 0, |
| 240 | .writearr = NULL, |
| 241 | .readcnt = 0, |
| 242 | .readarr = NULL, |
| 243 | }}; |
| 244 | |
| 245 | return spi_send_multicommand(cmd); |
| 246 | } |
| 247 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 248 | int default_spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 249 | { |
| 250 | int result = 0; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 251 | for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) { |
| 252 | result = spi_send_command(cmds->writecnt, cmds->readcnt, |
| 253 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 254 | } |
| 255 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame^] | 258 | int default_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 259 | { |
| 260 | int max_data = spi_programmer[spi_controller].max_data_read; |
| 261 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 262 | msg_perr("%s called, but SPI read chunk size not defined " |
| 263 | "on this hardware. Please report a bug at " |
| 264 | "flashrom@flashrom.org\n", __func__); |
| 265 | return 1; |
| 266 | } |
| 267 | return spi_read_chunked(flash, buf, start, len, max_data); |
| 268 | } |
| 269 | |
| 270 | int default_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 271 | { |
| 272 | int max_data = spi_programmer[spi_controller].max_data_write; |
| 273 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 274 | msg_perr("%s called, but SPI write chunk size not defined " |
| 275 | "on this hardware. Please report a bug at " |
| 276 | "flashrom@flashrom.org\n", __func__); |
| 277 | return 1; |
| 278 | } |
| 279 | return spi_write_chunked(flash, buf, start, len, max_data); |
| 280 | } |
| 281 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 282 | int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 283 | { |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 284 | int addrbase = 0; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 285 | if (!spi_programmer[spi_controller].read) { |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 286 | msg_perr("%s called, but SPI read is unsupported on this " |
| 287 | "hardware. Please report a bug at " |
| 288 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 289 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 292 | /* Check if the chip fits between lowest valid and highest possible |
| 293 | * address. Highest possible address with the current SPI implementation |
| 294 | * means 0xffffff, the highest unsigned 24bit number. |
| 295 | */ |
| 296 | addrbase = spi_get_valid_read_addr(); |
| 297 | if (addrbase + flash->total_size * 1024 > (1 << 24)) { |
| 298 | msg_perr("Flash chip size exceeds the allowed access window. "); |
| 299 | msg_perr("Read will probably fail.\n"); |
| 300 | /* Try to get the best alignment subject to constraints. */ |
| 301 | addrbase = (1 << 24) - flash->total_size * 1024; |
| 302 | } |
| 303 | /* Check if alignment is native (at least the largest power of two which |
| 304 | * is a factor of the mapped size of the chip). |
| 305 | */ |
| 306 | if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) { |
| 307 | msg_perr("Flash chip is not aligned natively in the allowed " |
| 308 | "access window.\n"); |
| 309 | msg_perr("Read will probably return garbage.\n"); |
| 310 | } |
| 311 | return spi_programmer[spi_controller].read(flash, buf, addrbase + start, len); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 314 | /* |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 315 | * Program chip using page (256 bytes) programming. |
| 316 | * 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] | 317 | * The redirect to single byte programming is achieved by setting |
| 318 | * .write_256 = spi_chip_write_1 |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 319 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 320 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 321 | int spi_chip_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 322 | { |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 323 | if (!spi_programmer[spi_controller].write_256) { |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 324 | msg_perr("%s called, but SPI page write is unsupported on this " |
| 325 | "hardware. Please report a bug at " |
| 326 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 327 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 330 | return spi_programmer[spi_controller].write_256(flash, buf, start, len); |
| 331 | } |
| 332 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 333 | /* |
| 334 | * Get the lowest allowed address for read accesses. This often happens to |
| 335 | * be the lowest allowed address for all commands which take an address. |
| 336 | * This is a programmer limitation. |
| 337 | */ |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 338 | uint32_t spi_get_valid_read_addr(void) |
| 339 | { |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 340 | switch (spi_controller) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 341 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 342 | #if defined(__i386__) || defined(__x86_64__) |
| 343 | case SPI_CONTROLLER_ICH7: |
| 344 | /* Return BBAR for ICH chipsets. */ |
| 345 | return ichspi_bbar; |
| 346 | #endif |
| 347 | #endif |
| 348 | default: |
| 349 | return 0; |
| 350 | } |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 351 | } |