ih_at91twi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2001-2005 by EmbeddedIT,
  3. * Ole Reinhardt <ole.reinhardt@embedded-it.de> All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
  22. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.3 2008/08/11 06:59:12 haraldkipp
  36. * BSD types replaced by stdint types (feature request #1282721).
  37. *
  38. * Revision 1.2 2008/07/26 09:43:01 haraldkipp
  39. * Added support for retrieving and setting the interrupt mode.
  40. *
  41. * Revision 1.1 2007/09/06 19:36:00 olereinhardt
  42. * First checkin, new twi driver for at91 (currently SAM7X256 is supported
  43. * only)
  44. *
  45. *
  46. */
  47. #include <arch/arm.h>
  48. #include <dev/irqreg.h>
  49. #if defined(TWI_ID)
  50. #ifndef NUT_IRQPRI_TWI
  51. #define NUT_IRQPRI_TWI 4
  52. #endif
  53. static int TwoWireIrqCtl(int cmd, void *param);
  54. IRQ_HANDLER sig_TWI = {
  55. #ifdef NUT_PERFMON
  56. 0, /* Interrupt counter, ir_count. */
  57. #endif
  58. NULL, /* Passed argument, ir_arg. */
  59. NULL, /* Handler subroutine, ir_handler. */
  60. TwoWireIrqCtl /* Interrupt control, ir_ctl. */
  61. };
  62. /*!
  63. * \brief Two Wire Interface interrupt entry.
  64. */
  65. static void TwoWireIrqEntry(void) NUT_NAKED_FUNC;
  66. void TwoWireIrqEntry(void)
  67. {
  68. IRQ_ENTRY();
  69. #ifdef NUT_PERFMON
  70. sig_TWI.ir_count++;
  71. #endif
  72. if (sig_TWI.ir_handler) {
  73. (sig_TWI.ir_handler) (sig_TWI.ir_arg);
  74. }
  75. IRQ_EXIT();
  76. }
  77. /*!
  78. * \brief Two Wire Interface interrupt control.
  79. *
  80. * \param cmd Control command.
  81. * - NUT_IRQCTL_INIT Initialize and disable interrupt.
  82. * - NUT_IRQCTL_STATUS Query interrupt status.
  83. * - NUT_IRQCTL_ENABLE Enable interrupt.
  84. * - NUT_IRQCTL_DISABLE Disable interrupt.
  85. * - NUT_IRQCTL_GETMODE Query interrupt mode.
  86. * - NUT_IRQCTL_SETMODE Set interrupt mode (NUT_IRQMODE_LEVEL or NUT_IRQMODE_EDGE).
  87. * - NUT_IRQCTL_GETPRIO Query interrupt priority.
  88. * - NUT_IRQCTL_SETPRIO Set interrupt priority.
  89. * - NUT_IRQCTL_GETCOUNT Query and clear interrupt counter.
  90. * \param param Pointer to optional parameter.
  91. *
  92. * \return 0 on success, -1 otherwise.
  93. */
  94. static int TwoWireIrqCtl(int cmd, void *param)
  95. {
  96. int rc = 0;
  97. unsigned int *ival = (unsigned int *)param;
  98. int_fast8_t enabled = inr(AIC_IMR) & _BV(TWI_ID);
  99. /* Disable interrupt. */
  100. if (enabled) {
  101. outr(AIC_IDCR, _BV(TWI_ID));
  102. }
  103. switch(cmd) {
  104. case NUT_IRQCTL_INIT:
  105. /* Set the vector. */
  106. outr(AIC_SVR(TWI_ID), (unsigned int)TwoWireIrqEntry);
  107. /* Initialize to edge triggered with defined priority. */
  108. outr(AIC_SMR(TWI_ID), AIC_SRCTYPE_INT_EDGE_TRIGGERED | NUT_IRQPRI_TWI);
  109. /* Clear interrupt */
  110. outr(AIC_ICCR, _BV(TWI_ID));
  111. break;
  112. case NUT_IRQCTL_STATUS:
  113. if (enabled) {
  114. *ival |= 1;
  115. }
  116. else {
  117. *ival &= ~1;
  118. }
  119. break;
  120. case NUT_IRQCTL_ENABLE:
  121. enabled = 1;
  122. break;
  123. case NUT_IRQCTL_DISABLE:
  124. enabled = 0;
  125. break;
  126. case NUT_IRQCTL_GETMODE:
  127. {
  128. unsigned int val = inr(AIC_SMR(TWI_ID)) & AIC_SRCTYPE;
  129. if (val == AIC_SRCTYPE_INT_LEVEL_SENSITIVE || val == AIC_SRCTYPE_EXT_HIGH_LEVEL) {
  130. *ival = NUT_IRQMODE_LEVEL;
  131. } else {
  132. *ival = NUT_IRQMODE_EDGE;
  133. }
  134. }
  135. break;
  136. case NUT_IRQCTL_SETMODE:
  137. if (*ival == NUT_IRQMODE_LEVEL) {
  138. outr(AIC_SMR(TWI_ID), (inr(AIC_SMR(TWI_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_INT_LEVEL_SENSITIVE);
  139. } else if (*ival == NUT_IRQMODE_EDGE) {
  140. outr(AIC_SMR(TWI_ID), (inr(AIC_SMR(TWI_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_INT_EDGE_TRIGGERED);
  141. } else {
  142. rc = -1;
  143. }
  144. break;
  145. case NUT_IRQCTL_GETPRIO:
  146. *ival = inr(AIC_SMR(TWI_ID)) & AIC_PRIOR;
  147. break;
  148. case NUT_IRQCTL_SETPRIO:
  149. outr(AIC_SMR(TWI_ID), (inr(AIC_SMR(TWI_ID)) & ~AIC_PRIOR) | *ival);
  150. break;
  151. #ifdef NUT_PERFMON
  152. case NUT_IRQCTL_GETCOUNT:
  153. *ival = (unsigned int)sig_TWI.ir_count;
  154. sig_TWI.ir_count = 0;
  155. break;
  156. #endif
  157. default:
  158. rc = -1;
  159. break;
  160. }
  161. /* Enable interrupt. */
  162. if (enabled) {
  163. outr(AIC_IECR, _BV(TWI_ID));
  164. }
  165. return rc;
  166. }
  167. #endif /* TWI_ID */