blob: 7d761200c7403dcb404222f93205268a248a61df [file] [log] [blame]
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger3431bb72009-06-24 08:28:39 +00004 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
Stefan Reinauera9424d52008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
Carl-Daniel Hailfinger70539262007-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
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +000025#include <string.h>
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000026#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000027#include "flashchips.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000028#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000029#include "programmer.h"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000030#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000031
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000032enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
Carl-Daniel Hailfinger1dfe0ff2009-05-31 17:57:34 +000033
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000034const struct spi_programmer spi_programmer[] = {
35 { /* SPI_CONTROLLER_NONE */
36 .command = NULL,
37 .multicommand = NULL,
38 .read = NULL,
39 .write_256 = NULL,
40 },
41
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000042#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000043#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000044 { /* SPI_CONTROLLER_ICH7 */
45 .command = ich_spi_send_command,
46 .multicommand = ich_spi_send_multicommand,
47 .read = ich_spi_read,
48 .write_256 = ich_spi_write_256,
49 },
50
51 { /* SPI_CONTROLLER_ICH9 */
52 .command = ich_spi_send_command,
53 .multicommand = ich_spi_send_multicommand,
54 .read = ich_spi_read,
55 .write_256 = ich_spi_write_256,
56 },
57
58 { /* SPI_CONTROLLER_IT87XX */
59 .command = it8716f_spi_send_command,
60 .multicommand = default_spi_send_multicommand,
61 .read = it8716f_spi_chip_read,
62 .write_256 = it8716f_spi_chip_write_256,
63 },
64
65 { /* SPI_CONTROLLER_SB600 */
66 .command = sb600_spi_send_command,
67 .multicommand = default_spi_send_multicommand,
68 .read = sb600_spi_read,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +000069 .write_256 = sb600_spi_write_256,
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000070 },
71
72 { /* SPI_CONTROLLER_VIA */
73 .command = ich_spi_send_command,
74 .multicommand = ich_spi_send_multicommand,
75 .read = ich_spi_read,
76 .write_256 = ich_spi_write_256,
77 },
78
79 { /* SPI_CONTROLLER_WBSIO */
80 .command = wbsio_spi_send_command,
81 .multicommand = default_spi_send_multicommand,
82 .read = wbsio_spi_read,
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +000083 .write_256 = spi_chip_write_1,
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000084 },
Carl-Daniel Hailfinger2f436162010-07-28 15:08:35 +000085
86 { /* SPI_CONTROLLER_MCP6X_BITBANG */
87 .command = bitbang_spi_send_command,
88 .multicommand = default_spi_send_multicommand,
89 .read = bitbang_spi_read,
90 .write_256 = bitbang_spi_write_256,
91 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000092#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000093#endif
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000094
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000095#if CONFIG_FT2232_SPI == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000096 { /* SPI_CONTROLLER_FT2232 */
97 .command = ft2232_spi_send_command,
98 .multicommand = default_spi_send_multicommand,
99 .read = ft2232_spi_read,
100 .write_256 = ft2232_spi_write_256,
101 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000102#endif
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000103
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000104#if CONFIG_DUMMY == 1
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000105 { /* SPI_CONTROLLER_DUMMY */
106 .command = dummy_spi_send_command,
107 .multicommand = default_spi_send_multicommand,
Carl-Daniel Hailfinger1b0ba892010-06-20 10:58:32 +0000108 .read = dummy_spi_read,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000109 .write_256 = dummy_spi_write_256,
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000110 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000111#endif
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000112
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000113#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000114 { /* SPI_CONTROLLER_BUSPIRATE */
115 .command = buspirate_spi_send_command,
116 .multicommand = default_spi_send_multicommand,
117 .read = buspirate_spi_read,
Carl-Daniel Hailfinger408e47a2010-03-22 03:30:58 +0000118 .write_256 = buspirate_spi_write_256,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000119 },
120#endif
121
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000122#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000123 { /* SPI_CONTROLLER_DEDIPROG */
124 .command = dediprog_spi_send_command,
125 .multicommand = default_spi_send_multicommand,
126 .read = dediprog_spi_read,
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000127 .write_256 = dediprog_spi_write_256,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000128 },
129#endif
130
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000131#if CONFIG_RAYER_SPI == 1
132 { /* SPI_CONTROLLER_RAYER */
133 .command = bitbang_spi_send_command,
134 .multicommand = default_spi_send_multicommand,
135 .read = bitbang_spi_read,
136 .write_256 = bitbang_spi_write_256,
137 },
138#endif
139
Idwer Vollering004f4b72010-09-03 18:21:21 +0000140#if CONFIG_NICINTEL_SPI == 1
141 { /* SPI_CONTROLLER_NICINTEL */
142 .command = bitbang_spi_send_command,
143 .multicommand = default_spi_send_multicommand,
144 .read = bitbang_spi_read,
145 .write_256 = bitbang_spi_write_256,
146 },
147#endif
148
Mark Marshall90021f22010-12-03 14:48:11 +0000149#if CONFIG_OGP_SPI == 1
150 { /* SPI_CONTROLLER_OGP */
151 .command = bitbang_spi_send_command,
152 .multicommand = default_spi_send_multicommand,
153 .read = bitbang_spi_read,
154 .write_256 = bitbang_spi_write_256,
155 },
156#endif
157
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000158 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000159};
160
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000161const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000162
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000163int spi_send_command(unsigned int writecnt, unsigned int readcnt,
Uwe Hermann394131e2008-10-18 21:14:13 +0000164 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000165{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000166 if (!spi_programmer[spi_controller].command) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000167 msg_perr("%s called, but SPI is unsupported on this "
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000168 "hardware. Please report a bug at "
169 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000170 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000171 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000172
173 return spi_programmer[spi_controller].command(writecnt, readcnt,
174 writearr, readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +0000175}
176
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000177int spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000178{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000179 if (!spi_programmer[spi_controller].multicommand) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000180 msg_perr("%s called, but SPI is unsupported on this "
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000181 "hardware. Please report a bug at "
182 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000183 return 1;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000184 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000185
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000186 return spi_programmer[spi_controller].multicommand(cmds);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000187}
188
189int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
190 const unsigned char *writearr, unsigned char *readarr)
191{
192 struct spi_command cmd[] = {
193 {
194 .writecnt = writecnt,
195 .readcnt = readcnt,
196 .writearr = writearr,
197 .readarr = readarr,
198 }, {
199 .writecnt = 0,
200 .writearr = NULL,
201 .readcnt = 0,
202 .readarr = NULL,
203 }};
204
205 return spi_send_multicommand(cmd);
206}
207
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000208int default_spi_send_multicommand(struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000209{
210 int result = 0;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +0000211 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
212 result = spi_send_command(cmds->writecnt, cmds->readcnt,
213 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000214 }
215 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000216}
217
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000218int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000219{
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +0000220 int addrbase = 0;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000221 if (!spi_programmer[spi_controller].read) {
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000222 msg_perr("%s called, but SPI read is unsupported on this "
223 "hardware. Please report a bug at "
224 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000225 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000226 }
227
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +0000228 /* Check if the chip fits between lowest valid and highest possible
229 * address. Highest possible address with the current SPI implementation
230 * means 0xffffff, the highest unsigned 24bit number.
231 */
232 addrbase = spi_get_valid_read_addr();
233 if (addrbase + flash->total_size * 1024 > (1 << 24)) {
234 msg_perr("Flash chip size exceeds the allowed access window. ");
235 msg_perr("Read will probably fail.\n");
236 /* Try to get the best alignment subject to constraints. */
237 addrbase = (1 << 24) - flash->total_size * 1024;
238 }
239 /* Check if alignment is native (at least the largest power of two which
240 * is a factor of the mapped size of the chip).
241 */
242 if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) {
243 msg_perr("Flash chip is not aligned natively in the allowed "
244 "access window.\n");
245 msg_perr("Read will probably return garbage.\n");
246 }
247 return spi_programmer[spi_controller].read(flash, buf, addrbase + start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000248}
249
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000250/*
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000251 * Program chip using page (256 bytes) programming.
252 * Some SPI masters can't do this, they use single byte programming instead.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000253 * The redirect to single byte programming is achieved by setting
254 * .write_256 = spi_chip_write_1
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000255 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000256/* real chunksize is up to 256, logical chunksize is 256 */
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000257int spi_chip_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000258{
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000259 if (!spi_programmer[spi_controller].write_256) {
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000260 msg_perr("%s called, but SPI page write is unsupported on this "
261 "hardware. Please report a bug at "
262 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000263 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000264 }
265
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000266 return spi_programmer[spi_controller].write_256(flash, buf, start, len);
267}
268
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000269/*
270 * Get the lowest allowed address for read accesses. This often happens to
271 * be the lowest allowed address for all commands which take an address.
272 * This is a programmer limitation.
273 */
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000274uint32_t spi_get_valid_read_addr(void)
275{
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000276 switch (spi_controller) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000277#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000278#if defined(__i386__) || defined(__x86_64__)
279 case SPI_CONTROLLER_ICH7:
280 /* Return BBAR for ICH chipsets. */
281 return ichspi_bbar;
282#endif
283#endif
284 default:
285 return 0;
286 }
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000287}