spibus1at91.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/spibus1at91.c
  36. * \brief Secondary AT91 SPI bus controller.
  37. *
  38. * May be configured as an interrupt driven or polling driver. The
  39. * interrupt driven version may use single or double buffering.
  40. *
  41. * \verbatim
  42. * $Id: spibus1at91.c 4473 2012-08-20 15:12:45Z haraldkipp $
  43. * \endverbatim
  44. */
  45. #include <cfg/spi.h>
  46. #include <cfg/arch/gpio.h>
  47. #include <dev/spibus_at91.h>
  48. #include <dev/irqreg.h>
  49. #include <sys/event.h>
  50. #include <sys/nutdebug.h>
  51. #include <stdlib.h>
  52. #include <errno.h>
  53. #if defined(SPI1_PIO_BASE)
  54. #if defined(SPI1_CS0_PIO_BIT)
  55. #if defined(SPI1_CS0_PIO_ID)
  56. #undef GPIO_ID
  57. #define GPIO_ID SPI1_CS0_PIO_ID
  58. #include <cfg/arch/porttran.h>
  59. static INLINE void SPI1_CS0_LO(void)
  60. {
  61. GPIO_SET_LO(SPI1_CS0_PIO_BIT);
  62. }
  63. static INLINE void SPI1_CS0_HI(void)
  64. {
  65. GPIO_SET_HI(SPI1_CS0_PIO_BIT);
  66. }
  67. static INLINE void SPI1_CS0_SO(void)
  68. {
  69. GPIO_ENABLE(SPI1_CS0_PIO_BIT);
  70. GPIO_OUTPUT(SPI1_CS0_PIO_BIT);
  71. }
  72. #else
  73. #define SPI1_CS0_LO()
  74. #define SPI1_CS0_HI()
  75. #define SPI1_CS0_SO()
  76. #endif
  77. #endif
  78. #if defined(SPI1_CS1_PIO_BIT)
  79. #if defined(SPI1_CS1_PIO_ID)
  80. #undef GPIO_ID
  81. #define GPIO_ID SPI1_CS1_PIO_ID
  82. #include <cfg/arch/porttran.h>
  83. static INLINE void SPI1_CS1_LO(void)
  84. {
  85. GPIO_SET_LO(SPI1_CS1_PIO_BIT);
  86. }
  87. static INLINE void SPI1_CS1_HI(void)
  88. {
  89. GPIO_SET_HI(SPI1_CS1_PIO_BIT);
  90. }
  91. static INLINE void SPI1_CS1_SO(void)
  92. {
  93. GPIO_ENABLE(SPI1_CS1_PIO_BIT);
  94. GPIO_OUTPUT(SPI1_CS1_PIO_BIT);
  95. }
  96. #else
  97. #define SPI1_CS1_LO()
  98. #define SPI1_CS1_HI()
  99. #define SPI1_CS1_SO()
  100. #endif
  101. #endif
  102. #if defined(SPI1_CS2_PIO_BIT)
  103. #if defined(SPI1_CS2_PIO_ID)
  104. #undef GPIO_ID
  105. #define GPIO_ID SPI1_CS2_PIO_ID
  106. #include <cfg/arch/porttran.h>
  107. static INLINE void SPI1_CS2_LO(void)
  108. {
  109. GPIO_SET_LO(SPI1_CS2_PIO_BIT);
  110. }
  111. static INLINE void SPI1_CS2_HI(void)
  112. {
  113. GPIO_SET_HI(SPI1_CS2_PIO_BIT);
  114. }
  115. static INLINE void SPI1_CS2_SO(void)
  116. {
  117. GPIO_ENABLE(SPI1_CS2_PIO_BIT);
  118. GPIO_OUTPUT(SPI1_CS2_PIO_BIT);
  119. }
  120. #else
  121. #define SPI1_CS2_LO()
  122. #define SPI1_CS2_HI()
  123. #define SPI1_CS2_SO()
  124. #endif
  125. #endif
  126. #if defined(SPI1_CS3_PIO_BIT)
  127. #if defined(SPI1_CS3_PIO_ID)
  128. #undef GPIO_ID
  129. #define GPIO_ID SPI1_CS3_PIO_ID
  130. #include <cfg/arch/porttran.h>
  131. static INLINE void SPI1_CS3_LO(void)
  132. {
  133. GPIO_SET_LO(SPI1_CS3_PIO_BIT);
  134. }
  135. static INLINE void SPI1_CS3_HI(void)
  136. {
  137. GPIO_SET_HI(SPI1_CS3_PIO_BIT);
  138. }
  139. static INLINE void SPI1_CS3_SO(void)
  140. {
  141. GPIO_ENABLE(SPI1_CS3_PIO_BIT);
  142. GPIO_OUTPUT(SPI1_CS3_PIO_BIT);
  143. }
  144. #else
  145. #define SPI1_CS3_LO()
  146. #define SPI1_CS3_HI()
  147. #define SPI1_CS3_SO()
  148. #endif
  149. #endif
  150. /*!
  151. * \brief Set the specified chip select to a given level.
  152. */
  153. int At91Spi1ChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  154. {
  155. int rc = 0;
  156. switch (cs) {
  157. #if defined(SPI1_CS0_PIO_BIT)
  158. case 0:
  159. if (hi) {
  160. SPI1_CS0_HI();
  161. } else {
  162. SPI1_CS0_LO();
  163. }
  164. SPI1_CS0_SO();
  165. break;
  166. #endif
  167. #if defined(SPI1_CS1_PIO_BIT)
  168. case 1:
  169. if (hi) {
  170. SPI1_CS1_HI();
  171. } else {
  172. SPI1_CS1_LO();
  173. }
  174. SPI1_CS1_SO();
  175. break;
  176. #endif
  177. #if defined(SPI1_CS2_PIO_BIT)
  178. case 2:
  179. if (hi) {
  180. SPI1_CS2_HI();
  181. } else {
  182. SPI1_CS2_LO();
  183. }
  184. SPI1_CS2_SO();
  185. break;
  186. #endif
  187. #if defined(SPI1_CS3_PIO_BIT)
  188. case 3:
  189. if (hi) {
  190. SPI1_CS3_HI();
  191. } else {
  192. SPI1_CS3_LO();
  193. }
  194. SPI1_CS3_SO();
  195. break;
  196. #endif
  197. default:
  198. errno = EIO;
  199. rc = -1;
  200. break;
  201. }
  202. return rc;
  203. }
  204. /*! \brief Select a device on the first SPI bus.
  205. *
  206. * Locks and activates the bus for the specified node.
  207. *
  208. * \param node Specifies the SPI bus node.
  209. * \param tmo Timeout in milliseconds. To disable timeout, set this
  210. * parameter to NUT_WAIT_INFINITE.
  211. *
  212. * \return 0 on success. In case of an error, -1 is returned and the bus
  213. * is not locked.
  214. */
  215. int At91SpiBus1Select(NUTSPINODE * node, uint32_t tmo)
  216. {
  217. int rc;
  218. /* Sanity check. */
  219. NUTASSERT(node != NULL);
  220. NUTASSERT(node->node_bus != NULL);
  221. NUTASSERT(node->node_stat != NULL);
  222. /* Allocate the bus. */
  223. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  224. if (rc) {
  225. errno = EIO;
  226. } else {
  227. AT91SPIREG *spireg = node->node_stat;
  228. outr(PMC_PCER, _BV(SPI1_ID));
  229. /* Enable SPI peripherals and clock. */
  230. outr(SPI1_PIO_BASE + SPI1_PSR_OFF, SPI1_PINS);
  231. outr(SPI1_PIO_BASE + PIO_PDR_OFF, SPI1_PINS);
  232. /* If the mode update bit is set, then update our shadow registers. */
  233. if (node->node_mode & SPI_MODE_UPDATE) {
  234. At91SpiSetup(node);
  235. }
  236. /* Enable SPI. */
  237. outr(SPI1_CR, SPI_SPIEN);
  238. /* Set SPI mode. */
  239. outr(SPI1_MR, spireg->at91spi_mr);
  240. outr(SPI1_CSR0 + node->node_cs * 4, spireg->at91spi_csr);
  241. /* Finally activate the node's chip select. */
  242. rc = At91Spi1ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  243. if (rc) {
  244. /* Release the bus in case of an error. */
  245. NutEventPost(&node->node_bus->bus_mutex);
  246. }
  247. }
  248. return rc;
  249. }
  250. /*! \brief Deselect a device on the first SPI bus.
  251. *
  252. * Deactivates the chip select and unlocks the bus.
  253. *
  254. * \param node Specifies the SPI bus node.
  255. *
  256. * \return Always 0.
  257. */
  258. int At91SpiBus1Deselect(NUTSPINODE * node)
  259. {
  260. /* Sanity check. */
  261. NUTASSERT(node != NULL);
  262. NUTASSERT(node->node_bus != NULL);
  263. #ifdef SPIBUS1_DOUBLE_BUFFER
  264. At91SpiBusWait(node, NUT_WAIT_INFINITE);
  265. #endif
  266. /* Deactivate the node's chip select. */
  267. At91Spi1ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  268. /* Release the bus. */
  269. NutEventPost(&node->node_bus->bus_mutex);
  270. return 0;
  271. }
  272. #if !defined(SPIBUS1_POLLING_MODE) || !defined(SPIBUS1_DOUBLE_BUFFER)
  273. static uint8_t * volatile spi1_txp;
  274. static uint8_t * volatile spi1_rxp;
  275. static volatile size_t spi1_xc;
  276. void At91SpiBus1Interrupt(void *arg)
  277. {
  278. uint8_t b;
  279. /* Get the received byte. */
  280. b = inb(SPI1_RDR);
  281. if (spi1_xc) {
  282. if (spi1_rxp) {
  283. *spi1_rxp++ = b;
  284. }
  285. spi1_xc--;
  286. }
  287. if (spi1_xc) {
  288. if (spi1_txp) {
  289. b = *spi1_txp++;
  290. }
  291. outb(SPI1_TDR, b);
  292. } else {
  293. NutEventPostFromIrq((void **)arg);
  294. }
  295. }
  296. /*!
  297. * \brief Transfer data on the SPI bus using single buffered interrupt mode.
  298. *
  299. * A device must have been selected by calling At91SpiSelect().
  300. *
  301. * \param node Specifies the SPI bus node.
  302. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  303. * byte values are transmitted.
  304. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  305. * data is discarded.
  306. * \param xlen Number of bytes to transfer.
  307. *
  308. * \return Always 0.
  309. */
  310. int At91SpiBus1Transfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  311. {
  312. uint8_t b = 0xff;
  313. uintptr_t base;
  314. /* Sanity check. */
  315. NUTASSERT(node != NULL);
  316. NUTASSERT(node->node_bus != NULL);
  317. NUTASSERT(node->node_bus->bus_base != 0);
  318. base = node->node_bus->bus_base;
  319. if (xlen) {
  320. spi1_txp = (uint8_t *) txbuf;
  321. spi1_rxp = (uint8_t *) rxbuf;
  322. spi1_xc = (size_t) xlen;
  323. if (spi1_txp) {
  324. b = *spi1_txp++;
  325. }
  326. /* Enable and kick interrupts. */
  327. outr(base + SPI_IER_OFF, SPI_RDRF);
  328. outr(base + SPI_TDR_OFF, b);
  329. /* Wait until transfer has finished. */
  330. NutEventWait(&node->node_bus->bus_ready, NUT_WAIT_INFINITE);
  331. outr(base + SPI_IDR_OFF, (unsigned int) - 1);
  332. }
  333. return 0;
  334. }
  335. #endif
  336. /*!
  337. * \brief AT91 SPI bus driver implementation structure.
  338. */
  339. NUTSPIBUS spiBus1At91 = {
  340. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  341. NULL, /*!< Bus ready signal (bus_ready). */
  342. SPI1_BASE, /*!< Bus base address (bus_base). */
  343. &sig_SPI1, /*!< Bus interrupt handler (bus_sig). */
  344. At91SpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  345. At91SpiBus1Select, /*!< Select the specified device (bus_alloc). */
  346. At91SpiBus1Deselect, /*!< Deselect the specified device (bus_release). */
  347. #if defined(SPIBUS1_POLLING_MODE)
  348. At91SpiBusPollTransfer, /*!< Transfer data to and from a specified node (bus_transfer). */
  349. #elif defined(SPIBUS1_DOUBLE_BUFFER)
  350. At91SpiBusDblBufTransfer,
  351. #else
  352. At91SpiBus1Transfer,
  353. #endif
  354. #ifdef SPIBUS1_DOUBLE_BUFFER
  355. At91SpiBusWait,
  356. #else
  357. NutSpiBusWait, /*!< Wait for bus transfer ready (bus_wait). */
  358. #endif
  359. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  360. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  361. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  362. };
  363. #endif /* SPI1_PIO_BASE */