sh_spi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * SH SPI driver
  3. *
  4. * Copyright (C) 2011-2012 Renesas Solutions Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #include <common.h>
  21. #include <malloc.h>
  22. #include <spi.h>
  23. #include <asm/io.h>
  24. #include "sh_spi.h"
  25. static void sh_spi_write(unsigned long data, unsigned long *reg)
  26. {
  27. writel(data, reg);
  28. }
  29. static unsigned long sh_spi_read(unsigned long *reg)
  30. {
  31. return readl(reg);
  32. }
  33. static void sh_spi_set_bit(unsigned long val, unsigned long *reg)
  34. {
  35. unsigned long tmp;
  36. tmp = sh_spi_read(reg);
  37. tmp |= val;
  38. sh_spi_write(tmp, reg);
  39. }
  40. static void sh_spi_clear_bit(unsigned long val, unsigned long *reg)
  41. {
  42. unsigned long tmp;
  43. tmp = sh_spi_read(reg);
  44. tmp &= ~val;
  45. sh_spi_write(tmp, reg);
  46. }
  47. static void clear_fifo(struct sh_spi *ss)
  48. {
  49. sh_spi_set_bit(SH_SPI_RSTF, &ss->regs->cr2);
  50. sh_spi_clear_bit(SH_SPI_RSTF, &ss->regs->cr2);
  51. }
  52. static int recvbuf_wait(struct sh_spi *ss)
  53. {
  54. while (sh_spi_read(&ss->regs->cr1) & SH_SPI_RBE) {
  55. if (ctrlc())
  56. return 1;
  57. udelay(10);
  58. }
  59. return 0;
  60. }
  61. static int write_fifo_empty_wait(struct sh_spi *ss)
  62. {
  63. while (!(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBE)) {
  64. if (ctrlc())
  65. return 1;
  66. udelay(10);
  67. }
  68. return 0;
  69. }
  70. void spi_init(void)
  71. {
  72. }
  73. static void sh_spi_set_cs(struct sh_spi *ss, unsigned int cs)
  74. {
  75. unsigned long val = 0;
  76. if (cs & 0x01)
  77. val |= SH_SPI_SSS0;
  78. if (cs & 0x02)
  79. val |= SH_SPI_SSS1;
  80. sh_spi_clear_bit(SH_SPI_SSS0 | SH_SPI_SSS1, &ss->regs->cr4);
  81. sh_spi_set_bit(val, &ss->regs->cr4);
  82. }
  83. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  84. unsigned int max_hz, unsigned int mode)
  85. {
  86. struct sh_spi *ss;
  87. if (!spi_cs_is_valid(bus, cs))
  88. return NULL;
  89. ss = spi_alloc_slave(struct sh_spi, bus, cs);
  90. if (!ss)
  91. return NULL;
  92. ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
  93. /* SPI sycle stop */
  94. sh_spi_write(0xfe, &ss->regs->cr1);
  95. /* CR1 init */
  96. sh_spi_write(0x00, &ss->regs->cr1);
  97. /* CR3 init */
  98. sh_spi_write(0x00, &ss->regs->cr3);
  99. sh_spi_set_cs(ss, cs);
  100. clear_fifo(ss);
  101. /* 1/8 clock */
  102. sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
  103. udelay(10);
  104. return &ss->slave;
  105. }
  106. void spi_free_slave(struct spi_slave *slave)
  107. {
  108. struct sh_spi *spi = to_sh_spi(slave);
  109. free(spi);
  110. }
  111. int spi_claim_bus(struct spi_slave *slave)
  112. {
  113. return 0;
  114. }
  115. void spi_release_bus(struct spi_slave *slave)
  116. {
  117. struct sh_spi *ss = to_sh_spi(slave);
  118. sh_spi_write(sh_spi_read(&ss->regs->cr1) &
  119. ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
  120. }
  121. static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
  122. unsigned int len, unsigned long flags)
  123. {
  124. int i, cur_len, ret = 0;
  125. int remain = (int)len;
  126. unsigned long tmp;
  127. if (len >= SH_SPI_FIFO_SIZE)
  128. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  129. while (remain > 0) {
  130. cur_len = (remain < SH_SPI_FIFO_SIZE) ?
  131. remain : SH_SPI_FIFO_SIZE;
  132. for (i = 0; i < cur_len &&
  133. !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
  134. !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
  135. i++)
  136. sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
  137. cur_len = i;
  138. if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
  139. /* Abort the transaction */
  140. flags |= SPI_XFER_END;
  141. sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
  142. ret = 1;
  143. break;
  144. }
  145. remain -= cur_len;
  146. tx_data += cur_len;
  147. if (remain > 0)
  148. write_fifo_empty_wait(ss);
  149. }
  150. if (flags & SPI_XFER_END) {
  151. tmp = sh_spi_read(&ss->regs->cr1);
  152. tmp = tmp & ~(SH_SPI_SSD | SH_SPI_SSDB);
  153. sh_spi_write(tmp, &ss->regs->cr1);
  154. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  155. udelay(100);
  156. write_fifo_empty_wait(ss);
  157. }
  158. return ret;
  159. }
  160. static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
  161. unsigned int len, unsigned long flags)
  162. {
  163. int i;
  164. unsigned long tmp;
  165. if (len > SH_SPI_MAX_BYTE)
  166. sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
  167. else
  168. sh_spi_write(len, &ss->regs->cr3);
  169. tmp = sh_spi_read(&ss->regs->cr1);
  170. tmp = tmp & ~(SH_SPI_SSD | SH_SPI_SSDB);
  171. sh_spi_write(tmp, &ss->regs->cr1);
  172. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  173. for (i = 0; i < len; i++) {
  174. if (recvbuf_wait(ss))
  175. return 0;
  176. rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
  177. }
  178. sh_spi_write(0, &ss->regs->cr3);
  179. return 0;
  180. }
  181. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  182. void *din, unsigned long flags)
  183. {
  184. struct sh_spi *ss = to_sh_spi(slave);
  185. const unsigned char *tx_data = dout;
  186. unsigned char *rx_data = din;
  187. unsigned int len = bitlen / 8;
  188. int ret = 0;
  189. if (flags & SPI_XFER_BEGIN)
  190. sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
  191. &ss->regs->cr1);
  192. if (tx_data)
  193. ret = sh_spi_send(ss, tx_data, len, flags);
  194. if (ret == 0 && rx_data)
  195. ret = sh_spi_receive(ss, rx_data, len, flags);
  196. if (flags & SPI_XFER_END) {
  197. sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
  198. udelay(100);
  199. sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
  200. &ss->regs->cr1);
  201. clear_fifo(ss);
  202. }
  203. return ret;
  204. }
  205. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  206. {
  207. if (!bus && cs < SH_SPI_NUM_CS)
  208. return 1;
  209. else
  210. return 0;
  211. }
  212. void spi_cs_activate(struct spi_slave *slave)
  213. {
  214. }
  215. void spi_cs_deactivate(struct spi_slave *slave)
  216. {
  217. }