blob: a53aa7f584c294549de070a887bffa13e928d908 [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
David Hendricksc801adb2010-12-09 16:58:56 -080025#include <string.h>
stepand4b13752007-10-15 21:45:29 +000026#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000027#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000028#include "chipdrivers.h"
David Hendricks82fd8ae2010-08-04 14:34:54 -070029#include "programmer.h"
hailfinger78031562008-05-13 14:58:23 +000030#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000031
hailfinger40167462009-05-31 17:57:34 +000032enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
stepand4b13752007-10-15 21:45:29 +000033
hailfinger948b81f2009-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
hailfinger90c7d542010-05-31 15:27:27 +000042#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +000043#if defined(__i386__) || defined(__x86_64__)
hailfinger948b81f2009-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
David Hendricks52c18be2010-08-16 18:13:59 -070058 { /* SPI_CONTROLLER_IT85XX */
59 .command = it85xx_spi_send_command,
60 .multicommand = default_spi_send_multicommand,
61 .read = ich_spi_read,
62 .write_256 = ich_spi_write_256,
63 },
64
hailfinger948b81f2009-07-22 15:36:50 +000065 { /* SPI_CONTROLLER_IT87XX */
66 .command = it8716f_spi_send_command,
67 .multicommand = default_spi_send_multicommand,
68 .read = it8716f_spi_chip_read,
69 .write_256 = it8716f_spi_chip_write_256,
70 },
71
David Hendricks46d32e32011-01-19 16:01:52 -080072 { /* SPI_CONTROLLER_MEC1308 */
73 .command = mec1308_spi_send_command,
74 .multicommand = default_spi_send_multicommand,
75 .read = mec1308_spi_read,
76 .write_256 = mec1308_spi_write_256,
77 },
78
hailfinger948b81f2009-07-22 15:36:50 +000079 { /* SPI_CONTROLLER_SB600 */
80 .command = sb600_spi_send_command,
81 .multicommand = default_spi_send_multicommand,
82 .read = sb600_spi_read,
David Hendricks82fd8ae2010-08-04 14:34:54 -070083 .write_256 = sb600_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +000084 },
85
86 { /* SPI_CONTROLLER_VIA */
87 .command = ich_spi_send_command,
88 .multicommand = ich_spi_send_multicommand,
89 .read = ich_spi_read,
90 .write_256 = ich_spi_write_256,
91 },
92
93 { /* SPI_CONTROLLER_WBSIO */
94 .command = wbsio_spi_send_command,
95 .multicommand = default_spi_send_multicommand,
96 .read = wbsio_spi_read,
David Hendricksc801adb2010-12-09 16:58:56 -080097 .write_256 = spi_chip_write_1,
David Hendricks82fd8ae2010-08-04 14:34:54 -070098 },
99
100 { /* SPI_CONTROLLER_MCP6X_BITBANG */
101 .command = bitbang_spi_send_command,
102 .multicommand = default_spi_send_multicommand,
103 .read = bitbang_spi_read,
104 .write_256 = bitbang_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +0000105 },
David Hendricksc801adb2010-12-09 16:58:56 -0800106
107 { /* SPI_CONTROLLER_WPCE775X */
108 .command = wpce775x_spi_send_command,
109 .multicommand = default_spi_send_multicommand,
110 .read = wpce775x_spi_read,
111 .write_256 = wpce775x_spi_write_256,
112 },
113
hailfinger80422e22009-12-13 22:28:00 +0000114#endif
Louis Yung-Chieh Lobc351d02011-03-31 13:09:21 +0800115#if defined(__arm__)
116 { /* SPI_CONTROLLER_TEGRA2 */
117 .command = tegra2_spi_send_command,
118 .multicommand = default_spi_send_multicommand,
119 .read = tegra2_spi_read,
120 .write_256 = tegra2_spi_write,
121 },
122#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000123#endif
hailfinger948b81f2009-07-22 15:36:50 +0000124
hailfinger90c7d542010-05-31 15:27:27 +0000125#if CONFIG_FT2232_SPI == 1
hailfinger948b81f2009-07-22 15:36:50 +0000126 { /* SPI_CONTROLLER_FT2232 */
127 .command = ft2232_spi_send_command,
128 .multicommand = default_spi_send_multicommand,
129 .read = ft2232_spi_read,
130 .write_256 = ft2232_spi_write_256,
131 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000132#endif
hailfinger948b81f2009-07-22 15:36:50 +0000133
hailfinger90c7d542010-05-31 15:27:27 +0000134#if CONFIG_DUMMY == 1
hailfinger948b81f2009-07-22 15:36:50 +0000135 { /* SPI_CONTROLLER_DUMMY */
136 .command = dummy_spi_send_command,
137 .multicommand = default_spi_send_multicommand,
hailfingera8727712010-06-20 10:58:32 +0000138 .read = dummy_spi_read,
David Hendricks82fd8ae2010-08-04 14:34:54 -0700139 .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 */
145 .command = buspirate_spi_send_command,
146 .multicommand = default_spi_send_multicommand,
147 .read = buspirate_spi_read,
hailfinger8b82a422010-03-22 03:30:58 +0000148 .write_256 = buspirate_spi_write_256,
hailfinger9c5add72009-11-24 00:20:03 +0000149 },
150#endif
151
hailfinger90c7d542010-05-31 15:27:27 +0000152#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000153 { /* SPI_CONTROLLER_DEDIPROG */
154 .command = dediprog_spi_send_command,
155 .multicommand = default_spi_send_multicommand,
156 .read = dediprog_spi_read,
David Hendricksc801adb2010-12-09 16:58:56 -0800157 .write_256 = dediprog_spi_write_256,
David Hendricks82fd8ae2010-08-04 14:34:54 -0700158 },
159#endif
160
161#if CONFIG_RAYER_SPI == 1
162 { /* SPI_CONTROLLER_RAYER */
163 .command = bitbang_spi_send_command,
164 .multicommand = default_spi_send_multicommand,
165 .read = bitbang_spi_read,
166 .write_256 = bitbang_spi_write_256,
hailfingerdfb32a02010-01-19 11:15:48 +0000167 },
168#endif
169
David Hendricksc801adb2010-12-09 16:58:56 -0800170#if CONFIG_NICINTEL_SPI == 1
171 { /* SPI_CONTROLLER_NICINTEL */
172 .command = bitbang_spi_send_command,
173 .multicommand = default_spi_send_multicommand,
174 .read = bitbang_spi_read,
175 .write_256 = bitbang_spi_write_256,
176 },
177#endif
178
David Hendricks668f29d2011-01-27 18:51:45 -0800179#if CONFIG_OGP_SPI == 1
180 { /* SPI_CONTROLLER_OGP */
181 .command = bitbang_spi_send_command,
182 .multicommand = default_spi_send_multicommand,
183 .read = bitbang_spi_read,
184 .write_256 = bitbang_spi_write_256,
185 },
186#endif
187
hailfingerd9dcfbd2009-08-19 13:27:58 +0000188 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
hailfinger948b81f2009-07-22 15:36:50 +0000189};
190
hailfingerd9dcfbd2009-08-19 13:27:58 +0000191const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
hailfinger948b81f2009-07-22 15:36:50 +0000192
hailfinger68002c22009-07-10 21:08:55 +0000193int spi_send_command(unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000194 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +0000195{
hailfinger948b81f2009-07-22 15:36:50 +0000196 if (!spi_programmer[spi_controller].command) {
snelsone42c3802010-05-07 20:09:04 +0000197 msg_perr("%s called, but SPI is unsupported on this "
hailfingercb0564e2010-06-20 10:39:33 +0000198 "hardware. Please report a bug at "
199 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000200 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000201 }
hailfinger948b81f2009-07-22 15:36:50 +0000202
203 return spi_programmer[spi_controller].command(writecnt, readcnt,
204 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +0000205}
206
hailfingerbb092112009-09-18 15:50:56 +0000207int spi_send_multicommand(struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +0000208{
hailfinger948b81f2009-07-22 15:36:50 +0000209 if (!spi_programmer[spi_controller].multicommand) {
snelsone42c3802010-05-07 20:09:04 +0000210 msg_perr("%s called, but SPI is unsupported on this "
hailfingercb0564e2010-06-20 10:39:33 +0000211 "hardware. Please report a bug at "
212 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000213 return 1;
hailfinger68002c22009-07-10 21:08:55 +0000214 }
hailfinger948b81f2009-07-22 15:36:50 +0000215
hailfingerbb092112009-09-18 15:50:56 +0000216 return spi_programmer[spi_controller].multicommand(cmds);
hailfinger948b81f2009-07-22 15:36:50 +0000217}
218
219int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
220 const unsigned char *writearr, unsigned char *readarr)
221{
222 struct spi_command cmd[] = {
223 {
224 .writecnt = writecnt,
225 .readcnt = readcnt,
226 .writearr = writearr,
227 .readarr = readarr,
228 }, {
229 .writecnt = 0,
230 .writearr = NULL,
231 .readcnt = 0,
232 .readarr = NULL,
233 }};
234
235 return spi_send_multicommand(cmd);
236}
237
hailfingerbb092112009-09-18 15:50:56 +0000238int default_spi_send_multicommand(struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +0000239{
240 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +0000241 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
242 result = spi_send_command(cmds->writecnt, cmds->readcnt,
243 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +0000244 }
245 return result;
hailfinger68002c22009-07-10 21:08:55 +0000246}
247
hailfinger0f08b7a2009-06-16 08:55:44 +0000248int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000249{
David Hendricksc801adb2010-12-09 16:58:56 -0800250 int addrbase = 0;
hailfinger948b81f2009-07-22 15:36:50 +0000251 if (!spi_programmer[spi_controller].read) {
hailfingercb0564e2010-06-20 10:39:33 +0000252 msg_perr("%s called, but SPI read is unsupported on this "
253 "hardware. Please report a bug at "
254 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000255 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000256 }
257
David Hendricksc801adb2010-12-09 16:58:56 -0800258 /* Check if the chip fits between lowest valid and highest possible
259 * address. Highest possible address with the current SPI implementation
260 * means 0xffffff, the highest unsigned 24bit number.
261 */
262 addrbase = spi_get_valid_read_addr();
263 if (addrbase + flash->total_size * 1024 > (1 << 24)) {
264 msg_perr("Flash chip size exceeds the allowed access window. ");
265 msg_perr("Read will probably fail.\n");
266 /* Try to get the best alignment subject to constraints. */
267 addrbase = (1 << 24) - flash->total_size * 1024;
268 }
269 /* Check if alignment is native (at least the largest power of two which
270 * is a factor of the mapped size of the chip).
271 */
272 if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) {
273 msg_perr("Flash chip is not aligned natively in the allowed "
274 "access window.\n");
275 msg_perr("Read will probably return garbage.\n");
276 }
277 return spi_programmer[spi_controller].read(flash, buf, addrbase + start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000278}
279
hailfingered063f52009-05-09 02:30:21 +0000280/*
hailfingered063f52009-05-09 02:30:21 +0000281 * Program chip using page (256 bytes) programming.
282 * Some SPI masters can't do this, they use single byte programming instead.
David Hendricks82fd8ae2010-08-04 14:34:54 -0700283 * The redirect to single byte programming is achieved by setting
284 * .write_256 = spi_chip_write_1
hailfingered063f52009-05-09 02:30:21 +0000285 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700286/* real chunksize is up to 256, logical chunksize is 256 */
David Hendricksc801adb2010-12-09 16:58:56 -0800287int spi_chip_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger2c361e42008-05-13 23:03:12 +0000288{
hailfinger948b81f2009-07-22 15:36:50 +0000289 if (!spi_programmer[spi_controller].write_256) {
hailfingercb0564e2010-06-20 10:39:33 +0000290 msg_perr("%s called, but SPI page write is unsupported on this "
291 "hardware. Please report a bug at "
292 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000293 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000294 }
295
David Hendricks82fd8ae2010-08-04 14:34:54 -0700296 return spi_programmer[spi_controller].write_256(flash, buf, start, len);
297}
298
hailfingerb767c122010-05-28 15:53:08 +0000299/*
300 * Get the lowest allowed address for read accesses. This often happens to
301 * be the lowest allowed address for all commands which take an address.
302 * This is a programmer limitation.
303 */
hailfinger54c14662009-05-13 11:40:08 +0000304uint32_t spi_get_valid_read_addr(void)
305{
hailfingerb767c122010-05-28 15:53:08 +0000306 switch (spi_controller) {
hailfinger90c7d542010-05-31 15:27:27 +0000307#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000308#if defined(__i386__) || defined(__x86_64__)
309 case SPI_CONTROLLER_ICH7:
310 /* Return BBAR for ICH chipsets. */
311 return ichspi_bbar;
312#endif
313#endif
314 default:
315 return 0;
316 }
hailfinger54c14662009-05-13 11:40:08 +0000317}