sh_spi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SH SPI driver
  4. *
  5. * Copyright (C) 2011-2012 Renesas Solutions Corp.
  6. */
  7. #include <common.h>
  8. #include <console.h>
  9. #include <malloc.h>
  10. #include <spi.h>
  11. #include <asm/io.h>
  12. #include "sh_spi.h"
  13. static void sh_spi_write(unsigned long data, unsigned long *reg)
  14. {
  15. writel(data, reg);
  16. }
  17. static unsigned long sh_spi_read(unsigned long *reg)
  18. {
  19. return readl(reg);
  20. }
  21. static void sh_spi_set_bit(unsigned long val, unsigned long *reg)
  22. {
  23. unsigned long tmp;
  24. tmp = sh_spi_read(reg);
  25. tmp |= val;
  26. sh_spi_write(tmp, reg);
  27. }
  28. static void sh_spi_clear_bit(unsigned long val, unsigned long *reg)
  29. {
  30. unsigned long tmp;
  31. tmp = sh_spi_read(reg);
  32. tmp &= ~val;
  33. sh_spi_write(tmp, reg);
  34. }
  35. static void clear_fifo(struct sh_spi *ss)
  36. {
  37. sh_spi_set_bit(SH_SPI_RSTF, &ss->regs->cr2);
  38. sh_spi_clear_bit(SH_SPI_RSTF, &ss->regs->cr2);
  39. }
  40. static int recvbuf_wait(struct sh_spi *ss)
  41. {
  42. while (sh_spi_read(&ss->regs->cr1) & SH_SPI_RBE) {
  43. if (ctrlc())
  44. return 1;
  45. udelay(10);
  46. }
  47. return 0;
  48. }
  49. static int write_fifo_empty_wait(struct sh_spi *ss)
  50. {
  51. while (!(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBE)) {
  52. if (ctrlc())
  53. return 1;
  54. udelay(10);
  55. }
  56. return 0;
  57. }
  58. static void sh_spi_set_cs(struct sh_spi *ss, unsigned int cs)
  59. {
  60. unsigned long val = 0;
  61. if (cs & 0x01)
  62. val |= SH_SPI_SSS0;
  63. if (cs & 0x02)
  64. val |= SH_SPI_SSS1;
  65. sh_spi_clear_bit(SH_SPI_SSS0 | SH_SPI_SSS1, &ss->regs->cr4);
  66. sh_spi_set_bit(val, &ss->regs->cr4);
  67. }
  68. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  69. unsigned int max_hz, unsigned int mode)
  70. {
  71. struct sh_spi *ss;
  72. if (!spi_cs_is_valid(bus, cs))
  73. return NULL;
  74. ss = spi_alloc_slave(struct sh_spi, bus, cs);
  75. if (!ss)
  76. return NULL;
  77. ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
  78. /* SPI sycle stop */
  79. sh_spi_write(0xfe, &ss->regs->cr1);
  80. /* CR1 init */
  81. sh_spi_write(0x00, &ss->regs->cr1);
  82. /* CR3 init */
  83. sh_spi_write(0x00, &ss->regs->cr3);
  84. sh_spi_set_cs(ss, cs);
  85. clear_fifo(ss);
  86. /* 1/8 clock */
  87. sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
  88. udelay(10);
  89. return &ss->slave;
  90. }
  91. void spi_free_slave(struct spi_slave *slave)
  92. {
  93. struct sh_spi *spi = to_sh_spi(slave);
  94. free(spi);
  95. }
  96. int spi_claim_bus(struct spi_slave *slave)
  97. {
  98. return 0;
  99. }
  100. void spi_release_bus(struct spi_slave *slave)
  101. {
  102. struct sh_spi *ss = to_sh_spi(slave);
  103. sh_spi_write(sh_spi_read(&ss->regs->cr1) &
  104. ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
  105. }
  106. static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
  107. unsigned int len, unsigned long flags)
  108. {
  109. int i, cur_len, ret = 0;
  110. int remain = (int)len;
  111. if (len >= SH_SPI_FIFO_SIZE)
  112. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  113. while (remain > 0) {
  114. cur_len = (remain < SH_SPI_FIFO_SIZE) ?
  115. remain : SH_SPI_FIFO_SIZE;
  116. for (i = 0; i < cur_len &&
  117. !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
  118. !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
  119. i++)
  120. sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
  121. cur_len = i;
  122. if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
  123. /* Abort the transaction */
  124. flags |= SPI_XFER_END;
  125. sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
  126. ret = 1;
  127. break;
  128. }
  129. remain -= cur_len;
  130. tx_data += cur_len;
  131. if (remain > 0)
  132. write_fifo_empty_wait(ss);
  133. }
  134. if (flags & SPI_XFER_END) {
  135. sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
  136. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  137. udelay(100);
  138. write_fifo_empty_wait(ss);
  139. }
  140. return ret;
  141. }
  142. static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
  143. unsigned int len, unsigned long flags)
  144. {
  145. int i;
  146. if (len > SH_SPI_MAX_BYTE)
  147. sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
  148. else
  149. sh_spi_write(len, &ss->regs->cr3);
  150. sh_spi_clear_bit(SH_SPI_SSD | SH_SPI_SSDB, &ss->regs->cr1);
  151. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  152. for (i = 0; i < len; i++) {
  153. if (recvbuf_wait(ss))
  154. return 0;
  155. rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
  156. }
  157. sh_spi_write(0, &ss->regs->cr3);
  158. return 0;
  159. }
  160. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  161. void *din, unsigned long flags)
  162. {
  163. struct sh_spi *ss = to_sh_spi(slave);
  164. const unsigned char *tx_data = dout;
  165. unsigned char *rx_data = din;
  166. unsigned int len = bitlen / 8;
  167. int ret = 0;
  168. if (flags & SPI_XFER_BEGIN)
  169. sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
  170. &ss->regs->cr1);
  171. if (tx_data)
  172. ret = sh_spi_send(ss, tx_data, len, flags);
  173. if (ret == 0 && rx_data)
  174. ret = sh_spi_receive(ss, rx_data, len, flags);
  175. if (flags & SPI_XFER_END) {
  176. sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
  177. udelay(100);
  178. sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
  179. &ss->regs->cr1);
  180. clear_fifo(ss);
  181. }
  182. return ret;
  183. }
  184. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  185. {
  186. if (!bus && cs < SH_SPI_NUM_CS)
  187. return 1;
  188. else
  189. return 0;
  190. }
  191. void spi_cs_activate(struct spi_slave *slave)
  192. {
  193. }
  194. void spi_cs_deactivate(struct spi_slave *slave)
  195. {
  196. }