andes_spi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Driver of Andes SPI Controller
  3. *
  4. * (C) Copyright 2011 Andes Technology
  5. * Macpaul Lin <macpaul@andestech.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <spi.h>
  12. #include <asm/io.h>
  13. #include "andes_spi.h"
  14. void spi_init(void)
  15. {
  16. /* do nothing */
  17. }
  18. static void andes_spi_spit_en(struct andes_spi_slave *ds)
  19. {
  20. unsigned int dcr = readl(&ds->regs->dcr);
  21. debug("%s: dcr: %x, write value: %x\n",
  22. __func__, dcr, (dcr | ANDES_SPI_DCR_SPIT));
  23. writel((dcr | ANDES_SPI_DCR_SPIT), &ds->regs->dcr);
  24. }
  25. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  26. unsigned int max_hz, unsigned int mode)
  27. {
  28. struct andes_spi_slave *ds;
  29. if (!spi_cs_is_valid(bus, cs))
  30. return NULL;
  31. ds = spi_alloc_slave(struct andes_spi_slave, bus, cs);
  32. if (!ds)
  33. return NULL;
  34. ds->regs = (struct andes_spi_regs *)CONFIG_SYS_SPI_BASE;
  35. /*
  36. * The hardware of andes_spi will set its frequency according
  37. * to APB/AHB bus clock. Hence the hardware doesn't allow changing of
  38. * requency and so the user requested speed is always ignored.
  39. */
  40. ds->freq = max_hz;
  41. return &ds->slave;
  42. }
  43. void spi_free_slave(struct spi_slave *slave)
  44. {
  45. struct andes_spi_slave *ds = to_andes_spi(slave);
  46. free(ds);
  47. }
  48. int spi_claim_bus(struct spi_slave *slave)
  49. {
  50. struct andes_spi_slave *ds = to_andes_spi(slave);
  51. unsigned int apb;
  52. unsigned int baud;
  53. /* Enable the SPI hardware */
  54. writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
  55. udelay(1000);
  56. /* setup format */
  57. baud = ((CONFIG_SYS_CLK_FREQ / CONFIG_SYS_SPI_CLK / 2) - 1) & 0xFF;
  58. /*
  59. * SPI_CLK = AHB bus clock / ((BAUD + 1)*2)
  60. * BAUD = AHB bus clock / SPI_CLK / 2) - 1
  61. */
  62. apb = (readl(&ds->regs->apb) & 0xffffff00) | baud;
  63. writel(apb, &ds->regs->apb);
  64. /* no interrupts */
  65. writel(0, &ds->regs->ie);
  66. return 0;
  67. }
  68. void spi_release_bus(struct spi_slave *slave)
  69. {
  70. struct andes_spi_slave *ds = to_andes_spi(slave);
  71. /* Disable the SPI hardware */
  72. writel(ANDES_SPI_CR_SPIRST, &ds->regs->cr);
  73. }
  74. static int andes_spi_read(struct spi_slave *slave, unsigned int len,
  75. u8 *rxp, unsigned long flags)
  76. {
  77. struct andes_spi_slave *ds = to_andes_spi(slave);
  78. unsigned int i, left;
  79. unsigned int data;
  80. debug("%s: slave: %x, len: %d, rxp: %x, flags: %d\n",
  81. __func__, slave, len, rxp, flags);
  82. debug("%s: data: ", __func__);
  83. while (len > 0) {
  84. left = min(len, 4);
  85. data = readl(&ds->regs->data);
  86. debug(" ");
  87. for (i = 0; i < left; i++) {
  88. debug("%02x ", data & 0xff);
  89. *rxp++ = data;
  90. data >>= 8;
  91. len--;
  92. }
  93. }
  94. debug("\n");
  95. return 0;
  96. }
  97. static int andes_spi_write(struct spi_slave *slave, unsigned int wlen,
  98. unsigned int rlen, const u8 *txp, unsigned long flags)
  99. {
  100. struct andes_spi_slave *ds = to_andes_spi(slave);
  101. unsigned int data;
  102. unsigned int i, left;
  103. unsigned int spit_enabled = 0;
  104. debug("%s: slave: %x, wlen: %d, rlen: %d, txp: %x, flags: %x\n",
  105. __func__, slave, wlen, rlen, txp, flags);
  106. /* The value of wlen and rlen wrote to register must minus 1 */
  107. if (rlen == 0) /* write only */
  108. writel(ANDES_SPI_DCR_MODE_WO | ANDES_SPI_DCR_WCNT(wlen-1) |
  109. ANDES_SPI_DCR_RCNT(0), &ds->regs->dcr);
  110. else /* write then read */
  111. writel(ANDES_SPI_DCR_MODE_WR | ANDES_SPI_DCR_WCNT(wlen-1) |
  112. ANDES_SPI_DCR_RCNT(rlen-1), &ds->regs->dcr);
  113. /* wait till SPIBSY is cleared */
  114. while (readl(&ds->regs->st) & ANDES_SPI_ST_SPIBSY)
  115. ;
  116. /* data write process */
  117. debug("%s: txp: ", __func__);
  118. while (wlen > 0) {
  119. /* clear the data */
  120. data = 0;
  121. /* data are usually be read 32bits once a time */
  122. left = min(wlen, 4);
  123. for (i = 0; i < left; i++) {
  124. debug("%x ", *txp);
  125. data |= *txp++ << (i * 8);
  126. wlen--;
  127. }
  128. debug("\n");
  129. debug("data: %08x\n", data);
  130. debug("streg before write: %08x\n", readl(&ds->regs->st));
  131. /* wait till TXFULL is deasserted */
  132. while (readl(&ds->regs->st) & ANDES_SPI_ST_TXFEL)
  133. ;
  134. writel(data, &ds->regs->data);
  135. debug("streg after write: %08x\n", readl(&ds->regs->st));
  136. if (spit_enabled == 0) {
  137. /* enable SPIT bit - trigger the tx and rx progress */
  138. andes_spi_spit_en(ds);
  139. spit_enabled = 1;
  140. }
  141. }
  142. debug("\n");
  143. return 0;
  144. }
  145. /*
  146. * spi_xfer:
  147. * Since andes_spi doesn't support independent command transaction,
  148. * that is, write and than read must be operated in continuous
  149. * execution, there is no need to set dcr and trigger spit again in
  150. * RX process.
  151. */
  152. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  153. const void *dout, void *din, unsigned long flags)
  154. {
  155. unsigned int len;
  156. static int op_nextime;
  157. static u8 tmp_cmd[5];
  158. static int tmp_wlen;
  159. unsigned int i;
  160. if (bitlen == 0)
  161. /* Finish any previously submitted transfers */
  162. goto out;
  163. if (bitlen % 8) {
  164. /* Errors always terminate an ongoing transfer */
  165. flags |= SPI_XFER_END;
  166. goto out;
  167. }
  168. len = bitlen / 8;
  169. debug("%s: slave: %08x, bitlen: %d, dout: "
  170. "%08x, din: %08x, flags: %d, len: %d\n",
  171. __func__, slave, bitlen, dout, din, flags, len);
  172. /*
  173. * Important:
  174. * andes_spi's hardware doesn't support 2 data channel. The read
  175. * and write cmd/data share the same register (data register).
  176. *
  177. * If a command has write and read transaction, you cannot do write
  178. * this time and then do read on next time.
  179. *
  180. * A command writes first with a read response must indicating
  181. * the read length in write operation. Hence the write action must
  182. * be stored temporary and wait until the next read action has been
  183. * arrived. Then we flush the write and read action out together.
  184. */
  185. if (!dout) {
  186. if (op_nextime == 1) {
  187. /* flags should be SPI_XFER_END, value is 2 */
  188. op_nextime = 0;
  189. andes_spi_write(slave, tmp_wlen, len, tmp_cmd, flags);
  190. }
  191. return andes_spi_read(slave, len, din, flags);
  192. } else if (!din) {
  193. if (flags == SPI_XFER_BEGIN) {
  194. /* store the write command and do operation next time */
  195. op_nextime = 1;
  196. memset(tmp_cmd, 0, sizeof(tmp_cmd));
  197. memcpy(tmp_cmd, dout, len);
  198. debug("%s: tmp_cmd: ", __func__);
  199. for (i = 0; i < len; i++)
  200. debug("%x ", *(tmp_cmd + i));
  201. debug("\n");
  202. tmp_wlen = len;
  203. } else {
  204. /*
  205. * flags should be (SPI_XFER_BEGIN | SPI_XFER_END),
  206. * the value is 3.
  207. */
  208. if (op_nextime == 1) {
  209. /* flags should be SPI_XFER_END, value is 2 */
  210. op_nextime = 0;
  211. /* flags 3 implies write only */
  212. andes_spi_write(slave, tmp_wlen, 0, tmp_cmd, 3);
  213. }
  214. debug("flags: %x\n", flags);
  215. return andes_spi_write(slave, len, 0, dout, flags);
  216. }
  217. }
  218. out:
  219. return 0;
  220. }
  221. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  222. {
  223. return bus == 0 && cs == 0;
  224. }
  225. void spi_cs_activate(struct spi_slave *slave)
  226. {
  227. /* do nothing */
  228. }
  229. void spi_cs_deactivate(struct spi_slave *slave)
  230. {
  231. /* do nothing */
  232. }