mpc8xxx_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
  3. * With help from the common/soft_spi and cpu/mpc8260 drivers
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #if defined(CONFIG_MPC8XXX_SPI) && defined(CONFIG_HARD_SPI)
  25. #include <spi.h>
  26. #include <asm/mpc8xxx_spi.h>
  27. #define SPI_EV_NE (0x80000000 >> 22) /* Receiver Not Empty */
  28. #define SPI_EV_NF (0x80000000 >> 23) /* Transmitter Not Full */
  29. #define SPI_MODE_LOOP (0x80000000 >> 1) /* Loopback mode */
  30. #define SPI_MODE_REV (0x80000000 >> 5) /* Reverse mode - MSB first */
  31. #define SPI_MODE_MS (0x80000000 >> 6) /* Always master */
  32. #define SPI_MODE_EN (0x80000000 >> 7) /* Enable interface */
  33. #define SPI_TIMEOUT 1000
  34. void spi_init(void)
  35. {
  36. volatile spi8xxx_t *spi = &((immap_t *) (CFG_IMMR))->spi;
  37. /*
  38. * SPI pins on the MPC83xx are not muxed, so all we do is initialize
  39. * some registers
  40. */
  41. spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
  42. spi->mode = (spi->mode & 0xfff0ffff) | (1 << 16); /* Use SYSCLK / 8
  43. (16.67MHz typ.) */
  44. spi->event = 0xffffffff; /* Clear all SPI events */
  45. spi->mask = 0x00000000; /* Mask all SPI interrupts */
  46. spi->com = 0; /* LST bit doesn't do anything, so disregard */
  47. }
  48. int spi_xfer(spi_chipsel_type chipsel, int bitlen, uchar *dout, uchar *din)
  49. {
  50. volatile spi8xxx_t *spi = &((immap_t *) (CFG_IMMR))->spi;
  51. unsigned int tmpdout, tmpdin, event;
  52. int numBlks = bitlen / 32 + (bitlen % 32 ? 1 : 0);
  53. int tm, isRead = 0;
  54. unsigned char charSize = 32;
  55. debug("spi_xfer: chipsel %08X dout %08X din %08X bitlen %d\n",
  56. (int)chipsel, *(uint *) dout, *(uint *) din, bitlen);
  57. if (chipsel != NULL)
  58. (*chipsel) (1); /* select the target chip */
  59. spi->event = 0xffffffff; /* Clear all SPI events */
  60. /* handle data in 32-bit chunks */
  61. while (numBlks--) {
  62. tmpdout = 0;
  63. charSize = (bitlen >= 32 ? 32 : bitlen);
  64. /* Shift data so it's msb-justified */
  65. tmpdout = *(u32 *) dout >> (32 - charSize);
  66. /* The LEN field of the SPMODE register is set as follows:
  67. *
  68. * Bit length setting
  69. * len <= 4 3
  70. * 4 < len <= 16 len - 1
  71. * len > 16 0
  72. */
  73. if (bitlen <= 16) {
  74. if (bitlen <= 4)
  75. spi->mode = (spi->mode & 0xff0fffff) |
  76. (3 << 20);
  77. else
  78. spi->mode = (spi->mode & 0xff0fffff) |
  79. ((bitlen - 1) << 20);
  80. } else {
  81. spi->mode = (spi->mode & 0xff0fffff);
  82. /* Set up the next iteration if sending > 32 bits */
  83. bitlen -= 32;
  84. dout += 4;
  85. }
  86. spi->tx = tmpdout; /* Write the data out */
  87. debug("*** spi_xfer: ... %08x written\n", tmpdout);
  88. /*
  89. * Wait for SPI transmit to get out
  90. * or time out (1 second = 1000 ms)
  91. * The NE event must be read and cleared first
  92. */
  93. for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
  94. event = spi->event;
  95. if (event & SPI_EV_NE) {
  96. tmpdin = spi->rx;
  97. spi->event |= SPI_EV_NE;
  98. isRead = 1;
  99. *(u32 *) din = (tmpdin << (32 - charSize));
  100. if (charSize == 32) {
  101. /* Advance output buffer by 32 bits */
  102. din += 4;
  103. }
  104. }
  105. /*
  106. * Only bail when we've had both NE and NF events.
  107. * This will cause timeouts on RO devices, so maybe
  108. * in the future put an arbitrary delay after writing
  109. * the device. Arbitrary delays suck, though...
  110. */
  111. if (isRead && (event & SPI_EV_NF))
  112. break;
  113. }
  114. if (tm >= SPI_TIMEOUT)
  115. puts("*** spi_xfer: Time out during SPI transfer");
  116. debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
  117. }
  118. if (chipsel != NULL)
  119. (*chipsel) (0); /* deselect the target chip */
  120. return 0;
  121. }
  122. #endif /* CONFIG_HARD_SPI */