andes_spi.c 7.3 KB

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