spi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
  5. * Copyright (C) 2008 coresystems GmbH
  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. * Contains the generic SPI framework
  22. */
  23. #include <strings.h>
  24. #include <string.h>
  25. #include "flash.h"
  26. #include "flashchips.h"
  27. #include "chipdrivers.h"
  28. #include "programmer.h"
  29. #include "spi.h"
  30. const struct spi_programmer spi_programmer_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. const struct spi_programmer *spi_programmer = &spi_programmer_none;
  40. int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  41. const unsigned char *writearr, unsigned char *readarr)
  42. {
  43. if (!spi_programmer->command) {
  44. msg_perr("%s called, but SPI is unsupported on this "
  45. "hardware. Please report a bug at "
  46. "flashrom@flashrom.org\n", __func__);
  47. return 1;
  48. }
  49. return spi_programmer->command(flash, writecnt, readcnt,
  50. writearr, readarr);
  51. }
  52. int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
  53. {
  54. if (!spi_programmer->multicommand) {
  55. msg_perr("%s called, but SPI is unsupported on this "
  56. "hardware. Please report a bug at "
  57. "flashrom@flashrom.org\n", __func__);
  58. return 1;
  59. }
  60. return spi_programmer->multicommand(flash, cmds);
  61. }
  62. int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
  63. const unsigned char *writearr, unsigned char *readarr)
  64. {
  65. struct spi_command cmd[] = {
  66. {
  67. .writecnt = writecnt,
  68. .readcnt = readcnt,
  69. .writearr = writearr,
  70. .readarr = readarr,
  71. }, {
  72. .writecnt = 0,
  73. .writearr = NULL,
  74. .readcnt = 0,
  75. .readarr = NULL,
  76. }};
  77. return spi_send_multicommand(flash, cmd);
  78. }
  79. int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
  80. {
  81. int result = 0;
  82. for (; (cmds->writecnt || cmds->readcnt) && !result; cmds++) {
  83. result = spi_send_command(flash, cmds->writecnt, cmds->readcnt,
  84. cmds->writearr, cmds->readarr);
  85. }
  86. return result;
  87. }
  88. int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  89. {
  90. unsigned int max_data = spi_programmer->max_data_read;
  91. int rc;
  92. if (max_data == MAX_DATA_UNSPECIFIED) {
  93. msg_perr("%s called, but SPI read chunk size not defined "
  94. "on this hardware. Please report a bug at "
  95. "flashrom@flashrom.org\n", __func__);
  96. return 1;
  97. }
  98. if (flash->feature_bits & FEATURE_UNBOUND_READ)
  99. rc = spi_read_unbound(flash, buf, start, len, max_data);
  100. else
  101. rc = spi_read_chunked(flash, buf, start, len, max_data);
  102. /* translate SPI-specific access denied error to generic error */
  103. if (rc == SPI_ACCESS_DENIED)
  104. rc = ACCESS_DENIED;
  105. return rc;
  106. }
  107. int default_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  108. {
  109. unsigned int max_data = spi_programmer->max_data_write;
  110. int rc;
  111. if (max_data == MAX_DATA_UNSPECIFIED) {
  112. msg_perr("%s called, but SPI write chunk size not defined "
  113. "on this hardware. Please report a bug at "
  114. "flashrom@flashrom.org\n", __func__);
  115. return 1;
  116. }
  117. rc = spi_write_chunked(flash, buf, start, len, max_data);
  118. /* translate SPI-specific access denied error to generic error */
  119. if (rc == SPI_ACCESS_DENIED)
  120. rc = ACCESS_DENIED;
  121. return rc;
  122. }
  123. int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  124. {
  125. unsigned int addrbase = 0;
  126. if (!spi_programmer->read) {
  127. msg_perr("%s called, but SPI read is unsupported on this "
  128. "hardware. Please report a bug at "
  129. "flashrom@flashrom.org\n", __func__);
  130. return 1;
  131. }
  132. /* Check if the chip fits between lowest valid and highest possible
  133. * address. Highest possible address with the current SPI implementation
  134. * means 0xffffff, the highest unsigned 24bit number.
  135. */
  136. addrbase = spi_get_valid_read_addr(flash);
  137. if (addrbase + flash->total_size * 1024 > (1 << 24)) {
  138. msg_perr("Flash chip size exceeds the allowed access window. ");
  139. msg_perr("Read will probably fail.\n");
  140. /* Try to get the best alignment subject to constraints. */
  141. addrbase = (1 << 24) - flash->total_size * 1024;
  142. }
  143. /* Check if alignment is native (at least the largest power of two which
  144. * is a factor of the mapped size of the chip).
  145. */
  146. if (ffs(flash->total_size * 1024) > (ffs(addrbase) ? : 33)) {
  147. msg_perr("Flash chip is not aligned natively in the allowed "
  148. "access window.\n");
  149. msg_perr("Read will probably return garbage.\n");
  150. }
  151. return spi_programmer->read(flash, buf, addrbase + start, len);
  152. }
  153. /*
  154. * Program chip using page (256 bytes) programming.
  155. * Some SPI masters can't do this, they use single byte programming instead.
  156. * The redirect to single byte programming is achieved by setting
  157. * .write_256 = spi_chip_write_1
  158. */
  159. /* real chunksize is up to 256, logical chunksize is 256 */
  160. int spi_chip_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  161. {
  162. if (!spi_programmer->write_256) {
  163. msg_perr("%s called, but SPI page write is unsupported on this "
  164. "hardware. Please report a bug at "
  165. "flashrom@flashrom.org\n", __func__);
  166. return 1;
  167. }
  168. return spi_programmer->write_256(flash, buf, start, len);
  169. }
  170. /*
  171. * Get the lowest allowed address for read accesses. This often happens to
  172. * be the lowest allowed address for all commands which take an address.
  173. * This is a programmer limitation.
  174. */
  175. uint32_t spi_get_valid_read_addr(struct flashctx *flash)
  176. {
  177. switch (spi_programmer->type) {
  178. #if CONFIG_INTERNAL == 1
  179. #if defined(__i386__) || defined(__x86_64__)
  180. case SPI_CONTROLLER_ICH7:
  181. /* Return BBAR for ICH chipsets. */
  182. return ichspi_bbar;
  183. #endif
  184. #endif
  185. default:
  186. return 0;
  187. }
  188. }
  189. void register_spi_programmer(const struct spi_programmer *pgm)
  190. {
  191. spi_programmer = pgm;
  192. buses_supported |= BUS_SPI;
  193. }