kirkwood_spi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * Derived from drivers/spi/mpc8xxx_spi.c
  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., 51 Franklin Street, Fifth Floor, Boston,
  24. * MA 02110-1301 USA
  25. */
  26. #include <common.h>
  27. #include <malloc.h>
  28. #include <spi.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/kirkwood.h>
  31. #include <asm/arch/spi.h>
  32. #include <asm/arch/mpp.h>
  33. static struct kwspi_registers *spireg = (struct kwspi_registers *)KW_SPI_BASE;
  34. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  35. unsigned int max_hz, unsigned int mode)
  36. {
  37. struct spi_slave *slave;
  38. u32 data;
  39. u32 kwspi_mpp_config[] = {
  40. MPP0_GPIO,
  41. MPP7_SPI_SCn,
  42. 0
  43. };
  44. if (!spi_cs_is_valid(bus, cs))
  45. return NULL;
  46. slave = malloc(sizeof(struct spi_slave));
  47. if (!slave)
  48. return NULL;
  49. slave->bus = bus;
  50. slave->cs = cs;
  51. writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl);
  52. /* calculate spi clock prescaller using max_hz */
  53. data = ((CONFIG_SYS_TCLK / 2) / max_hz) & KWSPI_CLKPRESCL_MASK;
  54. data |= 0x10;
  55. /* program spi clock prescaller using max_hz */
  56. writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
  57. debug("data = 0x%08x \n", data);
  58. writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
  59. writel(KWSPI_IRQMASK, spireg->irq_mask);
  60. /* program mpp registers to select SPI_CSn */
  61. if (cs) {
  62. kwspi_mpp_config[0] = MPP0_GPIO;
  63. kwspi_mpp_config[1] = MPP7_SPI_SCn;
  64. } else {
  65. kwspi_mpp_config[0] = MPP0_SPI_SCn;
  66. kwspi_mpp_config[1] = MPP7_GPO;
  67. }
  68. kirkwood_mpp_conf(kwspi_mpp_config);
  69. return slave;
  70. }
  71. void spi_free_slave(struct spi_slave *slave)
  72. {
  73. free(slave);
  74. }
  75. int spi_claim_bus(struct spi_slave *slave)
  76. {
  77. return 0;
  78. }
  79. void spi_release_bus(struct spi_slave *slave)
  80. {
  81. }
  82. #ifndef CONFIG_SPI_CS_IS_VALID
  83. /*
  84. * you can define this function board specific
  85. * define above CONFIG in board specific config file and
  86. * provide the function in board specific src file
  87. */
  88. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  89. {
  90. return (bus == 0 && (cs == 0 || cs == 1));
  91. }
  92. #endif
  93. void spi_cs_activate(struct spi_slave *slave)
  94. {
  95. writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
  96. }
  97. void spi_cs_deactivate(struct spi_slave *slave)
  98. {
  99. writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
  100. }
  101. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  102. void *din, unsigned long flags)
  103. {
  104. unsigned int tmpdout, tmpdin;
  105. int tm, isread = 0;
  106. debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  107. slave->bus, slave->cs, dout, din, bitlen);
  108. if (flags & SPI_XFER_BEGIN)
  109. spi_cs_activate(slave);
  110. /*
  111. * handle data in 8-bit chunks
  112. * TBD: 2byte xfer mode to be enabled
  113. */
  114. writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
  115. KWSPI_XFERLEN_1BYTE), &spireg->cfg);
  116. while (bitlen > 4) {
  117. debug("loopstart bitlen %d\n", bitlen);
  118. tmpdout = 0;
  119. /* Shift data so it's msb-justified */
  120. if (dout)
  121. tmpdout = *(u32 *) dout & 0x0ff;
  122. writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
  123. writel(tmpdout, &spireg->dout); /* Write the data out */
  124. debug("*** spi_xfer: ... %08x written, bitlen %d\n",
  125. tmpdout, bitlen);
  126. /*
  127. * Wait for SPI transmit to get out
  128. * or time out (1 second = 1000 ms)
  129. * The NE event must be read and cleared first
  130. */
  131. for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
  132. if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
  133. isread = 1;
  134. tmpdin = readl(&spireg->din);
  135. debug
  136. ("spi_xfer: din %08x..%08x read\n",
  137. din, tmpdin);
  138. if (din) {
  139. *((u8 *) din) = (u8) tmpdin;
  140. din += 1;
  141. }
  142. if (dout)
  143. dout += 1;
  144. bitlen -= 8;
  145. }
  146. if (isread)
  147. break;
  148. }
  149. if (tm >= KWSPI_TIMEOUT)
  150. printf("*** spi_xfer: Time out during SPI transfer\n");
  151. debug("loopend bitlen %d\n", bitlen);
  152. }
  153. if (flags & SPI_XFER_END)
  154. spi_cs_deactivate(slave);
  155. return 0;
  156. }