andes_spi.c 7.3 KB

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