blob: 5857fdac3e424683cc416447c96939cd8782106e [file] [log] [blame]
stepand4b13752007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfingera1289042009-06-24 08:28:39 +00004 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
stepandbd3af12008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
stepand4b13752007-10-15 21:45:29 +00006 *
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
oxygene70aa6502011-03-08 07:17:44 +000025#include <strings.h>
hailfinger132df7b2010-09-15 00:13:02 +000026#include <string.h>
stepand4b13752007-10-15 21:45:29 +000027#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000028#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000029#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000030#include "programmer.h"
hailfinger78031562008-05-13 14:58:23 +000031#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000032
hailfinger40167462009-05-31 17:57:34 +000033enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
hailfinger40167462009-05-31 17:57:34 +000034
hailfinger948b81f2009-07-22 15:36:50 +000035const struct spi_programmer spi_programmer[] = {
36 { /* SPI_CONTROLLER_NONE */
mkarcher8fb57592011-05-11 17:07:02 +000037 .max_data_read = MAX_DATA_UNSPECIFIED,
38 .max_data_write = MAX_DATA_UNSPECIFIED,
hailfinger948b81f2009-07-22 15:36:50 +000039 .command = NULL,
40 .multicommand = NULL,
41 .read = NULL,
42 .write_256 = NULL,
43 },
44
hailfinger90c7d542010-05-31 15:27:27 +000045#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +000046#if defined(__i386__) || defined(__x86_64__)
hailfinger948b81f2009-07-22 15:36:50 +000047 { /* SPI_CONTROLLER_ICH7 */
mkarcher8fb57592011-05-11 17:07:02 +000048 .max_data_read = 64,
49 .max_data_write = 64,
hailfinger948b81f2009-07-22 15:36:50 +000050 .command = ich_spi_send_command,
51 .multicommand = ich_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +000052 .read = default_spi_read,
53 .write_256 = default_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +000054 },
55
56 { /* SPI_CONTROLLER_ICH9 */
mkarcher8fb57592011-05-11 17:07:02 +000057 .max_data_read = 64,
58 .max_data_write = 64,
hailfinger948b81f2009-07-22 15:36:50 +000059 .command = ich_spi_send_command,
60 .multicommand = ich_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +000061 .read = default_spi_read,
62 .write_256 = default_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +000063 },
64
hailfinger2b46a862011-02-28 23:58:15 +000065 { /* SPI_CONTROLLER_IT85XX */
mkarcher8fb57592011-05-11 17:07:02 +000066 .max_data_read = 64,
67 .max_data_write = 64,
hailfinger2b46a862011-02-28 23:58:15 +000068 .command = it85xx_spi_send_command,
69 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +000070 .read = default_spi_read,
71 .write_256 = default_spi_write_256,
hailfinger2b46a862011-02-28 23:58:15 +000072 },
73
hailfinger948b81f2009-07-22 15:36:50 +000074 { /* SPI_CONTROLLER_IT87XX */
mkarcher8fb57592011-05-11 17:07:02 +000075 .max_data_read = MAX_DATA_UNSPECIFIED,
76 .max_data_write = MAX_DATA_UNSPECIFIED,
hailfinger948b81f2009-07-22 15:36:50 +000077 .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 */
mkarcher8fb57592011-05-11 17:07:02 +000084 .max_data_read = 8,
85 .max_data_write = 5,
hailfinger948b81f2009-07-22 15:36:50 +000086 .command = sb600_spi_send_command,
87 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +000088 .read = default_spi_read,
89 .write_256 = default_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +000090 },
91
92 { /* SPI_CONTROLLER_VIA */
mkarcher8fb57592011-05-11 17:07:02 +000093 .max_data_read = 16,
94 .max_data_write = 16,
hailfinger948b81f2009-07-22 15:36:50 +000095 .command = ich_spi_send_command,
96 .multicommand = ich_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +000097 .read = default_spi_read,
98 .write_256 = default_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +000099 },
100
101 { /* SPI_CONTROLLER_WBSIO */
mkarcher8fb57592011-05-11 17:07:02 +0000102 .max_data_read = MAX_DATA_UNSPECIFIED,
103 .max_data_write = MAX_DATA_UNSPECIFIED,
hailfinger948b81f2009-07-22 15:36:50 +0000104 .command = wbsio_spi_send_command,
105 .multicommand = default_spi_send_multicommand,
106 .read = wbsio_spi_read,
hailfinger71e1bd42010-10-13 22:26:56 +0000107 .write_256 = spi_chip_write_1,
hailfinger948b81f2009-07-22 15:36:50 +0000108 },
hailfinger52384c92010-07-28 15:08:35 +0000109
110 { /* SPI_CONTROLLER_MCP6X_BITBANG */
mkarcher8fb57592011-05-11 17:07:02 +0000111 .max_data_read = MAX_DATA_READ_UNLIMITED,
112 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
hailfinger52384c92010-07-28 15:08:35 +0000113 .command = bitbang_spi_send_command,
114 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000115 .read = default_spi_read,
116 .write_256 = default_spi_write_256,
hailfinger52384c92010-07-28 15:08:35 +0000117 },
hailfinger80422e22009-12-13 22:28:00 +0000118#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000119#endif
hailfinger948b81f2009-07-22 15:36:50 +0000120
hailfinger90c7d542010-05-31 15:27:27 +0000121#if CONFIG_FT2232_SPI == 1
hailfinger948b81f2009-07-22 15:36:50 +0000122 { /* SPI_CONTROLLER_FT2232 */
mkarcher8fb57592011-05-11 17:07:02 +0000123 .max_data_read = 64 * 1024,
124 .max_data_write = 256,
hailfinger948b81f2009-07-22 15:36:50 +0000125 .command = ft2232_spi_send_command,
126 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000127 .read = default_spi_read,
128 .write_256 = default_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +0000129 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000130#endif
hailfinger948b81f2009-07-22 15:36:50 +0000131
hailfinger90c7d542010-05-31 15:27:27 +0000132#if CONFIG_DUMMY == 1
hailfinger948b81f2009-07-22 15:36:50 +0000133 { /* SPI_CONTROLLER_DUMMY */
mkarcher8fb57592011-05-11 17:07:02 +0000134 .max_data_read = MAX_DATA_READ_UNLIMITED,
135 .max_data_write = MAX_DATA_UNSPECIFIED,
hailfinger948b81f2009-07-22 15:36:50 +0000136 .command = dummy_spi_send_command,
137 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000138 .read = default_spi_read,
hailfingerc7d06c62010-07-14 16:19:05 +0000139 .write_256 = dummy_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +0000140 },
hailfinger571a6b32009-09-16 10:09:21 +0000141#endif
hailfingerd9dcfbd2009-08-19 13:27:58 +0000142
hailfinger90c7d542010-05-31 15:27:27 +0000143#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000144 { /* SPI_CONTROLLER_BUSPIRATE */
mkarcher8fb57592011-05-11 17:07:02 +0000145 .max_data_read = 12,
146 .max_data_write = 12,
hailfinger9c5add72009-11-24 00:20:03 +0000147 .command = buspirate_spi_send_command,
148 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000149 .read = default_spi_read,
150 .write_256 = default_spi_write_256,
hailfinger9c5add72009-11-24 00:20:03 +0000151 },
152#endif
153
hailfinger90c7d542010-05-31 15:27:27 +0000154#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000155 { /* SPI_CONTROLLER_DEDIPROG */
mkarcher8fb57592011-05-11 17:07:02 +0000156 .max_data_read = MAX_DATA_UNSPECIFIED,
157 .max_data_write = MAX_DATA_UNSPECIFIED,
hailfingerdfb32a02010-01-19 11:15:48 +0000158 .command = dediprog_spi_send_command,
159 .multicommand = default_spi_send_multicommand,
160 .read = dediprog_spi_read,
hailfinger556e9c32010-11-23 21:28:16 +0000161 .write_256 = dediprog_spi_write_256,
hailfingerdfb32a02010-01-19 11:15:48 +0000162 },
163#endif
164
hailfinger52c4fa02010-07-21 10:26:01 +0000165#if CONFIG_RAYER_SPI == 1
166 { /* SPI_CONTROLLER_RAYER */
mkarcher8fb57592011-05-11 17:07:02 +0000167 .max_data_read = MAX_DATA_READ_UNLIMITED,
168 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
hailfinger52c4fa02010-07-21 10:26:01 +0000169 .command = bitbang_spi_send_command,
170 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000171 .read = default_spi_read,
172 .write_256 = default_spi_write_256,
hailfinger52c4fa02010-07-21 10:26:01 +0000173 },
174#endif
175
uwe6764e922010-09-03 18:21:21 +0000176#if CONFIG_NICINTEL_SPI == 1
177 { /* SPI_CONTROLLER_NICINTEL */
mkarcher8fb57592011-05-11 17:07:02 +0000178 .max_data_read = MAX_DATA_READ_UNLIMITED,
179 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
uwe6764e922010-09-03 18:21:21 +0000180 .command = bitbang_spi_send_command,
181 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000182 .read = default_spi_read,
183 .write_256 = default_spi_write_256,
uwe6764e922010-09-03 18:21:21 +0000184 },
185#endif
186
hailfingerfb1f31f2010-12-03 14:48:11 +0000187#if CONFIG_OGP_SPI == 1
188 { /* SPI_CONTROLLER_OGP */
mkarcher8fb57592011-05-11 17:07:02 +0000189 .max_data_read = MAX_DATA_READ_UNLIMITED,
190 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
hailfingerfb1f31f2010-12-03 14:48:11 +0000191 .command = bitbang_spi_send_command,
192 .multicommand = default_spi_send_multicommand,
mkarcher8fb57592011-05-11 17:07:02 +0000193 .read = default_spi_read,
194 .write_256 = default_spi_write_256,
hailfingerfb1f31f2010-12-03 14:48:11 +0000195 },
196#endif
197
hailfingerd9dcfbd2009-08-19 13:27:58 +0000198 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
hailfinger948b81f2009-07-22 15:36:50 +0000199};
200
hailfingerd9dcfbd2009-08-19 13:27:58 +0000201const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
hailfinger948b81f2009-07-22 15:36:50 +0000202
hailfinger68002c22009-07-10 21:08:55 +0000203int spi_send_command(unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000204 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +0000205{
hailfinger948b81f2009-07-22 15:36:50 +0000206 if (!spi_programmer[spi_controller].command) {
snelsone42c3802010-05-07 20:09:04 +0000207 msg_perr("%s called, but SPI is unsupported on this "
hailfingercb0564e2010-06-20 10:39:33 +0000208 "hardware. Please report a bug at "
209 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000210 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000211 }
hailfinger948b81f2009-07-22 15:36:50 +0000212
213 return spi_programmer[spi_controller].command(writecnt, readcnt,
214 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +0000215}
216
hailfingerbb092112009-09-18 15:50:56 +0000217int spi_send_multicommand(struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +0000218{
hailfinger948b81f2009-07-22 15:36:50 +0000219 if (!spi_programmer[spi_controller].multicommand) {
snelsone42c3802010-05-07 20:09:04 +0000220 msg_perr("%s called, but SPI is unsupported on this "
hailfingercb0564e2010-06-20 10:39:33 +0000221 "hardware. Please report a bug at "
222 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000223 return 1;
hailfinger68002c22009-07-10 21:08:55 +0000224 }
hailfinger948b81f2009-07-22 15:36:50 +0000225
hailfingerbb092112009-09-18 15:50:56 +0000226 return spi_programmer[spi_controller].multicommand(cmds);
hailfinger948b81f2009-07-22 15:36:50 +0000227}
228
229int 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
hailfingerbb092112009-09-18 15:50:56 +0000248int default_spi_send_multicommand(struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +0000249{
250 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +0000251 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
252 result = spi_send_command(cmds->writecnt, cmds->readcnt,
253 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +0000254 }
255 return result;
hailfinger68002c22009-07-10 21:08:55 +0000256}
257
mkarcher8fb57592011-05-11 17:07:02 +0000258int 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
270int 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
hailfinger0f08b7a2009-06-16 08:55:44 +0000282int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000283{
hailfinger132df7b2010-09-15 00:13:02 +0000284 int addrbase = 0;
hailfinger948b81f2009-07-22 15:36:50 +0000285 if (!spi_programmer[spi_controller].read) {
hailfingercb0564e2010-06-20 10:39:33 +0000286 msg_perr("%s called, but SPI read is unsupported on this "
287 "hardware. Please report a bug at "
288 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000289 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000290 }
291
hailfinger132df7b2010-09-15 00:13:02 +0000292 /* 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);
hailfingerb8f7e882008-01-19 00:04:46 +0000312}
313
hailfingered063f52009-05-09 02:30:21 +0000314/*
hailfingered063f52009-05-09 02:30:21 +0000315 * Program chip using page (256 bytes) programming.
316 * Some SPI masters can't do this, they use single byte programming instead.
hailfingerc7d06c62010-07-14 16:19:05 +0000317 * The redirect to single byte programming is achieved by setting
318 * .write_256 = spi_chip_write_1
hailfingered063f52009-05-09 02:30:21 +0000319 */
hailfingerc7d06c62010-07-14 16:19:05 +0000320/* real chunksize is up to 256, logical chunksize is 256 */
hailfinger71e1bd42010-10-13 22:26:56 +0000321int spi_chip_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger2c361e42008-05-13 23:03:12 +0000322{
hailfinger948b81f2009-07-22 15:36:50 +0000323 if (!spi_programmer[spi_controller].write_256) {
hailfingercb0564e2010-06-20 10:39:33 +0000324 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__);
hailfinger948b81f2009-07-22 15:36:50 +0000327 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000328 }
329
hailfingerc7d06c62010-07-14 16:19:05 +0000330 return spi_programmer[spi_controller].write_256(flash, buf, start, len);
331}
332
hailfingerb767c122010-05-28 15:53:08 +0000333/*
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 */
hailfinger54c14662009-05-13 11:40:08 +0000338uint32_t spi_get_valid_read_addr(void)
339{
hailfingerb767c122010-05-28 15:53:08 +0000340 switch (spi_controller) {
hailfinger90c7d542010-05-31 15:27:27 +0000341#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000342#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 }
hailfinger54c14662009-05-13 11:40:08 +0000351}