kirkwood_spi.c 4.4 KB

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