blob: 30e0d3301ff6498097cc8e82ba9e78cddd178823 [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"
hailfinger78031562008-05-13 14:58:23 +000028#include "spi.h"
stepand4b13752007-10-15 21:45:29 +000029
hailfinger40167462009-05-31 17:57:34 +000030enum spi_controller spi_controller = SPI_CONTROLLER_NONE;
31void *spibar = NULL;
32
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,
70 .write_256 = sb600_spi_write_1,
71 },
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,
84 .write_256 = wbsio_spi_write_1,
85 },
hailfinger80422e22009-12-13 22:28:00 +000086#endif
hailfinger324a9cc2010-05-26 01:45:41 +000087#endif
hailfinger948b81f2009-07-22 15:36:50 +000088
hailfinger90c7d542010-05-31 15:27:27 +000089#if CONFIG_FT2232_SPI == 1
hailfinger948b81f2009-07-22 15:36:50 +000090 { /* SPI_CONTROLLER_FT2232 */
91 .command = ft2232_spi_send_command,
92 .multicommand = default_spi_send_multicommand,
93 .read = ft2232_spi_read,
94 .write_256 = ft2232_spi_write_256,
95 },
hailfingerd9dcfbd2009-08-19 13:27:58 +000096#endif
hailfinger948b81f2009-07-22 15:36:50 +000097
hailfinger90c7d542010-05-31 15:27:27 +000098#if CONFIG_DUMMY == 1
hailfinger948b81f2009-07-22 15:36:50 +000099 { /* SPI_CONTROLLER_DUMMY */
100 .command = dummy_spi_send_command,
101 .multicommand = default_spi_send_multicommand,
102 .read = NULL,
103 .write_256 = NULL,
104 },
hailfinger571a6b32009-09-16 10:09:21 +0000105#endif
hailfingerd9dcfbd2009-08-19 13:27:58 +0000106
hailfinger90c7d542010-05-31 15:27:27 +0000107#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000108 { /* SPI_CONTROLLER_BUSPIRATE */
109 .command = buspirate_spi_send_command,
110 .multicommand = default_spi_send_multicommand,
111 .read = buspirate_spi_read,
hailfinger8b82a422010-03-22 03:30:58 +0000112 .write_256 = buspirate_spi_write_256,
hailfinger9c5add72009-11-24 00:20:03 +0000113 },
114#endif
115
hailfinger90c7d542010-05-31 15:27:27 +0000116#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000117 { /* SPI_CONTROLLER_DEDIPROG */
118 .command = dediprog_spi_send_command,
119 .multicommand = default_spi_send_multicommand,
120 .read = dediprog_spi_read,
121 .write_256 = spi_chip_write_1,
122 },
123#endif
124
hailfingerd9dcfbd2009-08-19 13:27:58 +0000125 {}, /* This entry corresponds to SPI_CONTROLLER_INVALID. */
hailfinger948b81f2009-07-22 15:36:50 +0000126};
127
hailfingerd9dcfbd2009-08-19 13:27:58 +0000128const int spi_programmer_count = ARRAY_SIZE(spi_programmer);
hailfinger948b81f2009-07-22 15:36:50 +0000129
hailfinger68002c22009-07-10 21:08:55 +0000130int spi_send_command(unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000131 const unsigned char *writearr, unsigned char *readarr)
hailfinger35cc8162007-10-16 21:09:06 +0000132{
hailfinger948b81f2009-07-22 15:36:50 +0000133 if (!spi_programmer[spi_controller].command) {
snelsone42c3802010-05-07 20:09:04 +0000134 msg_perr("%s called, but SPI is unsupported on this "
hailfinger948b81f2009-07-22 15:36:50 +0000135 "hardware. Please report a bug.\n", __func__);
136 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000137 }
hailfinger948b81f2009-07-22 15:36:50 +0000138
139 return spi_programmer[spi_controller].command(writecnt, readcnt,
140 writearr, readarr);
hailfinger35cc8162007-10-16 21:09:06 +0000141}
142
hailfingerbb092112009-09-18 15:50:56 +0000143int spi_send_multicommand(struct spi_command *cmds)
hailfinger68002c22009-07-10 21:08:55 +0000144{
hailfinger948b81f2009-07-22 15:36:50 +0000145 if (!spi_programmer[spi_controller].multicommand) {
snelsone42c3802010-05-07 20:09:04 +0000146 msg_perr("%s called, but SPI is unsupported on this "
hailfinger948b81f2009-07-22 15:36:50 +0000147 "hardware. Please report a bug.\n", __func__);
148 return 1;
hailfinger68002c22009-07-10 21:08:55 +0000149 }
hailfinger948b81f2009-07-22 15:36:50 +0000150
hailfingerbb092112009-09-18 15:50:56 +0000151 return spi_programmer[spi_controller].multicommand(cmds);
hailfinger948b81f2009-07-22 15:36:50 +0000152}
153
154int default_spi_send_command(unsigned int writecnt, unsigned int readcnt,
155 const unsigned char *writearr, unsigned char *readarr)
156{
157 struct spi_command cmd[] = {
158 {
159 .writecnt = writecnt,
160 .readcnt = readcnt,
161 .writearr = writearr,
162 .readarr = readarr,
163 }, {
164 .writecnt = 0,
165 .writearr = NULL,
166 .readcnt = 0,
167 .readarr = NULL,
168 }};
169
170 return spi_send_multicommand(cmd);
171}
172
hailfingerbb092112009-09-18 15:50:56 +0000173int default_spi_send_multicommand(struct spi_command *cmds)
hailfinger948b81f2009-07-22 15:36:50 +0000174{
175 int result = 0;
hailfingerbb092112009-09-18 15:50:56 +0000176 for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
177 result = spi_send_command(cmds->writecnt, cmds->readcnt,
178 cmds->writearr, cmds->readarr);
hailfinger948b81f2009-07-22 15:36:50 +0000179 }
180 return result;
hailfinger68002c22009-07-10 21:08:55 +0000181}
182
hailfinger0f08b7a2009-06-16 08:55:44 +0000183int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfingerb8f7e882008-01-19 00:04:46 +0000184{
hailfinger948b81f2009-07-22 15:36:50 +0000185 if (!spi_programmer[spi_controller].read) {
snelsone42c3802010-05-07 20:09:04 +0000186 msg_perr("%s called, but SPI read is unsupported on this"
hailfinger948b81f2009-07-22 15:36:50 +0000187 " hardware. Please report a bug.\n", __func__);
188 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000189 }
190
hailfinger948b81f2009-07-22 15:36:50 +0000191 return spi_programmer[spi_controller].read(flash, buf, start, len);
hailfingerb8f7e882008-01-19 00:04:46 +0000192}
193
hailfingered063f52009-05-09 02:30:21 +0000194/*
hailfingered063f52009-05-09 02:30:21 +0000195 * Program chip using page (256 bytes) programming.
196 * Some SPI masters can't do this, they use single byte programming instead.
197 */
hailfinger87c05482009-05-09 02:34:18 +0000198int spi_chip_write_256(struct flashchip *flash, uint8_t *buf)
hailfinger2c361e42008-05-13 23:03:12 +0000199{
hailfinger948b81f2009-07-22 15:36:50 +0000200 if (!spi_programmer[spi_controller].write_256) {
snelsone42c3802010-05-07 20:09:04 +0000201 msg_perr("%s called, but SPI page write is unsupported "
hailfinger948b81f2009-07-22 15:36:50 +0000202 " on this hardware. Please report a bug.\n", __func__);
203 return 1;
stepan3bdf6182008-06-30 23:45:22 +0000204 }
205
hailfinger948b81f2009-07-22 15:36:50 +0000206 return spi_programmer[spi_controller].write_256(flash, buf);
hailfingerf71c0ac2007-10-18 00:24:07 +0000207}
stuge712ce862009-01-26 03:37:40 +0000208
hailfingerb767c122010-05-28 15:53:08 +0000209/*
210 * Get the lowest allowed address for read accesses. This often happens to
211 * be the lowest allowed address for all commands which take an address.
212 * This is a programmer limitation.
213 */
hailfinger54c14662009-05-13 11:40:08 +0000214uint32_t spi_get_valid_read_addr(void)
215{
hailfingerb767c122010-05-28 15:53:08 +0000216 switch (spi_controller) {
hailfinger90c7d542010-05-31 15:27:27 +0000217#if CONFIG_INTERNAL == 1
hailfingerb767c122010-05-28 15:53:08 +0000218#if defined(__i386__) || defined(__x86_64__)
219 case SPI_CONTROLLER_ICH7:
220 /* Return BBAR for ICH chipsets. */
221 return ichspi_bbar;
222#endif
223#endif
224 default:
225 return 0;
226 }
hailfinger54c14662009-05-13 11:40:08 +0000227}