bitbang_spi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include "flash.h"
  24. #include "programmer.h"
  25. #include "spi.h"
  26. /* Length of half a clock period in usecs. */
  27. static int bitbang_spi_half_period;
  28. static const struct bitbang_spi_master *bitbang_spi_master = NULL;
  29. /* Note that CS# is active low, so val=0 means the chip is active. */
  30. static void bitbang_spi_set_cs(int val)
  31. {
  32. bitbang_spi_master->set_cs(val);
  33. }
  34. static void bitbang_spi_set_sck(int val)
  35. {
  36. bitbang_spi_master->set_sck(val);
  37. }
  38. static void bitbang_spi_set_mosi(int val)
  39. {
  40. bitbang_spi_master->set_mosi(val);
  41. }
  42. static int bitbang_spi_get_miso(void)
  43. {
  44. return bitbang_spi_master->get_miso();
  45. }
  46. static void bitbang_spi_request_bus(void)
  47. {
  48. if (bitbang_spi_master->request_bus)
  49. bitbang_spi_master->request_bus();
  50. }
  51. static void bitbang_spi_release_bus(void)
  52. {
  53. if (bitbang_spi_master->release_bus)
  54. bitbang_spi_master->release_bus();
  55. }
  56. static int bitbang_spi_send_command(const struct flashctx *flash,
  57. unsigned int writecnt, unsigned int readcnt,
  58. const unsigned char *writearr,
  59. unsigned char *readarr);
  60. static const struct spi_programmer spi_programmer_bitbang = {
  61. .type = SPI_CONTROLLER_BITBANG,
  62. .max_data_read = MAX_DATA_READ_UNLIMITED,
  63. .max_data_write = MAX_DATA_WRITE_UNLIMITED,
  64. .command = bitbang_spi_send_command,
  65. .multicommand = default_spi_send_multicommand,
  66. .read = default_spi_read,
  67. .write_256 = default_spi_write_256,
  68. };
  69. int bitbang_spi_init(const struct bitbang_spi_master *master, int halfperiod)
  70. {
  71. /* BITBANG_SPI_INVALID is 0, so if someone forgot to initialize ->type,
  72. * we catch it here. Same goes for missing initialization of bitbanging
  73. * functions.
  74. */
  75. if (!master || master->type == BITBANG_SPI_INVALID || !master->set_cs ||
  76. !master->set_sck || !master->set_mosi || !master->get_miso) {
  77. msg_perr("Incomplete SPI bitbang master setting!\n"
  78. "Please report a bug at flashrom@flashrom.org\n");
  79. return 1;
  80. }
  81. if (bitbang_spi_master) {
  82. msg_perr("SPI bitbang master already initialized!\n"
  83. "Please report a bug at flashrom@flashrom.org\n");
  84. return 1;
  85. }
  86. bitbang_spi_master = master;
  87. bitbang_spi_half_period = halfperiod;
  88. register_spi_programmer(&spi_programmer_bitbang);
  89. /* FIXME: Run bitbang_spi_request_bus here or in programmer init? */
  90. bitbang_spi_set_cs(1);
  91. bitbang_spi_set_sck(0);
  92. bitbang_spi_set_mosi(0);
  93. return 0;
  94. }
  95. int bitbang_spi_shutdown(const struct bitbang_spi_master *master)
  96. {
  97. if (!bitbang_spi_master) {
  98. msg_perr("Shutting down an uninitialized SPI bitbang master!\n"
  99. "Please report a bug at flashrom@flashrom.org\n");
  100. return 1;
  101. }
  102. if (master != bitbang_spi_master) {
  103. msg_perr("Shutting down a mismatched SPI bitbang master!\n"
  104. "Please report a bug at flashrom@flashrom.org\n");
  105. return 1;
  106. }
  107. /* FIXME: Run bitbang_spi_release_bus here or per command? */
  108. bitbang_spi_master = NULL;
  109. return 0;
  110. }
  111. static uint8_t bitbang_spi_readwrite_byte(uint8_t val)
  112. {
  113. uint8_t ret = 0;
  114. int i;
  115. for (i = 7; i >= 0; i--) {
  116. bitbang_spi_set_mosi((val >> i) & 1);
  117. programmer_delay(bitbang_spi_half_period);
  118. bitbang_spi_set_sck(1);
  119. ret <<= 1;
  120. ret |= bitbang_spi_get_miso();
  121. programmer_delay(bitbang_spi_half_period);
  122. bitbang_spi_set_sck(0);
  123. }
  124. return ret;
  125. }
  126. static int bitbang_spi_send_command(const struct flashctx *flash,
  127. unsigned int writecnt, unsigned int readcnt,
  128. const unsigned char *writearr,
  129. unsigned char *readarr)
  130. {
  131. int i;
  132. /* FIXME: Run bitbang_spi_request_bus here or in programmer init?
  133. * Requesting and releasing the SPI bus is handled in here to allow the
  134. * programmer to use its own SPI engine for native accesses.
  135. */
  136. bitbang_spi_request_bus();
  137. bitbang_spi_set_cs(0);
  138. for (i = 0; i < writecnt; i++)
  139. bitbang_spi_readwrite_byte(writearr[i]);
  140. for (i = 0; i < readcnt; i++)
  141. readarr[i] = bitbang_spi_readwrite_byte(0);
  142. programmer_delay(bitbang_spi_half_period);
  143. bitbang_spi_set_cs(1);
  144. programmer_delay(bitbang_spi_half_period);
  145. /* FIXME: Run bitbang_spi_release_bus here or in programmer init? */
  146. bitbang_spi_release_bus();
  147. return 0;
  148. }