kirkwood_spi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. u32 cs_spi_mpp_back[2];
  35. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  36. unsigned int max_hz, unsigned int mode)
  37. {
  38. struct spi_slave *slave;
  39. u32 data;
  40. u32 kwspi_mpp_config[] = { 0, 0 };
  41. if (!spi_cs_is_valid(bus, cs))
  42. return NULL;
  43. slave = malloc(sizeof(struct spi_slave));
  44. if (!slave)
  45. return NULL;
  46. slave->bus = bus;
  47. slave->cs = cs;
  48. writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl);
  49. /* calculate spi clock prescaller using max_hz */
  50. data = ((CONFIG_SYS_TCLK / 2) / max_hz) & KWSPI_CLKPRESCL_MASK;
  51. data |= 0x10;
  52. /* program spi clock prescaller using max_hz */
  53. writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
  54. debug("data = 0x%08x \n", data);
  55. writel(KWSPI_SMEMRDIRQ, &spireg->irq_cause);
  56. writel(KWSPI_IRQMASK, &spireg->irq_mask);
  57. /* program mpp registers to select SPI_CSn */
  58. if (cs) {
  59. kwspi_mpp_config[0] = MPP7_SPI_SCn;
  60. } else {
  61. kwspi_mpp_config[0] = MPP0_SPI_SCn;
  62. }
  63. kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back);
  64. return slave;
  65. }
  66. void spi_free_slave(struct spi_slave *slave)
  67. {
  68. kirkwood_mpp_conf(cs_spi_mpp_back, NULL);
  69. free(slave);
  70. }
  71. #if defined(CONFIG_SYS_KW_SPI_MPP)
  72. u32 spi_mpp_backup[4];
  73. #endif
  74. __attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
  75. {
  76. return 0;
  77. }
  78. int spi_claim_bus(struct spi_slave *slave)
  79. {
  80. #if defined(CONFIG_SYS_KW_SPI_MPP)
  81. u32 config;
  82. u32 spi_mpp_config[4];
  83. config = CONFIG_SYS_KW_SPI_MPP;
  84. if (config & MOSI_MPP6)
  85. spi_mpp_config[0] = MPP6_SPI_MOSI;
  86. else
  87. spi_mpp_config[0] = MPP1_SPI_MOSI;
  88. if (config & SCK_MPP10)
  89. spi_mpp_config[1] = MPP10_SPI_SCK;
  90. else
  91. spi_mpp_config[1] = MPP2_SPI_SCK;
  92. if (config & MISO_MPP11)
  93. spi_mpp_config[2] = MPP11_SPI_MISO;
  94. else
  95. spi_mpp_config[2] = MPP3_SPI_MISO;
  96. spi_mpp_config[3] = 0;
  97. spi_mpp_backup[3] = 0;
  98. /* set new spi mpp and save current mpp config */
  99. kirkwood_mpp_conf(spi_mpp_config, spi_mpp_backup);
  100. #endif
  101. return board_spi_claim_bus(slave);
  102. }
  103. __attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
  104. {
  105. }
  106. void spi_release_bus(struct spi_slave *slave)
  107. {
  108. #if defined(CONFIG_SYS_KW_SPI_MPP)
  109. kirkwood_mpp_conf(spi_mpp_backup, NULL);
  110. #endif
  111. board_spi_release_bus(slave);
  112. }
  113. #ifndef CONFIG_SPI_CS_IS_VALID
  114. /*
  115. * you can define this function board specific
  116. * define above CONFIG in board specific config file and
  117. * provide the function in board specific src file
  118. */
  119. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  120. {
  121. return (bus == 0 && (cs == 0 || cs == 1));
  122. }
  123. #endif
  124. void spi_init(void)
  125. {
  126. }
  127. void spi_cs_activate(struct spi_slave *slave)
  128. {
  129. writel(readl(&spireg->ctrl) | KWSPI_IRQUNMASK, &spireg->ctrl);
  130. }
  131. void spi_cs_deactivate(struct spi_slave *slave)
  132. {
  133. writel(readl(&spireg->ctrl) & KWSPI_IRQMASK, &spireg->ctrl);
  134. }
  135. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  136. void *din, unsigned long flags)
  137. {
  138. unsigned int tmpdout, tmpdin;
  139. int tm, isread = 0;
  140. debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n",
  141. slave->bus, slave->cs, dout, din, bitlen);
  142. if (flags & SPI_XFER_BEGIN)
  143. spi_cs_activate(slave);
  144. /*
  145. * handle data in 8-bit chunks
  146. * TBD: 2byte xfer mode to be enabled
  147. */
  148. writel(((readl(&spireg->cfg) & ~KWSPI_XFERLEN_MASK) |
  149. KWSPI_XFERLEN_1BYTE), &spireg->cfg);
  150. while (bitlen > 4) {
  151. debug("loopstart bitlen %d\n", bitlen);
  152. tmpdout = 0;
  153. /* Shift data so it's msb-justified */
  154. if (dout)
  155. tmpdout = *(u32 *) dout & 0x0ff;
  156. writel(~KWSPI_SMEMRDIRQ, &spireg->irq_cause);
  157. writel(tmpdout, &spireg->dout); /* Write the data out */
  158. debug("*** spi_xfer: ... %08x written, bitlen %d\n",
  159. tmpdout, bitlen);
  160. /*
  161. * Wait for SPI transmit to get out
  162. * or time out (1 second = 1000 ms)
  163. * The NE event must be read and cleared first
  164. */
  165. for (tm = 0, isread = 0; tm < KWSPI_TIMEOUT; ++tm) {
  166. if (readl(&spireg->irq_cause) & KWSPI_SMEMRDIRQ) {
  167. isread = 1;
  168. tmpdin = readl(&spireg->din);
  169. debug
  170. ("spi_xfer: din %p..%08x read\n",
  171. din, tmpdin);
  172. if (din) {
  173. *((u8 *) din) = (u8) tmpdin;
  174. din += 1;
  175. }
  176. if (dout)
  177. dout += 1;
  178. bitlen -= 8;
  179. }
  180. if (isread)
  181. break;
  182. }
  183. if (tm >= KWSPI_TIMEOUT)
  184. printf("*** spi_xfer: Time out during SPI transfer\n");
  185. debug("loopend bitlen %d\n", bitlen);
  186. }
  187. if (flags & SPI_XFER_END)
  188. spi_cs_deactivate(slave);
  189. return 0;
  190. }