lser.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <lib.h>
  31. #include <spinlock.h>
  32. #include <platform/bus.h>
  33. #include <lamebus/lser.h>
  34. #include "autoconf.h"
  35. /* Registers (offsets within slot) */
  36. #define LSER_REG_CHAR 0 /* Character in/out */
  37. #define LSER_REG_WIRQ 4 /* Write interrupt status */
  38. #define LSER_REG_RIRQ 8 /* Read interrupt status */
  39. /* Bits in the IRQ registers */
  40. #define LSER_IRQ_ENABLE 1
  41. #define LSER_IRQ_ACTIVE 2
  42. void
  43. lser_irq(void *vsc)
  44. {
  45. struct lser_softc *sc = vsc;
  46. uint32_t x;
  47. bool clear_to_write = false;
  48. bool got_a_read = false;
  49. uint32_t ch = 0;
  50. spinlock_acquire(&sc->ls_lock);
  51. x = bus_read_register(sc->ls_busdata, sc->ls_buspos, LSER_REG_WIRQ);
  52. if (x & LSER_IRQ_ACTIVE) {
  53. x = LSER_IRQ_ENABLE;
  54. sc->ls_wbusy = 0;
  55. clear_to_write = true;
  56. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  57. LSER_REG_WIRQ, x);
  58. }
  59. x = bus_read_register(sc->ls_busdata, sc->ls_buspos, LSER_REG_RIRQ);
  60. if (x & LSER_IRQ_ACTIVE) {
  61. x = LSER_IRQ_ENABLE;
  62. ch = bus_read_register(sc->ls_busdata, sc->ls_buspos,
  63. LSER_REG_CHAR);
  64. got_a_read = true;
  65. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  66. LSER_REG_RIRQ, x);
  67. }
  68. spinlock_release(&sc->ls_lock);
  69. if (clear_to_write && sc->ls_start != NULL) {
  70. sc->ls_start(sc->ls_devdata);
  71. }
  72. if (got_a_read && sc->ls_input != NULL) {
  73. sc->ls_input(sc->ls_devdata, ch);
  74. }
  75. }
  76. void
  77. lser_write(void *vls, int ch)
  78. {
  79. struct lser_softc *ls = vls;
  80. spinlock_acquire(&ls->ls_lock);
  81. if (ls->ls_wbusy) {
  82. /*
  83. * We're not clear to write.
  84. *
  85. * This should not happen. It's the job of the driver
  86. * attached to us to not write until we call
  87. * ls->ls_start.
  88. *
  89. * (Note: if we're the console, the panic will go to
  90. * lser_writepolled for printing, because we hold a
  91. * spinlock and interrupts are off; it won't recurse.)
  92. */
  93. panic("lser: Not clear to write\n");
  94. }
  95. ls->ls_wbusy = true;
  96. bus_write_register(ls->ls_busdata, ls->ls_buspos, LSER_REG_CHAR, ch);
  97. spinlock_release(&ls->ls_lock);
  98. }
  99. static
  100. void
  101. lser_poll_until_write(struct lser_softc *sc)
  102. {
  103. uint32_t val;
  104. KASSERT(spinlock_do_i_hold(&sc->ls_lock));
  105. do {
  106. val = bus_read_register(sc->ls_busdata, sc->ls_buspos,
  107. LSER_REG_WIRQ);
  108. }
  109. while ((val & LSER_IRQ_ACTIVE) == 0);
  110. }
  111. void
  112. lser_writepolled(void *vsc, int ch)
  113. {
  114. struct lser_softc *sc = vsc;
  115. bool irqpending = false;
  116. spinlock_acquire(&sc->ls_lock);
  117. if (sc->ls_wbusy) {
  118. irqpending = true;
  119. lser_poll_until_write(sc);
  120. /* Clear the ready condition */
  121. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  122. LSER_REG_WIRQ, LSER_IRQ_ENABLE);
  123. }
  124. /* Send the character. */
  125. bus_write_register(sc->ls_busdata, sc->ls_buspos, LSER_REG_CHAR, ch);
  126. /* Wait until it's done. */
  127. lser_poll_until_write(sc);
  128. /*
  129. * If there wasn't already an IRQ pending, clear the ready condition.
  130. * But if there was, leave the ready condition, so we get to the
  131. * interrupt handler in due course.
  132. */
  133. if (!irqpending) {
  134. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  135. LSER_REG_WIRQ, LSER_IRQ_ENABLE);
  136. }
  137. spinlock_release(&sc->ls_lock);
  138. }
  139. /*
  140. * Mask the interrupt while we're writing in polling mode. Doing so
  141. * causes the interrupt line to flap on every character, which makes
  142. * the CPU receiving those interrupts upset.
  143. */
  144. void
  145. lser_startpolling(void *vsc)
  146. {
  147. struct lser_softc *sc = vsc;
  148. sc->ls_maskinterrupt(sc->ls_busdata, sc->ls_buspos);
  149. }
  150. void
  151. lser_endpolling(void *vsc)
  152. {
  153. struct lser_softc *sc = vsc;
  154. sc->ls_unmaskinterrupt(sc->ls_busdata, sc->ls_buspos);
  155. }
  156. int
  157. config_lser(struct lser_softc *sc, int lserno)
  158. {
  159. (void)lserno;
  160. /*
  161. * Enable interrupting.
  162. */
  163. spinlock_init(&sc->ls_lock);
  164. sc->ls_wbusy = false;
  165. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  166. LSER_REG_RIRQ, LSER_IRQ_ENABLE);
  167. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  168. LSER_REG_WIRQ, LSER_IRQ_ENABLE);
  169. return 0;
  170. }