kirkwood_spi.c 5.5 KB

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