sh_spi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 = malloc(sizeof(struct spi_slave));
  90. if (!ss)
  91. return NULL;
  92. ss->slave.bus = bus;
  93. ss->slave.cs = cs;
  94. ss->regs = (struct sh_spi_regs *)CONFIG_SH_SPI_BASE;
  95. /* SPI sycle stop */
  96. sh_spi_write(0xfe, &ss->regs->cr1);
  97. /* CR1 init */
  98. sh_spi_write(0x00, &ss->regs->cr1);
  99. /* CR3 init */
  100. sh_spi_write(0x00, &ss->regs->cr3);
  101. sh_spi_set_cs(ss, cs);
  102. clear_fifo(ss);
  103. /* 1/8 clock */
  104. sh_spi_write(sh_spi_read(&ss->regs->cr2) | 0x07, &ss->regs->cr2);
  105. udelay(10);
  106. return &ss->slave;
  107. }
  108. void spi_free_slave(struct spi_slave *slave)
  109. {
  110. struct sh_spi *spi = to_sh_spi(slave);
  111. free(spi);
  112. }
  113. int spi_claim_bus(struct spi_slave *slave)
  114. {
  115. return 0;
  116. }
  117. void spi_release_bus(struct spi_slave *slave)
  118. {
  119. struct sh_spi *ss = to_sh_spi(slave);
  120. sh_spi_write(sh_spi_read(&ss->regs->cr1) &
  121. ~(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD), &ss->regs->cr1);
  122. }
  123. static int sh_spi_send(struct sh_spi *ss, const unsigned char *tx_data,
  124. unsigned int len, unsigned long flags)
  125. {
  126. int i, cur_len, ret = 0;
  127. int remain = (int)len;
  128. unsigned long tmp;
  129. if (len >= SH_SPI_FIFO_SIZE)
  130. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  131. while (remain > 0) {
  132. cur_len = (remain < SH_SPI_FIFO_SIZE) ?
  133. remain : SH_SPI_FIFO_SIZE;
  134. for (i = 0; i < cur_len &&
  135. !(sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) &&
  136. !(sh_spi_read(&ss->regs->cr1) & SH_SPI_TBF);
  137. i++)
  138. sh_spi_write(tx_data[i], &ss->regs->tbr_rbr);
  139. cur_len = i;
  140. if (sh_spi_read(&ss->regs->cr4) & SH_SPI_WPABRT) {
  141. /* Abort the transaction */
  142. flags |= SPI_XFER_END;
  143. sh_spi_set_bit(SH_SPI_WPABRT, &ss->regs->cr4);
  144. ret = 1;
  145. break;
  146. }
  147. remain -= cur_len;
  148. tx_data += cur_len;
  149. if (remain > 0)
  150. write_fifo_empty_wait(ss);
  151. }
  152. if (flags & SPI_XFER_END) {
  153. tmp = sh_spi_read(&ss->regs->cr1);
  154. tmp = tmp & ~(SH_SPI_SSD | SH_SPI_SSDB);
  155. sh_spi_write(tmp, &ss->regs->cr1);
  156. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  157. udelay(100);
  158. write_fifo_empty_wait(ss);
  159. }
  160. return ret;
  161. }
  162. static int sh_spi_receive(struct sh_spi *ss, unsigned char *rx_data,
  163. unsigned int len, unsigned long flags)
  164. {
  165. int i;
  166. unsigned long tmp;
  167. if (len > SH_SPI_MAX_BYTE)
  168. sh_spi_write(SH_SPI_MAX_BYTE, &ss->regs->cr3);
  169. else
  170. sh_spi_write(len, &ss->regs->cr3);
  171. tmp = sh_spi_read(&ss->regs->cr1);
  172. tmp = tmp & ~(SH_SPI_SSD | SH_SPI_SSDB);
  173. sh_spi_write(tmp, &ss->regs->cr1);
  174. sh_spi_set_bit(SH_SPI_SSA, &ss->regs->cr1);
  175. for (i = 0; i < len; i++) {
  176. if (recvbuf_wait(ss))
  177. return 0;
  178. rx_data[i] = (unsigned char)sh_spi_read(&ss->regs->tbr_rbr);
  179. }
  180. sh_spi_write(0, &ss->regs->cr3);
  181. return 0;
  182. }
  183. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  184. void *din, unsigned long flags)
  185. {
  186. struct sh_spi *ss = to_sh_spi(slave);
  187. const unsigned char *tx_data = dout;
  188. unsigned char *rx_data = din;
  189. unsigned int len = bitlen / 8;
  190. int ret = 0;
  191. if (flags & SPI_XFER_BEGIN)
  192. sh_spi_write(sh_spi_read(&ss->regs->cr1) & ~SH_SPI_SSA,
  193. &ss->regs->cr1);
  194. if (tx_data)
  195. ret = sh_spi_send(ss, tx_data, len, flags);
  196. if (ret == 0 && rx_data)
  197. ret = sh_spi_receive(ss, rx_data, len, flags);
  198. if (flags & SPI_XFER_END) {
  199. sh_spi_set_bit(SH_SPI_SSD, &ss->regs->cr1);
  200. udelay(100);
  201. sh_spi_clear_bit(SH_SPI_SSA | SH_SPI_SSDB | SH_SPI_SSD,
  202. &ss->regs->cr1);
  203. clear_fifo(ss);
  204. }
  205. return ret;
  206. }
  207. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  208. {
  209. if (!bus && cs < SH_SPI_NUM_CS)
  210. return 1;
  211. else
  212. return 0;
  213. }
  214. void spi_cs_activate(struct spi_slave *slave)
  215. {
  216. }
  217. void spi_cs_deactivate(struct spi_slave *slave)
  218. {
  219. }