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 | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010, 2011 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. |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contains the generic SPI framework |
| 19 | */ |
| 20 | |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 21 | #include <strings.h> |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 22 | #include <string.h> |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 24 | #include "flashchips.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 25 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 26 | #include "programmer.h" |
Carl-Daniel Hailfinger | d6cbf76 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 27 | #include "spi.h" |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 28 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame^] | 29 | int spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 30 | unsigned int readcnt, const unsigned char *writearr, |
| 31 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 32 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 33 | return flash->mst->spi.command(flash, writecnt, readcnt, writearr, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 34 | readarr); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame^] | 37 | int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds) |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 38 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 39 | return flash->mst->spi.multicommand(flash, cmds); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame^] | 42 | int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 43 | unsigned int readcnt, |
| 44 | const unsigned char *writearr, |
| 45 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 46 | { |
| 47 | struct spi_command cmd[] = { |
| 48 | { |
| 49 | .writecnt = writecnt, |
| 50 | .readcnt = readcnt, |
| 51 | .writearr = writearr, |
| 52 | .readarr = readarr, |
| 53 | }, { |
| 54 | .writecnt = 0, |
| 55 | .writearr = NULL, |
| 56 | .readcnt = 0, |
| 57 | .readarr = NULL, |
| 58 | }}; |
| 59 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 60 | return spi_send_multicommand(flash, cmd); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame^] | 63 | int default_spi_send_multicommand(const struct flashctx *flash, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 64 | struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 65 | { |
| 66 | int result = 0; |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 67 | for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 68 | result = spi_send_command(flash, cmds->writecnt, cmds->readcnt, |
Carl-Daniel Hailfinger | 26f7e64 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 69 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 70 | } |
| 71 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 74 | int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 75 | unsigned int len) |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 76 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 77 | unsigned int max_data = flash->mst->spi.max_data_read; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 78 | 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 | } |
| 84 | return spi_read_chunked(flash, buf, start, len, max_data); |
| 85 | } |
| 86 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 87 | int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 88 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 89 | unsigned int max_data = flash->mst->spi.max_data_write; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 90 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 91 | msg_perr("%s called, but SPI write chunk size not defined " |
| 92 | "on this hardware. Please report a bug at " |
| 93 | "flashrom@flashrom.org\n", __func__); |
| 94 | return 1; |
| 95 | } |
| 96 | return spi_write_chunked(flash, buf, start, len, max_data); |
| 97 | } |
| 98 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 99 | int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 100 | unsigned int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 101 | { |
Nico Huber | d8b2e80 | 2019-06-18 23:39:56 +0200 | [diff] [blame] | 102 | int ret; |
| 103 | size_t to_read; |
| 104 | for (; len; len -= to_read, buf += to_read, start += to_read) { |
| 105 | /* Do not cross 16MiB boundaries in a single transfer. |
| 106 | This helps with |
| 107 | o multi-die 4-byte-addressing chips, |
| 108 | o dediprog that has a protocol limit of 32MiB-512B. */ |
| 109 | to_read = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len); |
| 110 | ret = flash->mst->spi.read(flash, buf, start, to_read); |
| 111 | if (ret) |
| 112 | return ret; |
| 113 | } |
| 114 | return 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 117 | /* |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 118 | * Program chip using page (256 bytes) programming. |
| 119 | * 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] | 120 | * The redirect to single byte programming is achieved by setting |
| 121 | * .write_256 = spi_chip_write_1 |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 122 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 123 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 124 | int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 125 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 126 | return flash->mst->spi.write_256(flash, buf, start, len); |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 129 | int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 130 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 131 | return flash->mst->spi.write_aai(flash, buf, start, len); |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 134 | int register_spi_master(const struct spi_master *mst) |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 135 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 136 | struct registered_master rmst; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 137 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 138 | if (!mst->write_aai || !mst->write_256 || !mst->read || !mst->command || |
| 139 | !mst->multicommand || |
| 140 | ((mst->command == default_spi_send_command) && |
| 141 | (mst->multicommand == default_spi_send_multicommand))) { |
| 142 | msg_perr("%s called with incomplete master definition. " |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 143 | "Please report a bug at flashrom@flashrom.org\n", |
| 144 | __func__); |
| 145 | return ERROR_FLASHROM_BUG; |
| 146 | } |
| 147 | |
| 148 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 149 | rmst.buses_supported = BUS_SPI; |
| 150 | rmst.spi = *mst; |
| 151 | return register_master(&rmst); |
Stefan Tauner | 93f7023 | 2011-07-26 14:33:46 +0000 | [diff] [blame] | 152 | } |