stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 4 | * Copyright (C) 2007, 2008, 2009, 2010, 2011 Carl-Daniel Hailfinger |
stepan | dbd3af1 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
stepan | d4b1375 | 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. |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contains the generic SPI framework |
| 19 | */ |
| 20 | |
oxygene | 70aa650 | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 21 | #include <strings.h> |
hailfinger | 132df7b | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 22 | #include <string.h> |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 23 | #include "flash.h" |
hailfinger | 66966da | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 24 | #include "flashchips.h" |
snelson | 8913d08 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 25 | #include "chipdrivers.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 26 | #include "programmer.h" |
hailfinger | 7803156 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 27 | #include "spi.h" |
stepan | d4b1375 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 28 | |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 29 | int spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 30 | unsigned int readcnt, const unsigned char *writearr, |
| 31 | unsigned char *readarr) |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 32 | { |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 33 | return flash->mst->spi.command(flash, writecnt, readcnt, writearr, |
| 34 | readarr); |
hailfinger | 35cc816 | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 37 | int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds) |
hailfinger | 68002c2 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 38 | { |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 39 | return flash->mst->spi.multicommand(flash, cmds); |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 42 | int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 43 | unsigned int readcnt, |
| 44 | const unsigned char *writearr, |
| 45 | unsigned char *readarr) |
hailfinger | 948b81f | 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 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 60 | return spi_send_multicommand(flash, cmd); |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 63 | int default_spi_send_multicommand(const struct flashctx *flash, |
| 64 | struct spi_command *cmds) |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 65 | { |
| 66 | int result = 0; |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 67 | for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) { |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 68 | result = spi_send_command(flash, cmds->writecnt, cmds->readcnt, |
hailfinger | bb09211 | 2009-09-18 15:50:56 +0000 | [diff] [blame] | 69 | cmds->writearr, cmds->readarr); |
hailfinger | 948b81f | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 70 | } |
| 71 | return result; |
hailfinger | 68002c2 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 74 | int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 75 | unsigned int len) |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 76 | { |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 77 | unsigned int max_data = flash->mst->spi.max_data_read; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 78 | int rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 79 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 80 | msg_perr("%s called, but SPI read chunk size not defined " |
| 81 | "on this hardware. Please report a bug at " |
| 82 | "flashrom@flashrom.org\n", __func__); |
| 83 | return 1; |
| 84 | } |
Edward O'Callaghan | 2748621 | 2019-07-26 21:59:55 +1000 | [diff] [blame] | 85 | rc = spi_read_chunked(flash, buf, start, len, max_data); |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 86 | /* translate SPI-specific access denied error to generic error */ |
| 87 | if (rc == SPI_ACCESS_DENIED) |
| 88 | rc = ACCESS_DENIED; |
| 89 | return rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 92 | int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 93 | { |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 94 | unsigned int max_data = flash->mst->spi.max_data_write; |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 95 | int rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 96 | if (max_data == MAX_DATA_UNSPECIFIED) { |
| 97 | msg_perr("%s called, but SPI write chunk size not defined " |
| 98 | "on this hardware. Please report a bug at " |
| 99 | "flashrom@flashrom.org\n", __func__); |
| 100 | return 1; |
| 101 | } |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 102 | rc = spi_write_chunked(flash, buf, start, len, max_data); |
| 103 | /* translate SPI-specific access denied error to generic error */ |
| 104 | if (rc == SPI_ACCESS_DENIED) |
| 105 | rc = ACCESS_DENIED; |
| 106 | return rc; |
mkarcher | 8fb5759 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 109 | int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 110 | unsigned int len) |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 111 | { |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 112 | return flash->mst->spi.read(flash, buf, start, len); |
hailfinger | b8f7e88 | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
hailfinger | ed063f5 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 115 | /* |
hailfinger | ed063f5 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 116 | * Program chip using page (256 bytes) programming. |
| 117 | * Some SPI masters can't do this, they use single byte programming instead. |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 118 | * The redirect to single byte programming is achieved by setting |
| 119 | * .write_256 = spi_chip_write_1 |
hailfinger | ed063f5 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 120 | */ |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 121 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 122 | int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
hailfinger | 2c361e4 | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 123 | { |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 124 | return flash->mst->spi.write_256(flash, buf, start, len); |
hailfinger | c7d06c6 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Edward O'Callaghan | eeaac6b | 2020-10-12 19:51:56 +1100 | [diff] [blame] | 127 | int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
| 128 | { |
| 129 | return flash->mst->spi.write_aai(flash, buf, start, len); |
| 130 | } |
| 131 | |
Edward O'Callaghan | 20ba615 | 2019-08-26 23:21:09 +1000 | [diff] [blame] | 132 | int register_spi_master(const struct spi_master *mst) |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 133 | { |
Edward O'Callaghan | 20ba615 | 2019-08-26 23:21:09 +1000 | [diff] [blame] | 134 | struct registered_master rmst; |
| 135 | |
Edward O'Callaghan | c66827e | 2020-10-09 12:22:04 +1100 | [diff] [blame^] | 136 | if (!mst->write_aai || !mst->write_256 || !mst->read || !mst->command || |
| 137 | !mst->multicommand || |
| 138 | ((mst->command == default_spi_send_command) && |
| 139 | (mst->multicommand == default_spi_send_multicommand))) { |
| 140 | msg_perr("%s called with incomplete master definition. " |
| 141 | "Please report a bug at flashrom@flashrom.org\n", |
| 142 | __func__); |
| 143 | return ERROR_FLASHROM_BUG; |
| 144 | } |
| 145 | |
Edward O'Callaghan | 20ba615 | 2019-08-26 23:21:09 +1000 | [diff] [blame] | 146 | |
| 147 | rmst.buses_supported = BUS_SPI; |
| 148 | rmst.spi = *mst; |
Edward O'Callaghan | 20ba615 | 2019-08-26 23:21:09 +1000 | [diff] [blame] | 149 | return register_master(&rmst); |
David Hendricks | 9104083 | 2011-07-08 20:01:09 -0700 | [diff] [blame] | 150 | } |