Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 3431bb7 | 2009-06-24 08:28:39 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 6 | * |
| 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 Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 25 | #include <strings.h> |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 26 | #include <string.h> |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 27 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 28 | #include "flashchips.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 29 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 30 | #include "programmer.h" |
Carl-Daniel Hailfinger | d6cbf76 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 31 | #include "spi.h" |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 32 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 33 | const 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 Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 43 | const struct spi_programmer *spi_programmer = &spi_programmer_none; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 44 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 45 | int spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 46 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 47 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 48 | if (!spi_programmer->command) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 49 | msg_perr("%s called, but SPI is unsupported on this " |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 50 | "hardware. Please report a bug at " |
| 51 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 52 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 53 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 54 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 55 | return spi_programmer->command(writecnt, readcnt, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 56 | writearr, readarr); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 59 | int spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 60 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 61 | if (!spi_programmer->multicommand) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 62 | msg_perr("%s called, but SPI is unsupported on this " |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 63 | "hardware. Please report a bug at " |
| 64 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 65 | return 1; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 66 | } |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 67 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 68 | return spi_programmer->multicommand(cmds); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int default_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 72 | 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 | |
| 87 | return spi_send_multicommand(cmd); |
| 88 | } |
| 89 | |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 90 | int default_spi_send_multicommand(struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 91 | { |
| 92 | int result = 0; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 93 | for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) { |
| 94 | result = spi_send_command(cmds->writecnt, cmds->readcnt, |
| 95 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 96 | } |
| 97 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 100 | int default_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 101 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 102 | int max_data = spi_programmer->max_data_read; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 103 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 104 | msg_perr("%s called, but SPI read chunk size not defined " |
| 105 | "on this hardware. Please report a bug at " |
| 106 | "flashrom@flashrom.org\n", __func__); |
| 107 | return 1; |
| 108 | } |
| 109 | return spi_read_chunked(flash, buf, start, len, max_data); |
| 110 | } |
| 111 | |
| 112 | int default_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 113 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 114 | int max_data = spi_programmer->max_data_write; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 115 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 116 | msg_perr("%s called, but SPI write chunk size not defined " |
| 117 | "on this hardware. Please report a bug at " |
| 118 | "flashrom@flashrom.org\n", __func__); |
| 119 | return 1; |
| 120 | } |
| 121 | return spi_write_chunked(flash, buf, start, len, max_data); |
| 122 | } |
| 123 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 124 | int spi_chip_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 125 | { |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 126 | int addrbase = 0; |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 127 | if (!spi_programmer->read) { |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 128 | msg_perr("%s called, but SPI read is unsupported on this " |
| 129 | "hardware. Please report a bug at " |
| 130 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 131 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 134 | /* Check if the chip fits between lowest valid and highest possible |
| 135 | * address. Highest possible address with the current SPI implementation |
| 136 | * means 0xffffff, the highest unsigned 24bit number. |
| 137 | */ |
| 138 | addrbase = spi_get_valid_read_addr(); |
| 139 | if (addrbase + flash->total_size * 1024 > (1 << 24)) { |
| 140 | msg_perr("Flash chip size exceeds the allowed access window. "); |
| 141 | msg_perr("Read will probably fail.\n"); |
| 142 | /* Try to get the best alignment subject to constraints. */ |
| 143 | addrbase = (1 << 24) - flash->total_size * 1024; |
| 144 | } |
| 145 | /* Check if alignment is native (at least the largest power of two which |
| 146 | * is a factor of the mapped size of the chip). |
| 147 | */ |
| 148 | if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) { |
| 149 | msg_perr("Flash chip is not aligned natively in the allowed " |
| 150 | "access window.\n"); |
| 151 | msg_perr("Read will probably return garbage.\n"); |
| 152 | } |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 153 | return spi_programmer->read(flash, buf, addrbase + start, len); |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 156 | /* |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 157 | * Program chip using page (256 bytes) programming. |
| 158 | * Some SPI masters can't do this, they use single byte programming instead. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 159 | * The redirect to single byte programming is achieved by setting |
| 160 | * .write_256 = spi_chip_write_1 |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 161 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 162 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Carl-Daniel Hailfinger | 75a58f9 | 2010-10-13 22:26:56 +0000 | [diff] [blame] | 163 | int spi_chip_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 164 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 165 | if (!spi_programmer->write_256) { |
Carl-Daniel Hailfinger | 8ae500e | 2010-06-20 10:39:33 +0000 | [diff] [blame] | 166 | msg_perr("%s called, but SPI page write is unsupported on this " |
| 167 | "hardware. Please report a bug at " |
| 168 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 169 | return 1; |
Stefan Reinauer | 2cb94e1 | 2008-06-30 23:45:22 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 172 | return spi_programmer->write_256(flash, buf, start, len); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 175 | /* |
| 176 | * Get the lowest allowed address for read accesses. This often happens to |
| 177 | * be the lowest allowed address for all commands which take an address. |
| 178 | * This is a programmer limitation. |
| 179 | */ |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 180 | uint32_t spi_get_valid_read_addr(void) |
| 181 | { |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 182 | switch (spi_programmer->type) { |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 183 | #if CONFIG_INTERNAL == 1 |
Carl-Daniel Hailfinger | 80f3d05 | 2010-05-28 15:53:08 +0000 | [diff] [blame] | 184 | #if defined(__i386__) || defined(__x86_64__) |
| 185 | case SPI_CONTROLLER_ICH7: |
| 186 | /* Return BBAR for ICH chipsets. */ |
| 187 | return ichspi_bbar; |
| 188 | #endif |
| 189 | #endif |
| 190 | default: |
| 191 | return 0; |
| 192 | } |
Carl-Daniel Hailfinger | 3e9dbea | 2009-05-13 11:40:08 +0000 | [diff] [blame] | 193 | } |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 194 | |
| 195 | void register_spi_programmer(const struct spi_programmer *pgm) |
| 196 | { |
| 197 | spi_programmer = pgm; |
| 198 | buses_supported |= CHIP_BUSTYPE_SPI; |
| 199 | } |