blob: 02c83f7bfafca72c404bce95fc1ba87edbcd2f2f [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
Patrick Georgi97bc95c2011-03-08 07:17:44 +000025#include <strings.h>
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +000026#include <string.h>
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000027#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000028#include "flashchips.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000029#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000031#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000032
Michael Karcherb9dbe482011-05-11 17:07:07 +000033const struct spi_programmer spi_programmer_none = {
34 .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,
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000041};
42
Michael Karcherb9dbe482011-05-11 17:07:07 +000043const struct spi_programmer *spi_programmer = &spi_programmer_none;
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000044
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000045int spi_send_command(struct flashctx *flash, unsigned int writecnt,
46 unsigned int readcnt, const unsigned char *writearr,
47 unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000048{
Michael Karcherb9dbe482011-05-11 17:07:07 +000049 if (!spi_programmer->command) {
Sean Nelson316a29f2010-05-07 20:09:04 +000050 msg_perr("%s called, but SPI is unsupported on this "
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000051 "hardware. Please report a bug at "
52 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000053 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +000054 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000055
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000056 return spi_programmer->command(flash, writecnt, readcnt, writearr,
57 readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000058}
59
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000060int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds)
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000061{
Michael Karcherb9dbe482011-05-11 17:07:07 +000062 if (!spi_programmer->multicommand) {
Sean Nelson316a29f2010-05-07 20:09:04 +000063 msg_perr("%s called, but SPI is unsupported on this "
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000064 "hardware. Please report a bug at "
65 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000066 return 1;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000067 }
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000068
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000069 return spi_programmer->multicommand(flash, cmds);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000070}
71
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000072int default_spi_send_command(struct flashctx *flash, unsigned int writecnt,
73 unsigned int readcnt,
74 const unsigned char *writearr,
75 unsigned char *readarr)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000076{
77 struct spi_command cmd[] = {
78 {
79 .writecnt = writecnt,
80 .readcnt = readcnt,
81 .writearr = writearr,
82 .readarr = readarr,
83 }, {
84 .writecnt = 0,
85 .writearr = NULL,
86 .readcnt = 0,
87 .readarr = NULL,
88 }};
89
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000090 return spi_send_multicommand(flash, cmd);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000091}
92
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000093int default_spi_send_multicommand(struct flashctx *flash,
94 struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000095{
96 int result = 0;
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +000097 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000098 result = spi_send_command(flash, cmds->writecnt, cmds->readcnt,
Carl-Daniel Hailfinger26f7e642009-09-18 15:50:56 +000099 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000100 }
101 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000102}
103
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000104int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
105 unsigned int len)
Michael Karcher62797512011-05-11 17:07:02 +0000106{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000107 unsigned int max_data = spi_programmer->max_data_read;
Michael Karcher62797512011-05-11 17:07:02 +0000108 if (max_data == MAX_DATA_UNSPECIFIED) {
109 msg_perr("%s called, but SPI read chunk size not defined "
110 "on this hardware. Please report a bug at "
111 "flashrom@flashrom.org\n", __func__);
112 return 1;
113 }
114 return spi_read_chunked(flash, buf, start, len, max_data);
115}
116
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000117int default_spi_write_256(struct flashctx *flash, uint8_t *buf,
118 unsigned int start, unsigned int len)
Michael Karcher62797512011-05-11 17:07:02 +0000119{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000120 unsigned int max_data = spi_programmer->max_data_write;
Michael Karcher62797512011-05-11 17:07:02 +0000121 if (max_data == MAX_DATA_UNSPECIFIED) {
122 msg_perr("%s called, but SPI write chunk size not defined "
123 "on this hardware. Please report a bug at "
124 "flashrom@flashrom.org\n", __func__);
125 return 1;
126 }
127 return spi_write_chunked(flash, buf, start, len, max_data);
128}
129
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000130int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
131 unsigned int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000132{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000133 unsigned int addrbase = 0;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000134 if (!spi_programmer->read) {
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000135 msg_perr("%s called, but SPI read is unsupported on this "
136 "hardware. Please report a bug at "
137 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000138 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000139 }
140
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +0000141 /* Check if the chip fits between lowest valid and highest possible
142 * address. Highest possible address with the current SPI implementation
143 * means 0xffffff, the highest unsigned 24bit number.
144 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000145 addrbase = spi_get_valid_read_addr(flash);
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +0000146 if (addrbase + flash->total_size * 1024 > (1 << 24)) {
147 msg_perr("Flash chip size exceeds the allowed access window. ");
148 msg_perr("Read will probably fail.\n");
149 /* Try to get the best alignment subject to constraints. */
150 addrbase = (1 << 24) - flash->total_size * 1024;
151 }
152 /* Check if alignment is native (at least the largest power of two which
153 * is a factor of the mapped size of the chip).
154 */
155 if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) {
156 msg_perr("Flash chip is not aligned natively in the allowed "
157 "access window.\n");
158 msg_perr("Read will probably return garbage.\n");
159 }
Michael Karcherb9dbe482011-05-11 17:07:07 +0000160 return spi_programmer->read(flash, buf, addrbase + start, len);
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000161}
162
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000163/*
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000164 * Program chip using page (256 bytes) programming.
165 * Some SPI masters can't do this, they use single byte programming instead.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000166 * The redirect to single byte programming is achieved by setting
167 * .write_256 = spi_chip_write_1
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000168 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000169/* real chunksize is up to 256, logical chunksize is 256 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000170int spi_chip_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start,
171 unsigned int len)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000172{
Michael Karcherb9dbe482011-05-11 17:07:07 +0000173 if (!spi_programmer->write_256) {
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000174 msg_perr("%s called, but SPI page write is unsupported on this "
175 "hardware. Please report a bug at "
176 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +0000177 return 1;
Stefan Reinauer2cb94e12008-06-30 23:45:22 +0000178 }
179
Michael Karcherb9dbe482011-05-11 17:07:07 +0000180 return spi_programmer->write_256(flash, buf, start, len);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000181}
182
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000183/*
184 * Get the lowest allowed address for read accesses. This often happens to
185 * be the lowest allowed address for all commands which take an address.
186 * This is a programmer limitation.
187 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000188uint32_t spi_get_valid_read_addr(struct flashctx *flash)
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000189{
Michael Karcherb9dbe482011-05-11 17:07:07 +0000190 switch (spi_programmer->type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000191#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger80f3d052010-05-28 15:53:08 +0000192#if defined(__i386__) || defined(__x86_64__)
193 case SPI_CONTROLLER_ICH7:
194 /* Return BBAR for ICH chipsets. */
195 return ichspi_bbar;
196#endif
197#endif
198 default:
199 return 0;
200 }
Carl-Daniel Hailfinger3e9dbea2009-05-13 11:40:08 +0000201}
Michael Karcherb9dbe482011-05-11 17:07:07 +0000202
203void register_spi_programmer(const struct spi_programmer *pgm)
204{
205 spi_programmer = pgm;
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000206 buses_supported |= BUS_SPI;
Stefan Tauner93f70232011-07-26 14:33:46 +0000207}