blob: 6d5075458b15fab6f9769f88de93e3377ad1ee58 [file] [log] [blame]
stepand4b13752007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Craig Hesling65eb8812019-08-01 09:33:56 -07004 * 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 *
stepand4b13752007-10-15 21:45:29 +000016 */
17
18/*
19 * Contains the generic SPI framework
20 */
21
oxygene70aa6502011-03-08 07:17:44 +000022#include <strings.h>
hailfinger132df7b2010-09-15 00:13:02 +000023#include <string.h>
stepand4b13752007-10-15 21:45:29 +000024#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000025#include "flashchips.h"
snelson8913d082010-02-26 05:48:29 +000026#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
hailfinger78031562008-05-13 14:58:23 +000028#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000029
Craig Hesling65eb8812019-08-01 09:33:56 -070030const struct spi_master spi_master_none = {
31 .type = SPI_CONTROLLER_NONE,
32 .max_data_read = MAX_DATA_UNSPECIFIED,
33 .max_data_write = MAX_DATA_UNSPECIFIED,
34 .command = NULL,
35 .multicommand = NULL,
36 .read = NULL,
37 .write_256 = NULL,
38};
39
40const struct spi_master *spi_master = &spi_master_none;
41
Souvik Ghoshd75cd672016-06-17 14:21:39 -070042int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +000043 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +000044{
Craig Hesling65eb8812019-08-01 09:33:56 -070045 if (!spi_master->command) {
Duncan Laurie870d8af2019-01-09 18:05:23 -080046 msg_pdbg("%s called, but SPI is unsupported on this "
David Hendricksac1d25c2016-08-09 17:00:58 -070047 "hardware. Please report a bug at "
48 "flashrom@flashrom.org\n", __func__);
49 return 1;
50 }
51
Craig Hesling65eb8812019-08-01 09:33:56 -070052 return spi_master->command(flash, writecnt, readcnt,
David Hendricksac1d25c2016-08-09 17:00:58 -070053 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +000054}
55
Souvik Ghoshd75cd672016-06-17 14:21:39 -070056int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +000057{
Craig Hesling65eb8812019-08-01 09:33:56 -070058 if (!spi_master->multicommand) {
Duncan Laurie870d8af2019-01-09 18:05:23 -080059 msg_pdbg("%s called, but SPI is unsupported on this "
David Hendricksac1d25c2016-08-09 17:00:58 -070060 "hardware. Please report a bug at "
61 "flashrom@flashrom.org\n", __func__);
62 return 1;
63 }
64
Craig Hesling65eb8812019-08-01 09:33:56 -070065 return spi_master->multicommand(flash, cmds);
hailfinger948b81f2009-07-22 15:36:50 +000066}
67
Souvik Ghoshd75cd672016-06-17 14:21:39 -070068int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
hailfinger948b81f2009-07-22 15:36:50 +000069 const unsigned char *writearr, unsigned char *readarr)
70{
71 struct spi_command cmd[] = {
72 {
73 .writecnt = writecnt,
74 .readcnt = readcnt,
75 .writearr = writearr,
76 .readarr = readarr,
77 }, {
78 .writecnt = 0,
79 .writearr = NULL,
80 .readcnt = 0,
81 .readarr = NULL,
82 }};
83
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084 return spi_send_multicommand(flash, cmd);
hailfinger948b81f2009-07-22 15:36:50 +000085}
86
Souvik Ghoshd75cd672016-06-17 14:21:39 -070087int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +000088{
89 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +000090 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -070091 result = spi_send_command(flash, cmds->writecnt, cmds->readcnt,
hailfingerbb092112009-09-18 15:50:56 +000092 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +000093 }
94 return result;
hailfinger68002c22009-07-10 21:08:55 +000095}
96
Souvik Ghoshd75cd672016-06-17 14:21:39 -070097int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
mkarcher8fb57592011-05-11 17:07:02 +000098{
Craig Hesling65eb8812019-08-01 09:33:56 -070099 unsigned int max_data = spi_master->max_data_read;
David Hendricks1ed1d352011-11-23 17:54:37 -0800100 int rc;
mkarcher8fb57592011-05-11 17:07:02 +0000101 if (max_data == MAX_DATA_UNSPECIFIED) {
102 msg_perr("%s called, but SPI read chunk size not defined "
103 "on this hardware. Please report a bug at "
104 "flashrom@flashrom.org\n", __func__);
105 return 1;
106 }
Edward O'Callaghan27486212019-07-26 21:59:55 +1000107 rc = spi_read_chunked(flash, buf, start, len, max_data);
David Hendricks1ed1d352011-11-23 17:54:37 -0800108 /* translate SPI-specific access denied error to generic error */
109 if (rc == SPI_ACCESS_DENIED)
110 rc = ACCESS_DENIED;
111 return rc;
mkarcher8fb57592011-05-11 17:07:02 +0000112}
113
Patrick Georgiab8353e2017-02-03 18:32:01 +0100114int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
mkarcher8fb57592011-05-11 17:07:02 +0000115{
Craig Hesling65eb8812019-08-01 09:33:56 -0700116 unsigned int max_data = spi_master->max_data_write;
David Hendricks1ed1d352011-11-23 17:54:37 -0800117 int rc;
mkarcher8fb57592011-05-11 17:07:02 +0000118 if (max_data == MAX_DATA_UNSPECIFIED) {
119 msg_perr("%s called, but SPI write chunk size not defined "
120 "on this hardware. Please report a bug at "
121 "flashrom@flashrom.org\n", __func__);
122 return 1;
123 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800124 rc = spi_write_chunked(flash, buf, start, len, max_data);
125 /* translate SPI-specific access denied error to generic error */
126 if (rc == SPI_ACCESS_DENIED)
127 rc = ACCESS_DENIED;
128 return rc;
mkarcher8fb57592011-05-11 17:07:02 +0000129}
130
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700131int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000132{
stefanctc5eb8a92011-11-23 09:13:48 +0000133 unsigned int addrbase = 0;
Craig Hesling65eb8812019-08-01 09:33:56 -0700134 if (!spi_master->read) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700135 msg_perr("%s called, but SPI read is unsupported on this "
136 "hardware. Please report a bug at "
137 "flashrom@flashrom.org\n", __func__);
138 return 1;
139 }
140
hailfinger132df7b2010-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 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700145 addrbase = spi_get_valid_read_addr(flash);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200146 /* Show flash chip size warning if flash chip doesn't support
147 4-Bytes Addressing mode and last address excedes 24 bits */
Edward O'Callaghan9713aa62019-07-18 18:28:57 +1000148 if (!(flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN) &&
Boris Baykov1a2f5322016-06-11 18:29:00 +0200149 addrbase + flash->chip->total_size * 1024 > (1 << 24)) {
hailfinger132df7b2010-09-15 00:13:02 +0000150 msg_perr("Flash chip size exceeds the allowed access window. ");
151 msg_perr("Read will probably fail.\n");
152 /* Try to get the best alignment subject to constraints. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100153 addrbase = (1 << 24) - flash->chip->total_size * 1024;
hailfinger132df7b2010-09-15 00:13:02 +0000154 }
155 /* Check if alignment is native (at least the largest power of two which
156 * is a factor of the mapped size of the chip).
157 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100158 if (ffs(flash->chip->total_size * 1024) > (ffs(addrbase) ? : 33)) {
hailfinger132df7b2010-09-15 00:13:02 +0000159 msg_perr("Flash chip is not aligned natively in the allowed "
160 "access window.\n");
161 msg_perr("Read will probably return garbage.\n");
162 }
Craig Hesling65eb8812019-08-01 09:33:56 -0700163 return spi_master->read(flash, buf, addrbase + start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000164}
165
hailfingered063f52009-05-09 02:30:21 +0000166/*
hailfingered063f52009-05-09 02:30:21 +0000167 * Program chip using page (256 bytes) programming.
168 * Some SPI masters can't do this, they use single byte programming instead.
hailfingerc7d06c62010-07-14 16:19:05 +0000169 * The redirect to single byte programming is achieved by setting
170 * .write_256 = spi_chip_write_1
hailfingered063f52009-05-09 02:30:21 +0000171 */
hailfingerc7d06c62010-07-14 16:19:05 +0000172/* real chunksize is up to 256, logical chunksize is 256 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100173int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfinger2c361e42008-05-13 23:03:12 +0000174{
Craig Hesling65eb8812019-08-01 09:33:56 -0700175 if (!spi_master->write_256) {
David Hendricksac1d25c2016-08-09 17:00:58 -0700176 msg_perr("%s called, but SPI page write is unsupported on this "
177 "hardware. Please report a bug at "
178 "flashrom@flashrom.org\n", __func__);
179 return 1;
180 }
181
Craig Hesling65eb8812019-08-01 09:33:56 -0700182 return spi_master->write_256(flash, buf, start, len);
hailfingerc7d06c62010-07-14 16:19:05 +0000183}
184
hailfingerb767c122010-05-28 15:53:08 +0000185/*
186 * Get the lowest allowed address for read accesses. This often happens to
187 * be the lowest allowed address for all commands which take an address.
188 * This is a programmer limitation.
189 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700190uint32_t spi_get_valid_read_addr(struct flashctx *flash)
hailfinger54c14662009-05-13 11:40:08 +0000191{
Craig Hesling65eb8812019-08-01 09:33:56 -0700192 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000193#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000194#if defined(__i386__) || defined(__x86_64__)
195 case SPI_CONTROLLER_ICH7:
196 /* Return BBAR for ICH chipsets. */
197 return ichspi_bbar;
198#endif
199#endif
200 default:
201 return 0;
202 }
hailfinger54c14662009-05-13 11:40:08 +0000203}
mkarcherd264e9e2011-05-11 17:07:07 +0000204
Craig Hesling65eb8812019-08-01 09:33:56 -0700205void register_spi_master(const struct spi_master *pgm)
mkarcherd264e9e2011-05-11 17:07:07 +0000206{
Craig Hesling65eb8812019-08-01 09:33:56 -0700207 spi_master = pgm;
208 buses_supported |= BUS_SPI;
David Hendricks91040832011-07-08 20:01:09 -0700209}