spibus_at91.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright (C) 2008-2009 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/arm/dev/spibus_at91.c
  36. * \brief General AT91 SPI bus controller routines.
  37. *
  38. * \verbatim
  39. * $Id: spibus_at91.c 4473 2012-08-20 15:12:45Z haraldkipp $
  40. * \endverbatim
  41. */
  42. #include <cfg/spi.h>
  43. #include <sys/timer.h>
  44. #include <sys/nutdebug.h>
  45. #include <stdlib.h>
  46. #include <memdebug.h>
  47. #include <dev/spibus_at91.h>
  48. #define SPI_DOUBLE_BUFFER_MIN_TRANSFER_SIZE 4
  49. #if defined(SPIBUS0_DOUBLE_BUFFER) || defined(SPIBUS1_DOUBLE_BUFFER)
  50. /*!
  51. * \brief AT91 SPI interrupt handler.
  52. */
  53. static void At91SpiInterrupt(void *arg)
  54. {
  55. NutEventPostFromIrq((void **)arg);
  56. }
  57. #endif /* SPIBUS0_DOUBLE_BUFFER */
  58. /*!
  59. * \brief Update SPI shadow registers.
  60. *
  61. * \param node Specifies the SPI bus node.
  62. *
  63. * \return Always 0.
  64. */
  65. int At91SpiSetup(NUTSPINODE * node)
  66. {
  67. uint32_t clk;
  68. uint32_t clkdiv;
  69. AT91SPIREG *spireg;
  70. NUTASSERT(node != NULL);
  71. NUTASSERT(node->node_stat != NULL);
  72. NUTASSERT(node->node_bus != NULL);
  73. NUTASSERT(node->node_bus->bus_base != 0);
  74. spireg = node->node_stat;
  75. spireg->at91spi_mr &= ~(SPI_MODFDIS | SPI_LLB);
  76. if ((node->node_mode & SPI_MODE_FAULT) == 0) {
  77. spireg->at91spi_mr |= SPI_MODFDIS;
  78. }
  79. if (node->node_mode & SPI_MODE_LOOPBACK) {
  80. spireg->at91spi_mr |= SPI_LLB;
  81. }
  82. spireg->at91spi_csr &= ~(SPI_BITS | SPI_CPOL | SPI_NCPHA | SPI_CSAAT | SPI_SCBR);
  83. if (node->node_bits) {
  84. spireg->at91spi_csr |= ((uint32_t)(node->node_bits - 8) << SPI_BITS_LSB) & SPI_BITS;
  85. }
  86. if (node->node_mode & SPI_MODE_CPOL) {
  87. spireg->at91spi_csr |= SPI_CPOL;
  88. }
  89. if ((node->node_mode & SPI_MODE_CPHA) == 0) {
  90. spireg->at91spi_csr |= SPI_NCPHA;
  91. }
  92. if (node->node_mode & SPI_MODE_CSKEEP) {
  93. spireg->at91spi_csr |= SPI_CSAAT;
  94. }
  95. /* Query peripheral clock. */
  96. clk = NutClockGet(NUT_HWCLK_PERIPHERAL);
  97. /* Calculate the SPI clock divider. Avoid rounding errors. */
  98. clkdiv = (clk + node->node_rate - 1) / node->node_rate;
  99. /* The divider value minimum is 1. */
  100. if (clkdiv < 1) {
  101. clkdiv++;
  102. }
  103. /* The divider value maximum is 255. */
  104. else if (clkdiv > 255) {
  105. clkdiv = 255;
  106. }
  107. spireg->at91spi_csr |= clkdiv << SPI_SCBR_LSB;
  108. /* Update interface parameters. */
  109. node->node_rate = clk / clkdiv;
  110. node->node_mode &= ~SPI_MODE_UPDATE;
  111. return 0;
  112. }
  113. /*!
  114. * \brief Initialize an SPI bus node.
  115. *
  116. * This routine is called for each SPI node, which is registered via
  117. * NutRegisterSpiDevice().
  118. *
  119. * \param node Specifies the SPI bus node.
  120. *
  121. * \return 0 on success or -1 if there is no valid chip select.
  122. */
  123. int At91SpiBusNodeInit(NUTSPINODE * node)
  124. {
  125. int rc;
  126. #if !defined(SPIBUS0_POLLING_MODE) || (!defined(SPIBUS1_POLLING_MODE) && defined(SPI1_BASE)) || defined(SPI1_BASE)
  127. NUTSPIBUS *bus;
  128. #endif
  129. /* Sanity check. */
  130. NUTASSERT(node != NULL);
  131. NUTASSERT(node->node_bus != NULL);
  132. #if !defined(SPIBUS0_POLLING_MODE) || (!defined(SPIBUS1_POLLING_MODE) && defined(SPI1_BASE)) || defined(SPI1_BASE)
  133. bus = node->node_bus;
  134. #endif
  135. /* Try to deactivate the node's chip select. */
  136. #if defined(SPI1_BASE)
  137. if (bus->bus_base == SPI1_BASE) {
  138. rc = At91Spi1ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  139. } else
  140. #endif
  141. rc = At91Spi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  142. /* It should not hurt us being called more than once. Thus, we
  143. ** check wether any initialization had been taken place already. */
  144. if (rc == 0 && node->node_stat == NULL) {
  145. /* Allocate and set our shadow registers. */
  146. AT91SPIREG *spireg = malloc(sizeof(AT91SPIREG));
  147. if (spireg) {
  148. /* Set interface defaults. */
  149. spireg->at91spi_mr = SPI_MODFDIS | SPI_MSTR;
  150. switch (node->node_cs) {
  151. case 0:
  152. spireg->at91spi_mr |= SPI_PCS_0;
  153. break;
  154. case 1:
  155. spireg->at91spi_mr |= SPI_PCS_1;
  156. break;
  157. case 2:
  158. spireg->at91spi_mr |= SPI_PCS_2;
  159. break;
  160. case 3:
  161. spireg->at91spi_mr |= SPI_PCS_3;
  162. break;
  163. }
  164. spireg->at91spi_csr = 0;
  165. /* Update with node's defaults. */
  166. node->node_stat = (void *)spireg;
  167. At91SpiSetup(node);
  168. /*
  169. * Register and enable SPI interrupt handler.
  170. */
  171. #if !defined(SPIBUS1_POLLING_MODE) && defined(SPI1_BASE)
  172. if (bus->bus_base == SPI1_BASE) {
  173. #if defined(SPIBUS1_DOUBLE_BUFFER)
  174. NutRegisterIrqHandler(bus->bus_sig, At91SpiInterrupt, &bus->bus_ready);
  175. #else
  176. NutRegisterIrqHandler(bus->bus_sig, At91SpiBus1Interrupt, &bus->bus_ready);
  177. #endif
  178. outr(bus->bus_base + SPI_IDR_OFF, (unsigned int) - 1);
  179. NutIrqEnable(bus->bus_sig);
  180. } else
  181. #endif /* !SPIBUS1_POLLING_MODE */
  182. {
  183. #if !defined(SPIBUS0_POLLING_MODE)
  184. #if defined(SPIBUS0_DOUBLE_BUFFER)
  185. NutRegisterIrqHandler(bus->bus_sig, At91SpiInterrupt, &bus->bus_ready);
  186. #else
  187. NutRegisterIrqHandler(bus->bus_sig, At91SpiBus0Interrupt, &bus->bus_ready);
  188. #endif
  189. outr(bus->bus_base + SPI_IDR_OFF, (unsigned int) - 1);
  190. NutIrqEnable(bus->bus_sig);
  191. #endif /* !SPIBUS0_POLLING_MODE */
  192. }
  193. } else {
  194. /* Out of memory? */
  195. rc = -1;
  196. }
  197. }
  198. return rc;
  199. }
  200. #if defined(SPIBUS0_POLLING_MODE) || defined(SPIBUS1_POLLING_MODE) || defined(SPIBUS0_DOUBLE_BUFFER_HEURISTIC) || defined(SPIBUS1_DOUBLE_BUFFER_HEURISTIC)
  201. /*!
  202. * \brief Transfer data on the SPI bus in polling mode.
  203. *
  204. * A device must have been selected by calling At91SpiSelect().
  205. *
  206. * \param node Specifies the SPI bus node.
  207. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  208. * byte values are transmitted.
  209. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  210. * data is discarded.
  211. * \param xlen Number of bytes to transfer.
  212. *
  213. * \return Always 0.
  214. */
  215. int At91SpiBusPollTransfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  216. {
  217. uint8_t b = 0xff;
  218. uint8_t *txp = (uint8_t *) txbuf;
  219. uint8_t *rxp = (uint8_t *) rxbuf;
  220. uintptr_t base;
  221. /* Sanity check. */
  222. NUTASSERT(node != NULL);
  223. NUTASSERT(node->node_bus != NULL);
  224. NUTASSERT(node->node_bus->bus_base != 0);
  225. base = node->node_bus->bus_base;
  226. while (xlen--) {
  227. if (txp) {
  228. b = *txp++;
  229. }
  230. /* Transmission starts by writing the transmit data. */
  231. outr(base + SPI_TDR_OFF, b);
  232. /* Wait for receiver data register full. */
  233. while((inr(base + SPI_SR_OFF) & SPI_RDRF) == 0);
  234. /* Read incoming data. */
  235. b = (uint8_t)inr(base + SPI_RDR_OFF);
  236. if (rxp) {
  237. *rxp++ = b;
  238. }
  239. }
  240. return 0;
  241. }
  242. #endif
  243. #if defined(SPIBUS0_DOUBLE_BUFFER) || defined(SPIBUS1_DOUBLE_BUFFER)
  244. /*!
  245. * \brief Transfer data on the SPI bus using double buffered PDC.
  246. *
  247. * A device must have been selected by calling At91SpiSelect().
  248. *
  249. * \todo Not yet done. Given up after SAM7SE SDRAM problems.
  250. * Currently working fine on SAM7X platform
  251. * \todo Is this working asynchronously? Old comments mentioned that
  252. * the transfer might be still active when function returns.
  253. *
  254. * \param node Specifies the SPI bus node.
  255. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  256. * byte values are transmitted.
  257. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  258. * data is discarded.
  259. * \param xlen Number of bytes to transfer.
  260. *
  261. * \return Always 0.
  262. */
  263. int At91SpiBusDblBufTransfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  264. {
  265. uintptr_t base;
  266. uint32_t cr;
  267. uint32_t ir = 0;
  268. #if defined(SPIBUS0_DOUBLE_BUFFER_HEURISTIC) || defined(SPIBUS1_DOUBLE_BUFFER_HEURISTIC)
  269. if (xlen < SPI_DOUBLE_BUFFER_MIN_TRANSFER_SIZE) {
  270. return At91SpiBusPollTransfer(node, txbuf, rxbuf, xlen);
  271. }
  272. #endif
  273. /* Sanity check. */
  274. NUTASSERT(node != NULL);
  275. NUTASSERT(node->node_bus != NULL);
  276. NUTASSERT(node->node_bus->bus_base != 0);
  277. base = node->node_bus->bus_base;
  278. outr(base + PERIPH_PTCR_OFF, PDC_TXTDIS | PDC_RXTDIS);
  279. if (xlen) {
  280. /* Set first transmit pointer and counter. */
  281. if (txbuf) {
  282. outr(base + PERIPH_TPR_OFF, (uint32_t)txbuf);
  283. } else {
  284. outr(base + PERIPH_TPR_OFF, (uint32_t)rxbuf);
  285. }
  286. cr = PDC_TXTEN;
  287. outr(base + PERIPH_TCR_OFF, xlen);
  288. /* Set first receive pointer and counter. */
  289. if (rxbuf) {
  290. outr(base + PERIPH_RPR_OFF, (uint32_t)rxbuf);
  291. outr(base + PERIPH_RCR_OFF, xlen);
  292. cr |= PDC_RXTEN;
  293. ir = SPI_RXBUFF;
  294. } else {
  295. cr |= PDC_RXTDIS;
  296. ir = SPI_TXBUFE;
  297. outr(base + PERIPH_RPR_OFF, 0);
  298. outr(base + PERIPH_RCR_OFF, 0);
  299. }
  300. outr(base + PERIPH_TNPR_OFF, 0);
  301. outr(base + PERIPH_TNCR_OFF, 0);
  302. outr(base + PERIPH_RNPR_OFF, 0);
  303. outr(base + PERIPH_RNCR_OFF, 0);
  304. outr(base + SPI_IDR_OFF, (unsigned int) - 1);
  305. outr(base + SPI_IER_OFF, ir);
  306. outr(base + PERIPH_PTCR_OFF, cr);
  307. NutEventWait(&node->node_bus->bus_ready, NUT_WAIT_INFINITE);
  308. outr(base + PERIPH_PTCR_OFF, PDC_TXTDIS | PDC_RXTDIS);
  309. }
  310. return 0;
  311. }
  312. #endif
  313. #if defined(SPIBUS0_DOUBLE_BUFFER) || defined(SPIBUS1_DOUBLE_BUFFER)
  314. /*!
  315. * \brief Wait until all SPI bus transfers are done.
  316. *
  317. * \param node Specifies the SPI bus node.
  318. * \param tmo Timeout in milliseconds. To disable timeout, set this
  319. * parameter to NUT_WAIT_INFINITE.
  320. *
  321. * \return Always 0.
  322. */
  323. int At91SpiBusWait(NUTSPINODE * node, uint32_t tmo)
  324. {
  325. while ((inr(node->node_bus->bus_base + SPI_SR_OFF) & SPI_RXBUFF) == 0) {
  326. if (NutEventWait(&node->node_bus->bus_ready, tmo)) {
  327. return -1;
  328. }
  329. }
  330. return 0;
  331. }
  332. #endif /* SPIBUS_POLLING_MODE */