blob: 8196212eec468d4fc2cf122ecdbc567c2b596a1b [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
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010033const struct spi_master spi_master_none = {
David Hendricksac1d25c2016-08-09 17:00:58 -070034 .type = SPI_CONTROLLER_NONE,
35 .max_data_read = MAX_DATA_UNSPECIFIED,
36 .max_data_write = MAX_DATA_UNSPECIFIED,
37 .command = NULL,
38 .multicommand = NULL,
39 .read = NULL,
40 .write_256 = NULL,
41};
42
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010043const struct spi_master *spi_master = &spi_master_none;
David Hendricksac1d25c2016-08-09 17:00:58 -070044
Souvik Ghoshd75cd672016-06-17 14:21:39 -070045int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +000046 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +000047{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010048 if (!spi_master->command) {
David Hendricksac1d25c2016-08-09 17:00:58 -070049 msg_perr("%s called, but SPI is unsupported on this "
50 "hardware. Please report a bug at "
51 "flashrom@flashrom.org\n", __func__);
52 return 1;
53 }
54
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010055 return spi_master->command(flash, writecnt, readcnt,
David Hendricksac1d25c2016-08-09 17:00:58 -070056 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +000057}
58
Souvik Ghoshd75cd672016-06-17 14:21:39 -070059int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +000060{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010061 if (!spi_master->multicommand) {
David Hendricksac1d25c2016-08-09 17:00:58 -070062 msg_perr("%s called, but SPI is unsupported on this "
63 "hardware. Please report a bug at "
64 "flashrom@flashrom.org\n", __func__);
65 return 1;
66 }
67
Patrick Georgif4f1e2f2017-03-10 17:38:40 +010068 return spi_master->multicommand(flash, cmds);
hailfinger948b81f2009-07-22 15:36:50 +000069}
70
Souvik Ghoshd75cd672016-06-17 14:21:39 -070071int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger948b81f2009-07-22 15:36:50 +000072 const unsigned char *writearr, unsigned char *readarr)
73{
74 struct spi_command cmd[] = {
75 {
76 .writecnt = writecnt,
77 .readcnt = readcnt,
78 .writearr = writearr,
79 .readarr = readarr,
80 }, {
81 .writecnt = 0,
82 .writearr = NULL,
83 .readcnt = 0,
84 .readarr = NULL,
85 }};
86
Souvik Ghoshd75cd672016-06-17 14:21:39 -070087 return spi_send_multicommand(flash, cmd);
hailfinger948b81f2009-07-22 15:36:50 +000088}
89
Souvik Ghoshd75cd672016-06-17 14:21:39 -070090int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +000091{
92 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +000093 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070094 result = spi_send_command(flash, cmds->writecnt, cmds->readcnt,
hailfingerbb092112009-09-18 15:50:56 +000095 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +000096 }
97 return result;
hailfinger68002c22009-07-10 21:08:55 +000098}
99
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700100int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
mkarcher8fb57592011-05-11 17:07:02 +0000101{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100102 unsigned int max_data = spi_master->max_data_read;
David Hendricks1ed1d352011-11-23 17:54:37 -0800103 int rc;
mkarcher8fb57592011-05-11 17:07:02 +0000104 if (max_data == MAX_DATA_UNSPECIFIED) {
105 msg_perr("%s called, but SPI read chunk size not defined "
106 "on this hardware. Please report a bug at "
107 "flashrom@flashrom.org\n", __func__);
108 return 1;
109 }
Patrick Georgif3fa2992017-02-02 16:24:44 +0100110 if (flash->chip->feature_bits & FEATURE_UNBOUND_READ)
Duncan Laurie06ffd522015-10-26 12:56:08 -0700111 rc = spi_read_unbound(flash, buf, start, len, max_data);
112 else
113 rc = spi_read_chunked(flash, buf, start, len, max_data);
David Hendricks1ed1d352011-11-23 17:54:37 -0800114 /* translate SPI-specific access denied error to generic error */
115 if (rc == SPI_ACCESS_DENIED)
116 rc = ACCESS_DENIED;
117 return rc;
mkarcher8fb57592011-05-11 17:07:02 +0000118}
119
Patrick Georgiab8353e2017-02-03 18:32:01 +0100120int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
mkarcher8fb57592011-05-11 17:07:02 +0000121{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100122 unsigned int max_data = spi_master->max_data_write;
David Hendricks1ed1d352011-11-23 17:54:37 -0800123 int rc;
mkarcher8fb57592011-05-11 17:07:02 +0000124 if (max_data == MAX_DATA_UNSPECIFIED) {
125 msg_perr("%s called, but SPI write chunk size not defined "
126 "on this hardware. Please report a bug at "
127 "flashrom@flashrom.org\n", __func__);
128 return 1;
129 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800130 rc = spi_write_chunked(flash, buf, start, len, max_data);
131 /* translate SPI-specific access denied error to generic error */
132 if (rc == SPI_ACCESS_DENIED)
133 rc = ACCESS_DENIED;
134 return rc;
mkarcher8fb57592011-05-11 17:07:02 +0000135}
136
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700137int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000138{
stefanctc5eb8a92011-11-23 09:13:48 +0000139 unsigned int addrbase = 0;
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100140 if (!spi_master->read) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700141 msg_perr("%s called, but SPI read is unsupported on this "
142 "hardware. Please report a bug at "
143 "flashrom@flashrom.org\n", __func__);
144 return 1;
145 }
146
hailfinger132df7b2010-09-15 00:13:02 +0000147 /* Check if the chip fits between lowest valid and highest possible
148 * address. Highest possible address with the current SPI implementation
149 * means 0xffffff, the highest unsigned 24bit number.
150 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700151 addrbase = spi_get_valid_read_addr(flash);
Patrick Georgif3fa2992017-02-02 16:24:44 +0100152 if (addrbase + flash->chip->total_size * 1024 > (1 << 24)) {
hailfinger132df7b2010-09-15 00:13:02 +0000153 msg_perr("Flash chip size exceeds the allowed access window. ");
154 msg_perr("Read will probably fail.\n");
155 /* Try to get the best alignment subject to constraints. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100156 addrbase = (1 << 24) - flash->chip->total_size * 1024;
hailfinger132df7b2010-09-15 00:13:02 +0000157 }
158 /* Check if alignment is native (at least the largest power of two which
159 * is a factor of the mapped size of the chip).
160 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100161 if (ffs(flash->chip->total_size * 1024) > (ffs(addrbase) ? : 33)) {
hailfinger132df7b2010-09-15 00:13:02 +0000162 msg_perr("Flash chip is not aligned natively in the allowed "
163 "access window.\n");
164 msg_perr("Read will probably return garbage.\n");
165 }
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100166 return spi_master->read(flash, buf, addrbase + start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000167}
168
hailfingered063f52009-05-09 02:30:21 +0000169/*
hailfingered063f52009-05-09 02:30:21 +0000170 * Program chip using page (256 bytes) programming.
171 * Some SPI masters can't do this, they use single byte programming instead.
hailfingerc7d06c62010-07-14 16:19:05 +0000172 * The redirect to single byte programming is achieved by setting
173 * .write_256 = spi_chip_write_1
hailfingered063f52009-05-09 02:30:21 +0000174 */
hailfingerc7d06c62010-07-14 16:19:05 +0000175/* real chunksize is up to 256, logical chunksize is 256 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100176int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfinger2c361e42008-05-13 23:03:12 +0000177{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100178 if (!spi_master->write_256) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700179 msg_perr("%s called, but SPI page write is unsupported on this "
180 "hardware. Please report a bug at "
181 "flashrom@flashrom.org\n", __func__);
182 return 1;
183 }
184
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100185 return spi_master->write_256(flash, buf, start, len);
hailfingerc7d06c62010-07-14 16:19:05 +0000186}
187
hailfingerb767c122010-05-28 15:53:08 +0000188/*
189 * Get the lowest allowed address for read accesses. This often happens to
190 * be the lowest allowed address for all commands which take an address.
191 * This is a programmer limitation.
192 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700193uint32_t spi_get_valid_read_addr(struct flashctx *flash)
hailfinger54c14662009-05-13 11:40:08 +0000194{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100195 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000196#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000197#if defined(__i386__) || defined(__x86_64__)
198 case SPI_CONTROLLER_ICH7:
199 /* Return BBAR for ICH chipsets. */
200 return ichspi_bbar;
201#endif
202#endif
203 default:
204 return 0;
205 }
hailfinger54c14662009-05-13 11:40:08 +0000206}
mkarcherd264e9e2011-05-11 17:07:07 +0000207
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100208void register_spi_master(const struct spi_master *pgm)
mkarcherd264e9e2011-05-11 17:07:07 +0000209{
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100210 spi_master = pgm;
David Hendricksac1d25c2016-08-09 17:00:58 -0700211 buses_supported |= BUS_SPI;
David Hendricks91040832011-07-08 20:01:09 -0700212}