soft_spi.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * (C) Copyright 2002
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  4. *
  5. * Influenced by code from:
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <spi.h>
  28. #if defined(CONFIG_SOFT_SPI)
  29. #define DEBUG_SPI
  30. /*-----------------------------------------------------------------------
  31. * Definitions
  32. */
  33. #ifdef DEBUG_SPI
  34. #define PRINTD(fmt,args...) printf (fmt ,##args)
  35. #else
  36. #define PRINTD(fmt,args...)
  37. #endif
  38. /*=====================================================================*/
  39. /* Public Functions */
  40. /*=====================================================================*/
  41. /*-----------------------------------------------------------------------
  42. * Initialization
  43. */
  44. void spi_init (void)
  45. {
  46. #ifdef SPI_INIT
  47. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  48. SPI_INIT;
  49. #endif
  50. }
  51. /*-----------------------------------------------------------------------
  52. * SPI transfer
  53. *
  54. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  55. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  56. *
  57. * The source of the outgoing bits is the "dout" parameter and the
  58. * destination of the input bits is the "din" parameter. Note that "dout"
  59. * and "din" can point to the same memory location, in which case the
  60. * input data overwrites the output data (since both are buffered by
  61. * temporary variables, this is OK).
  62. *
  63. * If the chipsel() function is not NULL, it is called with a parameter
  64. * of '1' (chip select active) at the start of the transfer and again with
  65. * a parameter of '0' at the end of the transfer.
  66. *
  67. * If the chipsel() function _is_ NULL, it the responsibility of the
  68. * caller to make the appropriate chip select active before calling
  69. * spi_xfer() and making it inactive after spi_xfer() returns.
  70. */
  71. int spi_xfer(spi_chipsel_type chipsel, int bitlen, uchar *dout, uchar *din)
  72. {
  73. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  74. uchar tmpdin = 0;
  75. uchar tmpdout = 0;
  76. int j;
  77. PRINTD("spi_xfer: chipsel %08X dout %08X din %08X bitlen %d\n",
  78. (int)chipsel, *(uint *)dout, *(uint *)din, bitlen);
  79. if(chipsel != NULL) {
  80. (*chipsel)(1); /* select the target chip */
  81. }
  82. for(j = 0; j < bitlen; j++) {
  83. /*
  84. * Check if it is time to work on a new byte.
  85. */
  86. if((j % 8) == 0) {
  87. tmpdout = *dout++;
  88. if(j != 0) {
  89. *din++ = tmpdin;
  90. }
  91. tmpdin = 0;
  92. }
  93. SPI_SCL(0);
  94. SPI_SDA(tmpdout & 0x80);
  95. SPI_DELAY;
  96. SPI_SCL(1);
  97. SPI_DELAY;
  98. tmpdin <<= 1;
  99. tmpdin |= SPI_READ;
  100. tmpdout <<= 1;
  101. }
  102. /*
  103. * If the number of bits isn't a multiple of 8, shift the last
  104. * bits over to left-justify them. Then store the last byte
  105. * read in.
  106. */
  107. if((bitlen % 8) != 0)
  108. tmpdin <<= 8 - (bitlen % 8);
  109. *din++ = tmpdin;
  110. SPI_SCL(0); /* SPI wants the clock left low for idle */
  111. if(chipsel != NULL) {
  112. (*chipsel)(0); /* deselect the target chip */
  113. }
  114. return(0);
  115. }
  116. #endif /* CONFIG_SOFT_SPI */