blob: b005cdc403c6c0c67b176b4d906eb49263a04871 [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
stepand4b13752007-10-15 21:45:29 +000025#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000026#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000027#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000028#include "programmer.h"
hailfinger78031562008-05-13 14:58:23 +000029#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000030
hailfinger40167462009-05-31 17:57:34 +000031enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
hailfinger40167462009-05-31 17:57:34 +000032
hailfingerb8f7e882008-01-19 00:04:46 +000033void spi_prettyprint_status_register(struct flashchip *flash);
stepand4b13752007-10-15 21:45:29 +000034
hailfinger948b81f2009-07-22 15:36:50 +000035const struct spi_programmer spi_programmer[] = {
36 { /* SPI_CONTROLLER_NONE */
37 .command = NULL,
38 .multicommand = NULL,
39 .read = NULL,
40 .write_256 = NULL,
41 },
42
hailfinger90c7d542010-05-31 15:27:27 +000043#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +000044#if defined(__i386__) || defined(__x86_64__)
hailfinger948b81f2009-07-22 15:36:50 +000045 { /* SPI_CONTROLLER_ICH7 */
46 .command = ich_spi_send_command,
47 .multicommand = ich_spi_send_multicommand,
48 .read = ich_spi_read,
49 .write_256 = ich_spi_write_256,
50 },
51
52 { /* SPI_CONTROLLER_ICH9 */
53 .command = ich_spi_send_command,
54 .multicommand = ich_spi_send_multicommand,
55 .read = ich_spi_read,
56 .write_256 = ich_spi_write_256,
57 },
58
59 { /* SPI_CONTROLLER_IT87XX */
60 .command = it8716f_spi_send_command,
61 .multicommand = default_spi_send_multicommand,
62 .read = it8716f_spi_chip_read,
63 .write_256 = it8716f_spi_chip_write_256,
64 },
65
66 { /* SPI_CONTROLLER_SB600 */
67 .command = sb600_spi_send_command,
68 .multicommand = default_spi_send_multicommand,
69 .read = sb600_spi_read,
hailfingerc7d06c62010-07-14 16:19:05 +000070 .write_256 = sb600_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +000071 },
72
73 { /* SPI_CONTROLLER_VIA */
74 .command = ich_spi_send_command,
75 .multicommand = ich_spi_send_multicommand,
76 .read = ich_spi_read,
77 .write_256 = ich_spi_write_256,
78 },
79
80 { /* SPI_CONTROLLER_WBSIO */
81 .command = wbsio_spi_send_command,
82 .multicommand = default_spi_send_multicommand,
83 .read = wbsio_spi_read,
hailfingerd91c81f2010-07-14 19:57:52 +000084 .write_256 = spi_chip_write_1_new,
hailfinger948b81f2009-07-22 15:36:50 +000085 },
hailfinger52384c92010-07-28 15:08:35 +000086
87 { /* SPI_CONTROLLER_MCP6X_BITBANG */
88 .command = bitbang_spi_send_command,
89 .multicommand = default_spi_send_multicommand,
90 .read = bitbang_spi_read,
91 .write_256 = bitbang_spi_write_256,
92 },
hailfinger80422e22009-12-13 22:28:00 +000093#endif
hailfinger324a9cc2010-05-26 01:45:41 +000094#endif
hailfinger948b81f2009-07-22 15:36:50 +000095
hailfinger90c7d542010-05-31 15:27:27 +000096#if CONFIG_FT2232_SPI == 1
hailfinger948b81f2009-07-22 15:36:50 +000097 { /* SPI_CONTROLLER_FT2232 */
98 .command = ft2232_spi_send_command,
99 .multicommand = default_spi_send_multicommand,
100 .read = ft2232_spi_read,
101 .write_256 = ft2232_spi_write_256,
102 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000103#endif
hailfinger948b81f2009-07-22 15:36:50 +0000104
hailfinger90c7d542010-05-31 15:27:27 +0000105#if CONFIG_DUMMY == 1
hailfinger948b81f2009-07-22 15:36:50 +0000106 { /* SPI_CONTROLLER_DUMMY */
107 .command = dummy_spi_send_command,
108 .multicommand = default_spi_send_multicommand,
hailfingera8727712010-06-20 10:58:32 +0000109 .read = dummy_spi_read,
hailfingerc7d06c62010-07-14 16:19:05 +0000110 .write_256 = dummy_spi_write_256,
hailfinger948b81f2009-07-22 15:36:50 +0000111 },
hailfinger571a6b32009-09-16 10:09:21 +0000112#endif
hailfingerd9dcfbd2009-08-19 13:27:58 +0000113
hailfinger90c7d542010-05-31 15:27:27 +0000114#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000115 { /* SPI_CONTROLLER_BUSPIRATE */
116 .command = buspirate_spi_send_command,
117 .multicommand = default_spi_send_multicommand,
118 .read = buspirate_spi_read,
hailfinger8b82a422010-03-22 03:30:58 +0000119 .write_256 = buspirate_spi_write_256,
hailfinger9c5add72009-11-24 00:20:03 +0000120 },
121#endif
122
hailfinger90c7d542010-05-31 15:27:27 +0000123#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000124 { /* SPI_CONTROLLER_DEDIPROG */
125 .command = dediprog_spi_send_command,
126 .multicommand = default_spi_send_multicommand,
127 .read = dediprog_spi_read,
hailfingerc7d06c62010-07-14 16:19:05 +0000128 .write_256 = spi_chip_write_1_new,
hailfingerdfb32a02010-01-19 11:15:48 +0000129 },
130#endif
131
hailfinger52c4fa02010-07-21 10:26:01 +0000132#if CONFIG_RAYER_SPI == 1
133 { /* SPI_CONTROLLER_RAYER */
134 .command = bitbang_spi_send_command,
135 .multicommand = default_spi_send_multicommand,
136 .read = bitbang_spi_read,
137 .write_256 = bitbang_spi_write_256,
138 },
139#endif
140
hailfingerd9dcfbd2009-08-19 13:27:58 +0000141 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
hailfinger948b81f2009-07-22 15:36:50 +0000142};
143
hailfingerd9dcfbd2009-08-19 13:27:58 +0000144const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
hailfinger948b81f2009-07-22 15:36:50 +0000145
hailfinger68002c22009-07-10 21:08:55 +0000146int spi_send_command(unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000147 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +0000148{
hailfinger948b81f2009-07-22 15:36:50 +0000149 if (!spi_programmer[spi_controller].command) {
snelsone42c3802010-05-07 20:09:04 +0000150 msg_perr("%s called, but SPI is unsupported on this "
hailfingercb0564e2010-06-20 10:39:33 +0000151 "hardware. Please report a bug at "
152 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000153 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000154 }
hailfinger948b81f2009-07-22 15:36:50 +0000155
156 return spi_programmer[spi_controller].command(writecnt, readcnt,
157 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +0000158}
159
hailfingerbb092112009-09-18 15:50:56 +0000160int spi_send_multicommand(struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +0000161{
hailfinger948b81f2009-07-22 15:36:50 +0000162 if (!spi_programmer[spi_controller].multicommand) {
snelsone42c3802010-05-07 20:09:04 +0000163 msg_perr("%s called, but SPI is unsupported on this "
hailfingercb0564e2010-06-20 10:39:33 +0000164 "hardware. Please report a bug at "
165 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000166 return 1;
hailfinger68002c22009-07-10 21:08:55 +0000167 }
hailfinger948b81f2009-07-22 15:36:50 +0000168
hailfingerbb092112009-09-18 15:50:56 +0000169 return spi_programmer[spi_controller].multicommand(cmds);
hailfinger948b81f2009-07-22 15:36:50 +0000170}
171
172int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
173 const unsigned char *writearr, unsigned char *readarr)
174{
175 struct spi_command cmd[] = {
176 {
177 .writecnt = writecnt,
178 .readcnt = readcnt,
179 .writearr = writearr,
180 .readarr = readarr,
181 }, {
182 .writecnt = 0,
183 .writearr = NULL,
184 .readcnt = 0,
185 .readarr = NULL,
186 }};
187
188 return spi_send_multicommand(cmd);
189}
190
hailfingerbb092112009-09-18 15:50:56 +0000191int default_spi_send_multicommand(struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +0000192{
193 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +0000194 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
195 result = spi_send_command(cmds->writecnt, cmds->readcnt,
196 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +0000197 }
198 return result;
hailfinger68002c22009-07-10 21:08:55 +0000199}
200
hailfinger0f08b7a2009-06-16 08:55:44 +0000201int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000202{
hailfinger948b81f2009-07-22 15:36:50 +0000203 if (!spi_programmer[spi_controller].read) {
hailfingercb0564e2010-06-20 10:39:33 +0000204 msg_perr("%s called, but SPI read is unsupported on this "
205 "hardware. Please report a bug at "
206 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000207 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000208 }
209
hailfinger948b81f2009-07-22 15:36:50 +0000210 return spi_programmer[spi_controller].read(flash, buf, start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000211}
212
hailfingered063f52009-05-09 02:30:21 +0000213/*
hailfingered063f52009-05-09 02:30:21 +0000214 * Program chip using page (256 bytes) programming.
215 * Some SPI masters can't do this, they use single byte programming instead.
hailfingerc7d06c62010-07-14 16:19:05 +0000216 * The redirect to single byte programming is achieved by setting
217 * .write_256 = spi_chip_write_1
hailfingered063f52009-05-09 02:30:21 +0000218 */
hailfingerc7d06c62010-07-14 16:19:05 +0000219/* real chunksize is up to 256, logical chunksize is 256 */
220int spi_chip_write_256_new(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger2c361e42008-05-13 23:03:12 +0000221{
hailfinger948b81f2009-07-22 15:36:50 +0000222 if (!spi_programmer[spi_controller].write_256) {
hailfingercb0564e2010-06-20 10:39:33 +0000223 msg_perr("%s called, but SPI page write is unsupported on this "
224 "hardware. Please report a bug at "
225 "flashrom@flashrom.org\n", __func__);
hailfinger948b81f2009-07-22 15:36:50 +0000226 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000227 }
228
hailfingerc7d06c62010-07-14 16:19:05 +0000229 return spi_programmer[spi_controller].write_256(flash, buf, start, len);
230}
231
232/* Wrapper function until the generic code is converted to partial writes. */
233int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
234{
235 int ret;
236
hailfingerc7d06c62010-07-14 16:19:05 +0000237 msg_pinfo("Erasing flash before programming... ");
238 if (erase_flash(flash)) {
239 msg_perr("ERASE FAILED!\n");
240 return -1;
241 }
242 msg_pinfo("done.\n");
243 msg_pinfo("Programming flash... ");
244 ret = spi_chip_write_256_new(flash, buf, 0, flash->total_size * 1024);
245 if (!ret)
246 msg_pinfo("done.\n");
247 else
248 msg_pinfo("\n");
249 return ret;
hailfingerf71c0ac2007-10-18 00:24:07 +0000250}
stuge712ce862009-01-26 03:37:40 +0000251
hailfingerb767c122010-05-28 15:53:08 +0000252/*
253 * Get the lowest allowed address for read accesses. This often happens to
254 * be the lowest allowed address for all commands which take an address.
255 * This is a programmer limitation.
256 */
hailfinger54c14662009-05-13 11:40:08 +0000257uint32_t spi_get_valid_read_addr(void)
258{
hailfingerb767c122010-05-28 15:53:08 +0000259 switch (spi_controller) {
hailfinger90c7d542010-05-31 15:27:27 +0000260#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000261#if defined(__i386__) || defined(__x86_64__)
262 case SPI_CONTROLLER_ICH7:
263 /* Return BBAR for ICH chipsets. */
264 return ichspi_bbar;
265#endif
266#endif
267 default:
268 return 0;
269 }
hailfinger54c14662009-05-13 11:40:08 +0000270}