spibus0at91.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/spibus0at91.c
  36. * \brief Primary 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: spibus0at91.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(SPI0_CS0_PIO_BIT)
  54. #if defined(SPI0_CS0_PIO_ID)
  55. #undef GPIO_ID
  56. #define GPIO_ID SPI0_CS0_PIO_ID
  57. #include <cfg/arch/porttran.h>
  58. static INLINE void SPI0_CS0_LO(void)
  59. {
  60. GPIO_SET_LO(SPI0_CS0_PIO_BIT);
  61. }
  62. static INLINE void SPI0_CS0_HI(void)
  63. {
  64. GPIO_SET_HI(SPI0_CS0_PIO_BIT);
  65. }
  66. static INLINE void SPI0_CS0_SO(void)
  67. {
  68. GPIO_ENABLE(SPI0_CS0_PIO_BIT);
  69. GPIO_OUTPUT(SPI0_CS0_PIO_BIT);
  70. }
  71. #else
  72. #define SPI0_CS0_LO()
  73. #define SPI0_CS0_HI()
  74. #define SPI0_CS0_SO()
  75. #endif
  76. #endif
  77. #if defined(SPI0_CS1_PIO_BIT)
  78. #if defined(SPI0_CS1_PIO_ID)
  79. #undef GPIO_ID
  80. #define GPIO_ID SPI0_CS1_PIO_ID
  81. #include <cfg/arch/porttran.h>
  82. static INLINE void SPI0_CS1_LO(void)
  83. {
  84. GPIO_SET_LO(SPI0_CS1_PIO_BIT);
  85. }
  86. static INLINE void SPI0_CS1_HI(void)
  87. {
  88. GPIO_SET_HI(SPI0_CS1_PIO_BIT);
  89. }
  90. static INLINE void SPI0_CS1_SO(void)
  91. {
  92. GPIO_ENABLE(SPI0_CS1_PIO_BIT);
  93. GPIO_OUTPUT(SPI0_CS1_PIO_BIT);
  94. }
  95. #else
  96. #define SPI0_CS1_LO()
  97. #define SPI0_CS1_HI()
  98. #define SPI0_CS1_SO()
  99. #endif
  100. #endif
  101. #if defined(SPI0_CS2_PIO_BIT)
  102. #if defined(SPI0_CS2_PIO_ID)
  103. #undef GPIO_ID
  104. #define GPIO_ID SPI0_CS2_PIO_ID
  105. #include <cfg/arch/porttran.h>
  106. static INLINE void SPI0_CS2_LO(void)
  107. {
  108. GPIO_SET_LO(SPI0_CS2_PIO_BIT);
  109. }
  110. static INLINE void SPI0_CS2_HI(void)
  111. {
  112. GPIO_SET_HI(SPI0_CS2_PIO_BIT);
  113. }
  114. static INLINE void SPI0_CS2_SO(void)
  115. {
  116. GPIO_ENABLE(SPI0_CS2_PIO_BIT);
  117. GPIO_OUTPUT(SPI0_CS2_PIO_BIT);
  118. }
  119. #else
  120. #define SPI0_CS2_LO()
  121. #define SPI0_CS2_HI()
  122. #define SPI0_CS2_SO()
  123. #endif
  124. #endif
  125. #if defined(SPI0_CS3_PIO_BIT)
  126. #if defined(SPI0_CS3_PIO_ID)
  127. #undef GPIO_ID
  128. #define GPIO_ID SPI0_CS3_PIO_ID
  129. #include <cfg/arch/porttran.h>
  130. static INLINE void SPI0_CS3_LO(void)
  131. {
  132. GPIO_SET_LO(SPI0_CS3_PIO_BIT);
  133. }
  134. static INLINE void SPI0_CS3_HI(void)
  135. {
  136. GPIO_SET_HI(SPI0_CS3_PIO_BIT);
  137. }
  138. static INLINE void SPI0_CS3_SO(void)
  139. {
  140. GPIO_ENABLE(SPI0_CS3_PIO_BIT);
  141. GPIO_OUTPUT(SPI0_CS3_PIO_BIT);
  142. }
  143. #else
  144. #define SPI0_CS3_LO()
  145. #define SPI0_CS3_HI()
  146. #define SPI0_CS3_SO()
  147. #endif
  148. #endif
  149. /*!
  150. * \brief Set the specified chip select to a given level.
  151. */
  152. int At91Spi0ChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  153. {
  154. int rc = 0;
  155. switch (cs) {
  156. #if defined(SPI0_CS0_PIO_BIT)
  157. case 0:
  158. if (hi) {
  159. SPI0_CS0_HI();
  160. } else {
  161. SPI0_CS0_LO();
  162. }
  163. SPI0_CS0_SO();
  164. break;
  165. #endif
  166. #if defined(SPI0_CS1_PIO_BIT)
  167. case 1:
  168. if (hi) {
  169. SPI0_CS1_HI();
  170. } else {
  171. SPI0_CS1_LO();
  172. }
  173. SPI0_CS1_SO();
  174. break;
  175. #endif
  176. #if defined(SPI0_CS2_PIO_BIT)
  177. case 2:
  178. if (hi) {
  179. SPI0_CS2_HI();
  180. } else {
  181. SPI0_CS2_LO();
  182. }
  183. SPI0_CS2_SO();
  184. break;
  185. #endif
  186. #if defined(SPI0_CS3_PIO_BIT)
  187. case 3:
  188. if (hi) {
  189. SPI0_CS3_HI();
  190. } else {
  191. SPI0_CS3_LO();
  192. }
  193. SPI0_CS3_SO();
  194. break;
  195. #endif
  196. default:
  197. errno = EIO;
  198. rc = -1;
  199. break;
  200. }
  201. return rc;
  202. }
  203. /*! \brief Select a device on the first SPI bus.
  204. *
  205. * Locks and activates the bus for the specified node.
  206. *
  207. * \param node Specifies the SPI bus node.
  208. * \param tmo Timeout in milliseconds. To disable timeout, set this
  209. * parameter to NUT_WAIT_INFINITE.
  210. *
  211. * \return 0 on success. In case of an error, -1 is returned and the bus
  212. * is not locked.
  213. */
  214. int At91SpiBus0Select(NUTSPINODE * node, uint32_t tmo)
  215. {
  216. int rc;
  217. /* Sanity check. */
  218. NUTASSERT(node != NULL);
  219. NUTASSERT(node->node_bus != NULL);
  220. NUTASSERT(node->node_stat != NULL);
  221. /* Allocate the bus. */
  222. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  223. if (rc) {
  224. errno = EIO;
  225. } else {
  226. AT91SPIREG *spireg = node->node_stat;
  227. outr(PMC_PCER, _BV(SPI0_ID));
  228. /* Enable SPI peripherals and clock. */
  229. outr(SPI0_PIO_BASE + SPI0_PSR_OFF, SPI0_PINS);
  230. outr(SPI0_PIO_BASE + PIO_PDR_OFF, SPI0_PINS);
  231. /* If the mode update bit is set, then update our shadow registers. */
  232. if (node->node_mode & SPI_MODE_UPDATE) {
  233. At91SpiSetup(node);
  234. }
  235. /* Enable SPI. */
  236. outr(SPI0_CR, SPI_SPIEN);
  237. /* Set SPI mode. */
  238. outr(SPI0_MR, spireg->at91spi_mr);
  239. outr(SPI0_CSR0 + node->node_cs * 4, spireg->at91spi_csr);
  240. /* Finally activate the node's chip select. */
  241. rc = At91Spi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  242. if (rc) {
  243. /* Release the bus in case of an error. */
  244. NutEventPost(&node->node_bus->bus_mutex);
  245. }
  246. }
  247. return rc;
  248. }
  249. /*! \brief Deselect a device on the first SPI bus.
  250. *
  251. * Deactivates the chip select and unlocks the bus.
  252. *
  253. * \param node Specifies the SPI bus node.
  254. *
  255. * \return Always 0.
  256. */
  257. int At91SpiBus0Deselect(NUTSPINODE * node)
  258. {
  259. /* Sanity check. */
  260. NUTASSERT(node != NULL);
  261. NUTASSERT(node->node_bus != NULL);
  262. #ifdef SPIBUS0_DOUBLE_BUFFER
  263. At91SpiBusWait(node, NUT_WAIT_INFINITE);
  264. #endif
  265. /* Deactivate the node's chip select. */
  266. At91Spi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  267. #ifdef SPIBUS0_PIN_SHARING
  268. /* Disable SPI peripherals if pins are shared. */
  269. outr(SPI0_PIO_BASE + PIO_ODR_OFF, SPI0_PINS);
  270. outr(SPI0_PIO_BASE + PIO_PER_OFF, SPI0_PINS);
  271. #endif
  272. /* Release the bus. */
  273. NutEventPost(&node->node_bus->bus_mutex);
  274. return 0;
  275. }
  276. #if !defined(SPIBUS0_POLLING_MODE) || !defined(SPIBUS0_DOUBLE_BUFFER)
  277. static uint8_t * volatile spi0_txp;
  278. static uint8_t * volatile spi0_rxp;
  279. static volatile size_t spi0_xc;
  280. void At91SpiBus0Interrupt(void *arg)
  281. {
  282. uint8_t b;
  283. /* Get the received byte. */
  284. b = inb(SPI0_RDR);
  285. if (spi0_xc) {
  286. if (spi0_rxp) {
  287. *spi0_rxp++ = b;
  288. }
  289. spi0_xc--;
  290. }
  291. if (spi0_xc) {
  292. if (spi0_txp) {
  293. b = *spi0_txp++;
  294. }
  295. outb(SPI0_TDR, b);
  296. } else {
  297. NutEventPostFromIrq((void **)arg);
  298. }
  299. }
  300. /*!
  301. * \brief Transfer data on the SPI bus using single buffered interrupt mode.
  302. *
  303. * A device must have been selected by calling At91SpiSelect().
  304. *
  305. * \param node Specifies the SPI bus node.
  306. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  307. * byte values are transmitted.
  308. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  309. * data is discarded.
  310. * \param xlen Number of bytes to transfer.
  311. *
  312. * \return Always 0.
  313. */
  314. int At91SpiBus0Transfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  315. {
  316. uint8_t b = 0xff;
  317. uintptr_t base;
  318. /* Sanity check. */
  319. NUTASSERT(node != NULL);
  320. NUTASSERT(node->node_bus != NULL);
  321. NUTASSERT(node->node_bus->bus_base != 0);
  322. base = node->node_bus->bus_base;
  323. if (xlen) {
  324. spi0_txp = (uint8_t *) txbuf;
  325. spi0_rxp = (uint8_t *) rxbuf;
  326. spi0_xc = (size_t) xlen;
  327. if (spi0_txp) {
  328. b = *spi0_txp++;
  329. }
  330. /* Enable and kick interrupts. */
  331. outr(base + SPI_IER_OFF, SPI_RDRF);
  332. outr(base + SPI_TDR_OFF, b);
  333. /* Wait until transfer has finished. */
  334. NutEventWait(&node->node_bus->bus_ready, NUT_WAIT_INFINITE);
  335. outr(base + SPI_IDR_OFF, (unsigned int) - 1);
  336. }
  337. return 0;
  338. }
  339. #endif
  340. /*!
  341. * \brief AT91 SPI bus driver implementation structure.
  342. */
  343. NUTSPIBUS spiBus0At91 = {
  344. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  345. NULL, /*!< Bus ready signal (bus_ready). */
  346. SPI0_BASE, /*!< Bus base address (bus_base). */
  347. &sig_SPI0, /*!< Bus interrupt handler (bus_sig). */
  348. At91SpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  349. At91SpiBus0Select, /*!< Select the specified device (bus_alloc). */
  350. At91SpiBus0Deselect, /*!< Deselect the specified device (bus_release). */
  351. #if defined(SPIBUS0_POLLING_MODE)
  352. At91SpiBusPollTransfer, /*!< Transfer data to and from a specified node (bus_transfer). */
  353. #elif defined(SPIBUS0_DOUBLE_BUFFER)
  354. At91SpiBusDblBufTransfer,
  355. #else
  356. At91SpiBus0Transfer,
  357. #endif
  358. #ifdef SPIBUS0_DOUBLE_BUFFER
  359. At91SpiBusWait,
  360. #else
  361. NutSpiBusWait, /*!< Wait for bus transfer ready (bus_wait). */
  362. #endif
  363. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  364. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  365. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  366. };