kirkwood_spi.c 5.5 KB

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