kirkwood_spi.c 4.8 KB

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