blob: 25b292628e57ebaff5563dabd8d43beaade3a735 [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
Souvik Ghoshd75cd672016-06-17 14:21:39 -070033int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +000034 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +000035{
Souvik Ghosh63b92f92016-06-29 18:45:52 -070036 return flash->pgm->spi.command(flash, writecnt, readcnt, writearr,
37 readarr);
hailfinger35cc8162007-10-16 21:09:06 +000038}
39
Souvik Ghoshd75cd672016-06-17 14:21:39 -070040int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +000041{
Souvik Ghosh63b92f92016-06-29 18:45:52 -070042 return flash->pgm->spi.multicommand(flash, cmds);
hailfinger948b81f2009-07-22 15:36:50 +000043}
44
Souvik Ghoshd75cd672016-06-17 14:21:39 -070045int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger948b81f2009-07-22 15:36:50 +000046 const unsigned char *writearr, unsigned char *readarr)
47{
48 struct spi_command cmd[] = {
49 {
50 .writecnt = writecnt,
51 .readcnt = readcnt,
52 .writearr = writearr,
53 .readarr = readarr,
54 }, {
55 .writecnt = 0,
56 .writearr = NULL,
57 .readcnt = 0,
58 .readarr = NULL,
59 }};
60
Souvik Ghoshd75cd672016-06-17 14:21:39 -070061 return spi_send_multicommand(flash, cmd);
hailfinger948b81f2009-07-22 15:36:50 +000062}
63
Souvik Ghoshd75cd672016-06-17 14:21:39 -070064int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +000065{
66 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +000067 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070068 result = spi_send_command(flash, cmds->writecnt, cmds->readcnt,
hailfingerbb092112009-09-18 15:50:56 +000069 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +000070 }
71 return result;
hailfinger68002c22009-07-10 21:08:55 +000072}
73
Souvik Ghoshd75cd672016-06-17 14:21:39 -070074int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
mkarcher8fb57592011-05-11 17:07:02 +000075{
Souvik Ghosh63b92f92016-06-29 18:45:52 -070076 unsigned int max_data = flash->pgm->spi.max_data_read;
David Hendricks1ed1d352011-11-23 17:54:37 -080077 int rc;
mkarcher8fb57592011-05-11 17:07:02 +000078 if (max_data == MAX_DATA_UNSPECIFIED) {
79 msg_perr("%s called, but SPI read chunk size not defined "
80 "on this hardware. Please report a bug at "
81 "flashrom@flashrom.org\n", __func__);
82 return 1;
83 }
Duncan Laurie06ffd522015-10-26 12:56:08 -070084 if (flash->feature_bits & FEATURE_UNBOUND_READ)
85 rc = spi_read_unbound(flash, buf, start, len, max_data);
86 else
87 rc = spi_read_chunked(flash, buf, start, len, max_data);
David Hendricks1ed1d352011-11-23 17:54:37 -080088 /* translate SPI-specific access denied error to generic error */
89 if (rc == SPI_ACCESS_DENIED)
90 rc = ACCESS_DENIED;
91 return rc;
mkarcher8fb57592011-05-11 17:07:02 +000092}
93
Souvik Ghoshd75cd672016-06-17 14:21:39 -070094int default_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
mkarcher8fb57592011-05-11 17:07:02 +000095{
Souvik Ghosh63b92f92016-06-29 18:45:52 -070096 unsigned int max_data = flash->pgm->spi.max_data_write;
David Hendricks1ed1d352011-11-23 17:54:37 -080097 int rc;
mkarcher8fb57592011-05-11 17:07:02 +000098 if (max_data == MAX_DATA_UNSPECIFIED) {
99 msg_perr("%s called, but SPI write chunk size not defined "
100 "on this hardware. Please report a bug at "
101 "flashrom@flashrom.org\n", __func__);
102 return 1;
103 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800104 rc = spi_write_chunked(flash, buf, start, len, max_data);
105 /* translate SPI-specific access denied error to generic error */
106 if (rc == SPI_ACCESS_DENIED)
107 rc = ACCESS_DENIED;
108 return rc;
mkarcher8fb57592011-05-11 17:07:02 +0000109}
110
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700111int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000112{
stefanctc5eb8a92011-11-23 09:13:48 +0000113 unsigned int addrbase = 0;
hailfinger132df7b2010-09-15 00:13:02 +0000114 /* Check if the chip fits between lowest valid and highest possible
115 * address. Highest possible address with the current SPI implementation
116 * means 0xffffff, the highest unsigned 24bit number.
117 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700118 addrbase = spi_get_valid_read_addr(flash);
hailfinger132df7b2010-09-15 00:13:02 +0000119 if (addrbase + flash->total_size * 1024 > (1 << 24)) {
120 msg_perr("Flash chip size exceeds the allowed access window. ");
121 msg_perr("Read will probably fail.\n");
122 /* Try to get the best alignment subject to constraints. */
123 addrbase = (1 << 24) - flash->total_size * 1024;
124 }
125 /* Check if alignment is native (at least the largest power of two which
126 * is a factor of the mapped size of the chip).
127 */
128 if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) {
129 msg_perr("Flash chip is not aligned natively in the allowed "
130 "access window.\n");
131 msg_perr("Read will probably return garbage.\n");
132 }
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700133 return flash->pgm->spi.read(flash, buf, addrbase + start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000134}
135
hailfingered063f52009-05-09 02:30:21 +0000136/*
hailfingered063f52009-05-09 02:30:21 +0000137 * Program chip using page (256 bytes) programming.
138 * Some SPI masters can't do this, they use single byte programming instead.
hailfingerc7d06c62010-07-14 16:19:05 +0000139 * The redirect to single byte programming is achieved by setting
140 * .write_256 = spi_chip_write_1
hailfingered063f52009-05-09 02:30:21 +0000141 */
hailfingerc7d06c62010-07-14 16:19:05 +0000142/* real chunksize is up to 256, logical chunksize is 256 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700143int spi_chip_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfinger2c361e42008-05-13 23:03:12 +0000144{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700145 return flash->pgm->spi.write_256(flash, buf, start, len);
hailfingerc7d06c62010-07-14 16:19:05 +0000146}
147
hailfingerb767c122010-05-28 15:53:08 +0000148/*
149 * Get the lowest allowed address for read accesses. This often happens to
150 * be the lowest allowed address for all commands which take an address.
151 * This is a programmer limitation.
152 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700153uint32_t spi_get_valid_read_addr(struct flashctx *flash)
hailfinger54c14662009-05-13 11:40:08 +0000154{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700155 switch (flash->pgm->spi.type) {
hailfinger90c7d542010-05-31 15:27:27 +0000156#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000157#if defined(__i386__) || defined(__x86_64__)
158 case SPI_CONTROLLER_ICH7:
159 /* Return BBAR for ICH chipsets. */
160 return ichspi_bbar;
161#endif
162#endif
163 default:
164 return 0;
165 }
hailfinger54c14662009-05-13 11:40:08 +0000166}
mkarcherd264e9e2011-05-11 17:07:07 +0000167
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700168int register_spi_programmer(const struct spi_programmer *pgm)
mkarcherd264e9e2011-05-11 17:07:07 +0000169{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700170 struct registered_programmer rpgm;
171
172 if (!pgm->write_256 || !pgm->read || !pgm->command ||
173 !pgm->multicommand ||
174 ((pgm->command == default_spi_send_command) &&
175 (pgm->multicommand == default_spi_send_multicommand))) {
176 msg_perr("%s called with incomplete programmer definition. "
177 "Please report a bug at flashrom@flashrom.org\n",
178 __func__);
179 return ERROR_FLASHROM_BUG;
180 }
181
182 rpgm.buses_supported = BUS_SPI;
183 rpgm.spi = *pgm;
184 return register_programmer(&rpgm);
David Hendricks91040832011-07-08 20:01:09 -0700185}